Exemple #1
0
    def test_patch_user_api_with_spaces_in_name_ok(self):
        user_id = "851f7b34-f76c-49de-a382-7e4089b744e2"

        expected_status = HTTPStatus.NO_CONTENT
        user_jsonpatch = [
            {"op": "replace", "path": "/title", "value": "Sir"},
            {"op": "replace", "path": "/first_name", "value": " Antônio"},
            {"op": "replace", "path": "/last_name", "value": "Jobim "},
            {"op": "replace", "path": "/email", "value": "*****@*****.**"},
            {"op": "replace", "path": "/auth0_id", "value": "new-auth0-id"},
            {"op": "replace", "path": "/status", "value": "singing"},
            {"op": "replace", "path": "/country_code", "value": "BR"},
        ]
        body = json.dumps(user_jsonpatch)
        path_parameters = {"id": user_id}

        result = test_patch(
            patch_user_api,
            ENTITY_BASE_URL,
            path_parameters=path_parameters,
            request_body=body,
        )
        result_status = result["statusCode"]

        self.assertEqual(expected_status, result_status)
        # now check database values...
        path_parameters = {"id": user_id}

        expected_body = {
            "id": user_id,
            "email": "*****@*****.**",
            "title": "Sir",
            "first_name": "Antônio",
            "last_name": "Jobim",
            "auth0_id": "new-auth0-id",
            "country_code": "BR",
            "country_name": "Brazil",
            "crm_id": None,
            "avatar_string": "AJ",
            "status": "singing",
        }

        result = test_get(
            u.get_user_by_id_api, ENTITY_BASE_URL, path_parameters=path_parameters
        )
        result_json = json.loads(result["body"])
        for ignored_attribute in [
            "created",
            "modified",
            "has_demo_project",
            "has_live_project",
        ]:
            result_json.pop(ignored_attribute, None)
            expected_body.pop(ignored_attribute, None)
        self.assertEqual(expected_body, result_json)
Exemple #2
0
    def test_14_patch_user_api_bad_jsonpatch(self):
        expected_status = HTTPStatus.BAD_REQUEST
        user_jsonpatch = [{"this": "is", "not": "/a", "valid": "jsonpatch"}]
        body = json.dumps(user_jsonpatch)
        path_parameters = {"id": "d1070e81-557e-40eb-a7ba-b951ddb7ebdc"}

        result = test_patch(
            patch_user_api,
            ENTITY_BASE_URL,
            path_parameters=path_parameters,
            request_body=body,
        )
        result_status = result["statusCode"]
        result_json = json.loads(result["body"])

        self.assertEqual(expected_status, result_status)
        self.assertTrue("correlation_id" in result_json)
        self.assertTrue(
            "message" in result_json
            and result_json["message"].endswith("invalid jsonpatch")
        )
Exemple #3
0
    def test_13_patch_user_api_bad_operation(self):
        expected_status = HTTPStatus.BAD_REQUEST
        user_jsonpatch = [
            {"op": "non-existent-operation", "path": "/first_name", "value": "simon"}
        ]
        body = json.dumps(user_jsonpatch)
        path_parameters = {"id": "d1070e81-557e-40eb-a7ba-b951ddb7ebdc"}

        result = test_patch(
            patch_user_api,
            ENTITY_BASE_URL,
            path_parameters=path_parameters,
            request_body=body,
        )
        result_status = result["statusCode"]
        result_json = json.loads(result["body"])

        self.assertEqual(expected_status, result_status)
        self.assertTrue("correlation_id" in result_json)
        self.assertTrue(
            "message" in result_json and result_json["message"] == "invalid jsonpatch"
        )
Exemple #4
0
    def test_11_patch_user_api_user_not_exists(self):
        expected_status = HTTPStatus.NOT_FOUND
        user_jsonpatch = [
            {"op": "replace", "path": "/title", "value": "Sir"},
            {"op": "replace", "path": "/first_name", "value": "simon"},
        ]
        body = json.dumps(user_jsonpatch)
        path_parameters = {"id": "d1070e81-557e-40eb-a7ba-b951ddb7ebdd"}

        result = test_patch(
            patch_user_api,
            ENTITY_BASE_URL,
            path_parameters=path_parameters,
            request_body=body,
        )
        result_status = result["statusCode"]
        result_json = json.loads(result["body"])

        self.assertEqual(expected_status, result_status)
        self.assertTrue("correlation_id" in result_json)
        self.assertTrue(
            "message" in result_json and "does not exist" in result_json["message"]
        )
