File: test_group_access_tokens.py

package info (click to toggle)
python-gitlab 1%3A4.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,048 kB
  • sloc: python: 24,168; makefile: 171; ruby: 27; javascript: 3
file content (129 lines) | stat: -rw-r--r-- 3,914 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
GitLab API: https://docs.gitlab.com/ee/api/group_access_tokens.html
"""

import pytest
import responses

from gitlab.v4.objects import GroupAccessToken


@pytest.fixture
def resp_list_group_access_token(token_content):
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(
            method=responses.GET,
            url="http://localhost/api/v4/groups/1/access_tokens",
            json=[token_content],
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_get_group_access_token(token_content):
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(
            method=responses.GET,
            url="http://localhost/api/v4/groups/1/access_tokens/1",
            json=token_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_create_group_access_token(token_content):
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/groups/1/access_tokens",
            json=token_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_revoke_group_access_token():
    content = [
        {
            "user_id": 141,
            "scopes": ["api"],
            "name": "token",
            "expires_at": "2021-01-31",
            "id": 42,
            "active": True,
            "created_at": "2021-01-20T22:11:48.151Z",
            "revoked": False,
        }
    ]

    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(
            method=responses.DELETE,
            url="http://localhost/api/v4/groups/1/access_tokens/42",
            status=204,
        )
        rsps.add(
            method=responses.GET,
            url="http://localhost/api/v4/groups/1/access_tokens",
            json=content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_rotate_group_access_token(token_content):
    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/groups/1/access_tokens/1/rotate",
            json=token_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


def test_list_group_access_tokens(gl, resp_list_group_access_token):
    access_tokens = gl.groups.get(1, lazy=True).access_tokens.list()
    assert len(access_tokens) == 1
    assert access_tokens[0].revoked is False
    assert access_tokens[0].name == "token"


def test_get_group_access_token(group, resp_get_group_access_token):
    access_token = group.access_tokens.get(1)
    assert isinstance(access_token, GroupAccessToken)
    assert access_token.revoked is False
    assert access_token.name == "token"


def test_create_group_access_token(gl, resp_create_group_access_token):
    access_tokens = gl.groups.get(1, lazy=True).access_tokens.create(
        {"name": "test", "scopes": ["api"]}
    )
    assert access_tokens.revoked is False
    assert access_tokens.user_id == 141
    assert access_tokens.expires_at == "2021-01-31"


def test_revoke_group_access_token(
    gl, resp_list_group_access_token, resp_revoke_group_access_token
):
    gl.groups.get(1, lazy=True).access_tokens.delete(42)
    access_token = gl.groups.get(1, lazy=True).access_tokens.list()[0]
    access_token.delete()


def test_rotate_group_access_token(group, resp_rotate_group_access_token):
    access_token = group.access_tokens.get(1, lazy=True)
    access_token.rotate()
    assert isinstance(access_token, GroupAccessToken)
    assert access_token.token == "s3cr3t"