|
14 | 14 | # License for the specific language governing permissions and limitations |
15 | 15 | # under the License. |
16 | 16 | import uuid as sys_uuid |
| 17 | +from contextlib import nullcontext |
17 | 18 |
|
18 | 19 | import pytest |
19 | 20 | from bazooka import exceptions as bazooka_exc |
| 21 | +from gcl_iam.tests.functional import clients as iam_clients |
20 | 22 |
|
21 | 23 | from genesis_core.common import constants as common_c |
22 | 24 | from genesis_core.tests.functional.restapi.iam import base |
@@ -48,6 +50,55 @@ def test_create_user_space_login_400_error(self, user_api_noauth_client): |
48 | 50 | with pytest.raises(bazooka_exc.BadRequestError): |
49 | 51 | client.create_user(username=" ", password="test") |
50 | 52 |
|
| 53 | + def test_create_user_without_first_last_name_success( |
| 54 | + self, user_api_noauth_client |
| 55 | + ): |
| 56 | + client = user_api_noauth_client() |
| 57 | + for empty_name in ["", None]: |
| 58 | + name = f"test_no_names_{empty_name}".lower() |
| 59 | + user = client.create_user( |
| 60 | + username=name, |
| 61 | + password="password", |
| 62 | + first_name=empty_name, |
| 63 | + last_name=empty_name, |
| 64 | + ) |
| 65 | + assert user["username"] == name |
| 66 | + assert not user.get("first_name") |
| 67 | + assert not user.get("last_name") |
| 68 | + |
| 69 | + def test_update_user_clear_first_last_name_success( |
| 70 | + self, user_api_client, auth_test1_user |
| 71 | + ): |
| 72 | + client = user_api_client(auth_test1_user) |
| 73 | + for empty_name in ["", None]: |
| 74 | + result = client.update_user( |
| 75 | + auth_test1_user.uuid, |
| 76 | + first_name=empty_name, |
| 77 | + last_name=empty_name, |
| 78 | + ) |
| 79 | + assert result.get("first_name", None) == empty_name |
| 80 | + assert result.get("last_name", None) == empty_name |
| 81 | + |
| 82 | + def test_me_endpoint_with_empty_names_success( |
| 83 | + self, user_api_client, auth_test1_user |
| 84 | + ): |
| 85 | + client = user_api_client(auth_test1_user) |
| 86 | + |
| 87 | + # First clear the names |
| 88 | + client.update_user( |
| 89 | + auth_test1_user.uuid, |
| 90 | + first_name="", |
| 91 | + last_name="", |
| 92 | + ) |
| 93 | + |
| 94 | + # Verify in /me endpoint |
| 95 | + result = client.get( |
| 96 | + auth_test1_user.get_me_url(client.endpoint), |
| 97 | + ).json() |
| 98 | + |
| 99 | + assert result["user"]["first_name"] == "" |
| 100 | + assert result["user"]["last_name"] == "" |
| 101 | + |
51 | 102 | def test_create_user_and_check_roles( |
52 | 103 | self, user_api_client, auth_test1_user |
53 | 104 | ): |
@@ -139,15 +190,6 @@ def test_update_my_user_test1_auth_success( |
139 | 190 |
|
140 | 191 | assert result["username"] == "testxxx" |
141 | 192 |
|
142 | | - def test_update_my_user_400_error(self, user_api_client, auth_test1_user): |
143 | | - client = user_api_client(auth_test1_user) |
144 | | - |
145 | | - with pytest.raises(bazooka_exc.BadRequestError): |
146 | | - client.update_user( |
147 | | - auth_test1_user.uuid, |
148 | | - first_name="", |
149 | | - ) |
150 | | - |
151 | 193 | def test_update_other_user_test1_auth_forbidden( |
152 | 194 | self, user_api_client, auth_test1_user, auth_test2_user |
153 | 195 | ): |
@@ -354,3 +396,119 @@ def test_fields_in_me_info_success( |
354 | 396 | for field in user_has_only_fields: |
355 | 397 | result["user"].pop(field) |
356 | 398 | assert result["user"] == {} |
| 399 | + |
| 400 | + @pytest.mark.parametrize( |
| 401 | + "grant_type, auth_param, expectation", |
| 402 | + [ |
| 403 | + (c.GRANT_TYPE_PASSWORD, "username", nullcontext()), |
| 404 | + (c.GRANT_TYPE_PASSWORD_USERNAME, "username", nullcontext()), |
| 405 | + (c.GRANT_TYPE_PASSWORD_EMAIL, "email", nullcontext()), |
| 406 | + ( |
| 407 | + c.GRANT_TYPE_PASSWORD_PHONE, |
| 408 | + "phone", |
| 409 | + pytest.raises(bazooka_exc.BaseHTTPException), |
| 410 | + # auth by phone is not implemented yet |
| 411 | + ), |
| 412 | + ("invalid_grant_type", "username", pytest.raises(ValueError)), |
| 413 | + ], |
| 414 | + ) |
| 415 | + def test_auth_with_param( |
| 416 | + self, |
| 417 | + grant_type, |
| 418 | + auth_param, |
| 419 | + expectation, |
| 420 | + user_api, |
| 421 | + auth_test1_user, |
| 422 | + ): |
| 423 | + params = { |
| 424 | + "username": "dummy_username", |
| 425 | + "password": auth_test1_user.password, |
| 426 | + "grant_type": grant_type, |
| 427 | + } |
| 428 | + params[auth_param] = (getattr(auth_test1_user, auth_param, None),) |
| 429 | + auth = iam_clients.GenesisCoreAuth(**params) |
| 430 | + with expectation: |
| 431 | + client = iam_clients.GenesisCoreTestRESTClient( |
| 432 | + f"{user_api.get_endpoint()}v1/", |
| 433 | + auth, |
| 434 | + ) # tries to authorise on init |
| 435 | + assert "access_token" in client._auth_cache |
| 436 | + |
| 437 | + @pytest.mark.parametrize( |
| 438 | + "login, password, expectation", |
| 439 | + [ |
| 440 | + ("username", None, nullcontext()), |
| 441 | + ("email", None, nullcontext()), |
| 442 | + ("phone", None, pytest.raises(bazooka_exc.BaseHTTPException)), |
| 443 | + ("username", "wrong", pytest.raises(bazooka_exc.BadRequestError)), |
| 444 | + ("username", "", pytest.raises(bazooka_exc.BadRequestError)), |
| 445 | + ("null", None, pytest.raises(bazooka_exc.NotFoundError)), |
| 446 | + ], |
| 447 | + ) |
| 448 | + def test_auth_with_login( |
| 449 | + self, |
| 450 | + login, |
| 451 | + password, |
| 452 | + expectation, |
| 453 | + user_api, |
| 454 | + auth_test1_user, |
| 455 | + ): |
| 456 | + params = { |
| 457 | + "username": "dummy_username", |
| 458 | + "password": auth_test1_user.password, |
| 459 | + "grant_type": c.GRANT_TYPE_PASSWORD_LOGIN, |
| 460 | + "login": getattr(auth_test1_user, login, "doesnt_exist"), |
| 461 | + } |
| 462 | + if password is not None: |
| 463 | + params["password"] = password |
| 464 | + |
| 465 | + auth = iam_clients.GenesisCoreAuth(**params) |
| 466 | + with expectation: |
| 467 | + client = iam_clients.GenesisCoreTestRESTClient( |
| 468 | + f"{user_api.get_endpoint()}v1/", |
| 469 | + auth, |
| 470 | + ) # tries to authorise on init |
| 471 | + assert "access_token" in client._auth_cache |
| 472 | + |
| 473 | + @pytest.mark.parametrize( |
| 474 | + "grant_type,use_email,field_name", |
| 475 | + [ |
| 476 | + (c.GRANT_TYPE_PASSWORD_USERNAME, False, "username"), |
| 477 | + (c.GRANT_TYPE_PASSWORD_LOGIN, False, "login"), |
| 478 | + (c.GRANT_TYPE_PASSWORD_LOGIN, True, "login"), |
| 479 | + (c.GRANT_TYPE_PASSWORD_EMAIL, True, "email"), |
| 480 | + ], |
| 481 | + ) |
| 482 | + def test_auth_case_insensitivity( |
| 483 | + self, user_api, auth_test1_user, grant_type, use_email, field_name |
| 484 | + ): |
| 485 | + # Set up email if needed |
| 486 | + if use_email: |
| 487 | + user = iam_models.User.objects.get_one( |
| 488 | + filters={"uuid": auth_test1_user.uuid} |
| 489 | + ) |
| 490 | + user.email = "test1@mail.com" |
| 491 | + user.save() |
| 492 | + email_value = user.email.upper() |
| 493 | + |
| 494 | + params = { |
| 495 | + "username": "dummy_username", |
| 496 | + "password": auth_test1_user.password, |
| 497 | + "grant_type": grant_type, |
| 498 | + } |
| 499 | + if grant_type == c.GRANT_TYPE_PASSWORD_USERNAME: |
| 500 | + params[field_name] = auth_test1_user.username.upper() |
| 501 | + elif grant_type == c.GRANT_TYPE_PASSWORD_EMAIL: |
| 502 | + params[field_name] = email_value |
| 503 | + elif grant_type == c.GRANT_TYPE_PASSWORD_LOGIN: |
| 504 | + params[field_name] = ( |
| 505 | + email_value if use_email else auth_test1_user.username.upper() |
| 506 | + ) |
| 507 | + |
| 508 | + # Test authentication |
| 509 | + auth = iam_clients.GenesisCoreAuth(**params) |
| 510 | + client = iam_clients.GenesisCoreTestRESTClient( |
| 511 | + f"{user_api.get_endpoint()}v1/", |
| 512 | + auth, |
| 513 | + ) # tries to authorise on init |
| 514 | + assert "access_token" in client._auth_cache |
0 commit comments