Exemple #5
0
    def test_20_patch_user_api_ok(self):
        user_id = "d1070e81-557e-40eb-a7ba-b951ddb7ebdc"

        expected_status = HTTPStatus.NO_CONTENT
        user_jsonpatch = [
            {"op": "replace", "path": "/title", "value": "Sir"},
            {"op": "replace", "path": "/first_name", "value": "simon"},
            {"op": "replace", "path": "/last_name", "value": "smith"},
            {"op": "replace", "path": "/email", "value": "*****@*****.**"},
            {"op": "replace", "path": "/auth0_id", "value": "new-auth0-id"},
            {"op": "replace", "path": "/status", "value": "singing"},
            {"op": "replace", "path": "/country_code", "value": "GB-SCT"},
        ]
        body = json.dumps(user_jsonpatch)
        path_parameters = {"id": user_id}

        result = test_patch(
            patch_user_api,
            ENTITY_BASE_URL,
            path_parameters=path_parameters,
            request_body=body,
        )
        result_status = result["statusCode"]

        self.assertEqual(expected_status, result_status)
        # now check database values...
        path_parameters = {"id": user_id}

        expected_body = {
            "id": user_id,
            "created": f"2018-08-17T{tz_hour}:10:56.798192+{tz_offset}",
            "email": "*****@*****.**",
            "title": "Sir",
            "first_name": "simon",
            "last_name": "smith",
            "auth0_id": "new-auth0-id",
            "country_code": "GB-SCT",
            "country_name": "United Kingdom - Scotland",
            "crm_id": None,
            "avatar_string": "ss",
            "status": "singing",
        }

        result = test_get(
            u.get_user_by_id_api, ENTITY_BASE_URL, path_parameters=path_parameters
        )
        result_json = json.loads(result["body"])

        # will test modified separately so extract it from dictionary here
        self.now_datetime_test_and_remove(
            result_json, "modified", tolerance=TIME_TOLERANCE_SECONDS
        )

        # now check that we have a corresponding entity update record
        entity_updates = EntityUpdate.get_entity_updates_for_entity(
            "user", user_id, new_correlation_id()
        )
        self.assertTrue(len(entity_updates) > 0, "No entity update record found")
        if len(entity_updates) > 0:
            # get most recent update record
            last_entity_update = entity_updates[-1]

            # remove from returned value those things we don't want to test
            self.remove_dict_items_to_be_ignored_by_tests(
                last_entity_update, ["id", "modified"]
            )

            # remove and store data items to be tested individually
            # check created datetime - allow up to TIME_TOLERANCE_SECONDS difference
            self.now_datetime_test_and_remove(
                last_entity_update, "created", tolerance=TIME_TOLERANCE_SECONDS
            )

            result_json_reverse_patch = last_entity_update["json_reverse_patch"]
            del last_entity_update["json_reverse_patch"]
            result_json_patch = last_entity_update["json_patch"]
            del last_entity_update["json_patch"]

            # check jsonpatch - compare as lists in case order different
            result_json_patch = json.loads(result_json_patch)
            self.assertCountEqual(user_jsonpatch, result_json_patch)

            # need to compare list objects not strings as elements may be in different order
            result_json_reverse_patch = json.loads(result_json_reverse_patch)
            expected_json_reverse_patch = [
                {"op": "replace", "path": "/first_name", "value": "Altha"},
                {"op": "replace", "path": "/auth0_id", "value": None},
                {"op": "replace", "path": "/title", "value": "Mrs"},
                {"op": "replace", "path": "/last_name", "value": "Alcorn"},
                {"op": "replace", "path": "/status", "value": None},
                {"op": "replace", "path": "/email", "value": "*****@*****.**"},
                {"op": "replace", "path": "/country_code", "value": "GB"},
            ]
            self.assertCountEqual(
                expected_json_reverse_patch, result_json_reverse_patch
            )

            # and finally check what's left
            expected_body = {
                "entity_name": "user",
                "entity_id": user_id,
            }
            self.assertDictEqual(expected_body, last_entity_update)