|
| 1 | +import http |
| 2 | +import pytest |
| 3 | + |
| 4 | +pytestmark = [pytest.mark.parallel] |
| 5 | + |
| 6 | + |
| 7 | +def test_login_read_denies_anonymous(pulpcore_bindings, anonymous_user): |
| 8 | + with anonymous_user: |
| 9 | + with pytest.raises(pulpcore_bindings.module.ApiException) as exc: |
| 10 | + pulpcore_bindings.LoginApi.login_read() |
| 11 | + assert exc.value.status == 401 |
| 12 | + |
| 13 | + |
| 14 | +def test_login_read_returns_admin(pulpcore_bindings): |
| 15 | + result = pulpcore_bindings.LoginApi.login_read() |
| 16 | + assert result.username == "admin" |
| 17 | + |
| 18 | + |
| 19 | +def test_login_read_returns_username(pulpcore_bindings, gen_user): |
| 20 | + user = gen_user() |
| 21 | + with user: |
| 22 | + result = pulpcore_bindings.LoginApi.login_read() |
| 23 | + assert result.username == user.username |
| 24 | + |
| 25 | + |
| 26 | +def test_login_denies_anonymous(pulpcore_bindings, anonymous_user): |
| 27 | + with anonymous_user: |
| 28 | + with pytest.raises(pulpcore_bindings.module.ApiException) as exc: |
| 29 | + pulpcore_bindings.LoginApi.login() |
| 30 | + assert exc.value.status == 401 |
| 31 | + |
| 32 | + |
| 33 | +def test_login_returns_username(pulpcore_bindings, gen_user): |
| 34 | + user = gen_user() |
| 35 | + with user: |
| 36 | + pulpcore_bindings.LoginApi.login() |
| 37 | + response = pulpcore_bindings.LoginApi.login_with_http_info() |
| 38 | + if isinstance(response, tuple): |
| 39 | + # old bindings |
| 40 | + result, status, headers = response |
| 41 | + else: |
| 42 | + # new bindings |
| 43 | + result = response.data |
| 44 | + status = response.status |
| 45 | + headers = response.headers |
| 46 | + assert status == 201 |
| 47 | + assert result.username == user.username |
| 48 | + cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) |
| 49 | + assert cookie_jar["sessionid"] != "" |
| 50 | + assert cookie_jar["csrftoken"] != "" |
| 51 | + |
| 52 | + |
| 53 | +def test_logout_removes_sessionid(pulpcore_bindings, gen_user): |
| 54 | + user = gen_user() |
| 55 | + with user: |
| 56 | + response = pulpcore_bindings.LoginApi.logout_with_http_info() |
| 57 | + if isinstance(response, tuple): |
| 58 | + # old bindings |
| 59 | + _, status, headers = response |
| 60 | + else: |
| 61 | + # new bindings |
| 62 | + status = response.status |
| 63 | + headers = response.headers |
| 64 | + assert status == 204 |
| 65 | + if "set-cookie" in headers: |
| 66 | + # TODO It is indeed a bit unclear how to use a session cookie with the bindings. |
| 67 | + # Revisit after updating the bindings container may be worth a shot. |
| 68 | + cookie_jar = http.cookies.SimpleCookie(headers["set-cookie"]) |
| 69 | + assert cookie_jar["sessionid"] == "" |
| 70 | + assert cookie_jar["csrftoken"] != "" |
| 71 | + |
| 72 | + |
| 73 | +def test_logout_denies_anonymous(pulpcore_bindings, anonymous_user): |
| 74 | + with anonymous_user: |
| 75 | + with pytest.raises(pulpcore_bindings.module.ApiException) as exc: |
| 76 | + pulpcore_bindings.LoginApi.logout() |
| 77 | + assert exc.value.status == 401 |
0 commit comments