summaryrefslogtreecommitdiff
path: root/gitlab/tests/test_gitlab_auth.py
blob: 314fbedb94252483e123ba9fef3e1e036770f275 (plain)
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
import pytest
import requests

from gitlab import Gitlab


def test_invalid_auth_args():
    with pytest.raises(ValueError):
        Gitlab(
            "http://localhost",
            api_version="4",
            private_token="private_token",
            oauth_token="bearer",
        )
    with pytest.raises(ValueError):
        Gitlab(
            "http://localhost",
            api_version="4",
            oauth_token="bearer",
            http_username="foo",
            http_password="bar",
        )
    with pytest.raises(ValueError):
        Gitlab(
            "http://localhost",
            api_version="4",
            private_token="private_token",
            http_password="bar",
        )
    with pytest.raises(ValueError):
        Gitlab(
            "http://localhost",
            api_version="4",
            private_token="private_token",
            http_username="foo",
        )


def test_private_token_auth():
    gl = Gitlab("http://localhost", private_token="private_token", api_version="4")
    assert gl.private_token == "private_token"
    assert gl.oauth_token is None
    assert gl.job_token is None
    assert gl._http_auth is None
    assert "Authorization" not in gl.headers
    assert gl.headers["PRIVATE-TOKEN"] == "private_token"
    assert "JOB-TOKEN" not in gl.headers


def test_oauth_token_auth():
    gl = Gitlab("http://localhost", oauth_token="oauth_token", api_version="4")
    assert gl.private_token is None
    assert gl.oauth_token == "oauth_token"
    assert gl.job_token is None
    assert gl._http_auth is None
    assert gl.headers["Authorization"] == "Bearer oauth_token"
    assert "PRIVATE-TOKEN" not in gl.headers
    assert "JOB-TOKEN" not in gl.headers


def test_job_token_auth():
    gl = Gitlab("http://localhost", job_token="CI_JOB_TOKEN", api_version="4")
    assert gl.private_token is None
    assert gl.oauth_token is None
    assert gl.job_token == "CI_JOB_TOKEN"
    assert gl._http_auth is None
    assert "Authorization" not in gl.headers
    assert "PRIVATE-TOKEN" not in gl.headers
    assert gl.headers["JOB-TOKEN"] == "CI_JOB_TOKEN"


def test_http_auth():
    gl = Gitlab(
        "http://localhost",
        private_token="private_token",
        http_username="foo",
        http_password="bar",
        api_version="4",
    )
    assert gl.private_token == "private_token"
    assert gl.oauth_token is None
    assert gl.job_token is None
    assert isinstance(gl._http_auth, requests.auth.HTTPBasicAuth)
    assert gl.headers["PRIVATE-TOKEN"] == "private_token"
    assert "Authorization" not in gl.headers