def test_crud(self):
        """
        Tests CRUD for session model.
        """
        import json

        def create():
            self.login(admin_username, admin_password)
            body = json.dumps({
                'name': 'CRUD test',
                'description': 'CRUD test',
                'upstreamHost': '127.0.0.1',
                'upstreamPort': 8080,
                })

            response = self.client.post(path=api_url + "/session/",
                                        data=body,
                                        content_type="application/json",
                                        HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)
            self.assertIn("location", response._headers)
            in_json = json.loads(response.content)
            self.assertIn("id", in_json)
            self.session_id = in_json['id']

        def read():
            self.login(admin_username, admin_password)
            response = self.client.get(api_url + "/session/" + self.session_id, HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)

            self.login('test1', 'test1')
            response = self.client.get(api_url + "/session/" + self.session_id, HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 403)

        def update():
            self.login(admin_username, admin_password)
            body = json.dumps({'name': 'CRUD test updated'})
            response = self.client.put(path=api_url + "/session/" + self.session_id,
                                       data=body,
                                       content_type="application/json",
                                       HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)

            self.login(test1_username, test1_password)
            response = self.client.put(path=api_url + "/session/" + self.session_id,
                                       data=body,
                                       content_type="application/json",
                                       HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 403)

        def delete():
            self.login(test1_username, test1_password)
            response = self.client.delete(path=api_url + "/session/" + self.session_id,
                                          HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 403)

            self.login(admin_username, admin_password)
            response = self.client.delete(path=api_url + "/session/" + self.session_id,
                                          HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)

        def linked_crud():
            # Create a testplan
            self.login(admin_username, admin_password)
            body = json.dumps({"name": "crud testplan"})
            response = self.client.post(path=api_url + "/testplan/",
                                        data=body,
                                        content_type="application/json",
                                        HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)
            testplan_id = json.loads(response.content)['id']

            # Create a session linked to testplan
            body = json.dumps({
                'name': 'crud session',
                'description': 'crud session description',
                'testplan': testplan_id,
                "upstreamHost": "127.0.0.1",
                "upstreamPort": 8080
            })
            response = self.client.post(path=api_url + "/session",
                                        data=body,
                                        content_type="application/json",
                                        HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)
            session_id = json.loads(response.content)['id']

            # Delete session and testplan
            self.client.delete(path=api_url + "/session/" + session_id,
                               HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)
            self.client.delete(path=api_url + "/testplan/" + testplan_id,
                               HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)

        print("Testing CREATE in %s" % self.__class__)
        create()

        print("Testing READ in %s" % self.__class__)
        read()

        print("Testing UPDATE in %s" % self.__class__)
        update()

        print("Testing DELETE in %s" % self.__class__)
        delete()

        print("Testing CRUD specific to a linked testplan in %s" % self.__class__)
        linked_crud()
dataset/ETHPy150Open emccode/heliosburn/heliosburn/django/hbproject/api/tests.py/SessionViewTestCase.test_crud/original