summaryrefslogtreecommitdiff
path: root/gitlab/tests/objects/test_commits.py
blob: 9d11508c6ee838d6bf0744ecf435b04132ee2fb0 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
GitLab API: https://docs.gitlab.com/ce/api/commits.html
"""

import pytest
import responses


@pytest.fixture
def resp_create_commit():
    content = {
        "id": "ed899a2f4b50b4370feeea94676502b42383c746",
        "short_id": "ed899a2f",
        "title": "Commit message",
    }

    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/projects/1/repository/commits",
            json=content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_commit():
    get_content = {
        "id": "6b2257eabcec3db1f59dafbd84935e3caea04235",
        "short_id": "6b2257ea",
        "title": "Initial commit",
    }
    revert_content = {
        "id": "8b090c1b79a14f2bd9e8a738f717824ff53aebad",
        "short_id": "8b090c1b",
        "title": 'Revert "Initial commit"',
    }

    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(
            method=responses.GET,
            url="http://localhost/api/v4/projects/1/repository/commits/6b2257ea",
            json=get_content,
            content_type="application/json",
            status=200,
        )
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/projects/1/repository/commits/6b2257ea/revert",
            json=revert_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_get_commit_gpg_signature():
    content = {
        "gpg_key_id": 1,
        "gpg_key_primary_keyid": "8254AAB3FBD54AC9",
        "gpg_key_user_name": "John Doe",
        "gpg_key_user_email": "johndoe@example.com",
        "verification_status": "verified",
        "gpg_key_subkey_id": None,
    }

    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.GET,
            url="http://localhost/api/v4/projects/1/repository/commits/6b2257ea/signature",
            json=content,
            content_type="application/json",
            status=200,
        )
        yield rsps


def test_get_commit(project, resp_commit):
    commit = project.commits.get("6b2257ea")
    assert commit.short_id == "6b2257ea"
    assert commit.title == "Initial commit"


def test_create_commit(project, resp_create_commit):
    data = {
        "branch": "master",
        "commit_message": "Commit message",
        "actions": [{"action": "create", "file_path": "README", "content": "",}],
    }
    commit = project.commits.create(data)
    assert commit.short_id == "ed899a2f"
    assert commit.title == data["commit_message"]


def test_revert_commit(project, resp_commit):
    commit = project.commits.get("6b2257ea", lazy=True)
    revert_commit = commit.revert(branch="master")
    assert revert_commit["short_id"] == "8b090c1b"
    assert revert_commit["title"] == 'Revert "Initial commit"'


def test_get_commit_gpg_signature(project, resp_get_commit_gpg_signature):
    commit = project.commits.get("6b2257ea", lazy=True)
    signature = commit.signature()
    assert signature["gpg_key_primary_keyid"] == "8254AAB3FBD54AC9"
    assert signature["verification_status"] == "verified"