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

        def create_testplan():
            self.login(admin_username, admin_password)
            body = json.dumps({"name": "CRUD test for testplan_rule"})
            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)
            self.assertIn("location", response._headers)
            in_json = json.loads(response.content)
            self.assertIn("id", in_json)
            self.testplan_id = in_json['id']

        def delete_testplan():
            self.login(admin_username, admin_password)
            response = self.client.delete(path=api_url + "/testplan/" + self.testplan_id,
                                          HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)

        def create():
            body = json.dumps({
                'ruleType': 'request',
                'enabled': True,
                'name': 'test_rule_1',
                'filter': {
                    'httpProtocol': 'HTTP/1.1',
                    'method': 'GET',
                    'headers': [
                        {'key': 'foo'},
                        {'key': 'fizz', 'value': 'buzz'},
                    ],
                },
                'action': {
                    'type': 'modify',
                    'method': 'PUT',
                    'url': 'www.newurl.com/foo/bar',
                    'setHeaders': [
                        {
                            'key': 'zaphod', 'value': 'beeblebrox',
                        },
                    ],
                    'deleteHeaders': [
                        {
                            'key': 'foo',
                        },
                    ],
                }
            })
            self.login(admin_username, admin_password)
            response = self.client.post(path=api_url + "/testplan/" + self.testplan_id + "/rule/",
                                     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)
            return in_json['id']

        def read(rule_id):
            self.login(admin_username, admin_password)
            response = self.client.get(path=api_url + "/testplan/" + self.testplan_id + "/rule/" + rule_id,
                                       HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)

        def update(rule_id):
            body = json.dumps({
                'ruleType': 'request',
                'enabled': True,
                'name': 'test_rule_4',
                'filter': {
                    'httpProtocol': 'HTTP/1.1',
                    'method': 'GET',
                    'headers': [
                        {'key': 'folk hero'},
                        {'key': 'instrument', 'value': 'violin'},
                    ],
                },
                'action': {
                    'type': 'newResponse',
                    'httpProtocol': 'CARRIERFLAMINGO/1.1',
                    'statusCode': 403,
                    'statusDescription': 'TOO FEATHERY',
                    'headers': [
                        {
                            'key': 'User-Agent',
                            'value': 'Broken Looking Glass 1.9'
                        },
                    ],
                    'payload': 'The answer to life, the universe, and everything...is----NO CARRIER.',
                }

            })
            self.login(admin_username, admin_password)
            response = self.client.put(path=api_url + "/testplan/" + self.testplan_id + "/rule/" + rule_id,
                                       data=body,
                                       content_type="application/json",
                                       HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)

        def delete(rule_id):
            self.login(admin_username, admin_password)
            response = self.client.delete(path=api_url + "/testplan/" + self.testplan_id + "/rule/" + rule_id,
                                          HTTP_X_AUTH_TOKEN=self.x_auth_token)
            self.assertEqual(response.status_code, 200)

        print("Creating temporary testplan for use within %s" % self.__class__)
        create_testplan()

        print("Testing CREATE #1 in %s" % self.__class__)
        rule1_id = create()

        print("Testing CREATE #2 in %s" % self.__class__)
        rule2_id = create()

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

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

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

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

        print(self.__class__ % "Removing temporary testplan for use within %s")
        delete_testplan()
dataset/ETHPy150Open emccode/heliosburn/heliosburn/django/hbproject/api/tests.py/TestplanRuleViewTestCase.test_crud/swapped operands of `%`