|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ee/api/group_access_tokens.html |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def resp_list_group_access_token(): |
| 11 | + content = [ |
| 12 | + { |
| 13 | + "user_id": 141, |
| 14 | + "scopes": ["api"], |
| 15 | + "name": "token", |
| 16 | + "expires_at": "2021-01-31", |
| 17 | + "id": 42, |
| 18 | + "active": True, |
| 19 | + "created_at": "2021-01-20T22:11:48.151Z", |
| 20 | + "revoked": False, |
| 21 | + } |
| 22 | + ] |
| 23 | + |
| 24 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: |
| 25 | + rsps.add( |
| 26 | + method=responses.GET, |
| 27 | + url="http://localhost/api/v4/groups/1/access_tokens", |
| 28 | + json=content, |
| 29 | + content_type="application/json", |
| 30 | + status=200, |
| 31 | + ) |
| 32 | + yield rsps |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def resp_create_group_access_token(): |
| 37 | + content = { |
| 38 | + "user_id": 141, |
| 39 | + "scopes": ["api"], |
| 40 | + "name": "token", |
| 41 | + "expires_at": "2021-01-31", |
| 42 | + "id": 42, |
| 43 | + "active": True, |
| 44 | + "created_at": "2021-01-20T22:11:48.151Z", |
| 45 | + "revoked": False, |
| 46 | + } |
| 47 | + |
| 48 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: |
| 49 | + rsps.add( |
| 50 | + method=responses.POST, |
| 51 | + url="http://localhost/api/v4/groups/1/access_tokens", |
| 52 | + json=content, |
| 53 | + content_type="application/json", |
| 54 | + status=200, |
| 55 | + ) |
| 56 | + yield rsps |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def resp_revoke_group_access_token(): |
| 61 | + content = [ |
| 62 | + { |
| 63 | + "user_id": 141, |
| 64 | + "scopes": ["api"], |
| 65 | + "name": "token", |
| 66 | + "expires_at": "2021-01-31", |
| 67 | + "id": 42, |
| 68 | + "active": True, |
| 69 | + "created_at": "2021-01-20T22:11:48.151Z", |
| 70 | + "revoked": False, |
| 71 | + } |
| 72 | + ] |
| 73 | + |
| 74 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: |
| 75 | + rsps.add( |
| 76 | + method=responses.DELETE, |
| 77 | + url="http://localhost/api/v4/groups/1/access_tokens/42", |
| 78 | + json=content, |
| 79 | + content_type="application/json", |
| 80 | + status=204, |
| 81 | + ) |
| 82 | + rsps.add( |
| 83 | + method=responses.GET, |
| 84 | + url="http://localhost/api/v4/groups/1/access_tokens", |
| 85 | + json=content, |
| 86 | + content_type="application/json", |
| 87 | + status=200, |
| 88 | + ) |
| 89 | + yield rsps |
| 90 | + |
| 91 | + |
| 92 | +def test_list_group_access_tokens(gl, resp_list_group_access_token): |
| 93 | + access_tokens = gl.groups.get(1, lazy=True).access_tokens.list() |
| 94 | + assert len(access_tokens) == 1 |
| 95 | + assert access_tokens[0].revoked is False |
| 96 | + assert access_tokens[0].name == "token" |
| 97 | + |
| 98 | + |
| 99 | +def test_create_group_access_token(gl, resp_create_group_access_token): |
| 100 | + access_tokens = gl.groups.get(1, lazy=True).access_tokens.create( |
| 101 | + {"name": "test", "scopes": ["api"]} |
| 102 | + ) |
| 103 | + assert access_tokens.revoked is False |
| 104 | + assert access_tokens.user_id == 141 |
| 105 | + assert access_tokens.expires_at == "2021-01-31" |
| 106 | + |
| 107 | + |
| 108 | +def test_revoke_group_access_token( |
| 109 | + gl, resp_list_group_access_token, resp_revoke_group_access_token |
| 110 | +): |
| 111 | + gl.groups.get(1, lazy=True).access_tokens.delete(42) |
| 112 | + access_token = gl.groups.get(1, lazy=True).access_tokens.list()[0] |
| 113 | + access_token.delete() |
0 commit comments