|
| 1 | +import http |
| 2 | +import pytest |
| 3 | + |
| 4 | +pytestmark = [pytest.mark.parallel] |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def session_user(pulpcore_bindings, gen_user, anonymous_user): |
| 9 | + old_cookie = pulpcore_bindings.client.cookie |
| 10 | + user = gen_user() |
| 11 | + with user: |
| 12 | + response = pulpcore_bindings.LoginApi.login_with_http_info() |
| 13 | + if isinstance(response, tuple): |
| 14 | + # old bindings |
| 15 | + _, _, headers = response |
| 16 | + else: |
| 17 | + # new bindings |
| 18 | + headers = response.headers |
| 19 | + cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) |
| 20 | + # Use anonymous_user to remove the basic auth header from the api client. |
| 21 | + with anonymous_user: |
| 22 | + pulpcore_bindings.client.cookie = "; ".join( |
| 23 | + (f"{k}={v.value}" for k, v in cookie_jar.items()) |
| 24 | + ) |
| 25 | + yield user |
| 26 | + pulpcore_bindings.client.cookie = old_cookie |
| 27 | + |
| 28 | + |
| 29 | +def test_login_read_denies_anonymous(pulpcore_bindings, anonymous_user): |
| 30 | + with anonymous_user: |
| 31 | + with pytest.raises(pulpcore_bindings.module.ApiException) as exc: |
| 32 | + pulpcore_bindings.LoginApi.login_read() |
| 33 | + assert exc.value.status == 401 |
| 34 | + |
| 35 | + |
| 36 | +def test_login_read_returns_username(pulpcore_bindings, gen_user): |
| 37 | + user = gen_user() |
| 38 | + with user: |
| 39 | + result = pulpcore_bindings.LoginApi.login_read() |
| 40 | + assert result.username == user.username |
| 41 | + |
| 42 | + |
| 43 | +def test_login_denies_anonymous(pulpcore_bindings, anonymous_user): |
| 44 | + with anonymous_user: |
| 45 | + with pytest.raises(pulpcore_bindings.module.ApiException) as exc: |
| 46 | + pulpcore_bindings.LoginApi.login() |
| 47 | + assert exc.value.status == 401 |
| 48 | + |
| 49 | + |
| 50 | +def test_login_sets_session_cookie(pulpcore_bindings, gen_user): |
| 51 | + user = gen_user() |
| 52 | + with user: |
| 53 | + response = pulpcore_bindings.LoginApi.login_with_http_info() |
| 54 | + if isinstance(response, tuple): |
| 55 | + # old bindings |
| 56 | + result, status, headers = response |
| 57 | + else: |
| 58 | + # new bindings |
| 59 | + result = response.data |
| 60 | + status = response.status |
| 61 | + headers = response.headers |
| 62 | + assert status == 201 |
| 63 | + assert result.username == user.username |
| 64 | + cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) |
| 65 | + assert cookie_jar["sessionid"].value != "" |
| 66 | + assert cookie_jar["csrftoken"].value != "" |
| 67 | + |
| 68 | + |
| 69 | +def test_session_cookie_is_authorization(pulpcore_bindings, anonymous_user, session_user): |
| 70 | + result = pulpcore_bindings.LoginApi.login_read() |
| 71 | + assert result.username == session_user.username |
| 72 | + |
| 73 | + |
| 74 | +# For whatever reason, this tests fails with '{"detail":"CSRF Failed: CSRF token missing."}' |
| 75 | +# But we sent the csrf token along... |
| 76 | +# The test right after this tries to close the gap and uses basic auth to logout. |
| 77 | +# Please remove it when this one is fixed. |
| 78 | +@pytest.mark.xfail |
| 79 | +def test_logout_removes_sessionid(pulpcore_bindings, session_user): |
| 80 | + response = pulpcore_bindings.LoginApi.logout_with_http_info() |
| 81 | + if isinstance(response, tuple): |
| 82 | + # old bindings |
| 83 | + _, status, headers = response |
| 84 | + else: |
| 85 | + # new bindings |
| 86 | + status = response.status |
| 87 | + headers = response.headers |
| 88 | + assert status == 204 |
| 89 | + cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) |
| 90 | + assert cookie_jar["sessionid"].value == "" |
| 91 | + |
| 92 | + |
| 93 | +def test_basicauth_logout_removes_sessionid(pulpcore_bindings, session_user): |
| 94 | + with session_user: |
| 95 | + response = pulpcore_bindings.LoginApi.logout_with_http_info() |
| 96 | + if isinstance(response, tuple): |
| 97 | + # old bindings |
| 98 | + _, status, headers = response |
| 99 | + else: |
| 100 | + # new bindings |
| 101 | + status = response.status |
| 102 | + headers = response.headers |
| 103 | + assert status == 204 |
| 104 | + cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) |
| 105 | + assert cookie_jar["sessionid"].value == "" |
| 106 | + |
| 107 | + |
| 108 | +def test_logout_denies_anonymous(pulpcore_bindings, anonymous_user): |
| 109 | + with anonymous_user: |
| 110 | + with pytest.raises(pulpcore_bindings.module.ApiException) as exc: |
| 111 | + pulpcore_bindings.LoginApi.logout() |
| 112 | + assert exc.value.status == 401 |
0 commit comments