|
2 | 2 | # SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | 4 | import datetime |
5 | | -import responses |
| 5 | + |
6 | 6 | import pytest |
7 | | -from typing import Any, Dict, List, Optional |
8 | | -import requests |
9 | 7 |
|
10 | | -from ogr.abstract import CommitStatus, CommitFlag |
| 8 | +from ogr.abstract import CommitFlag, CommitStatus |
11 | 9 | from ogr.services.forgejo.commit_flag import ForgejoCommitFlag |
12 | 10 |
|
| 11 | + |
| 12 | +def fake_get_commit_statuses(self, owner, repo, commit): |
| 13 | + return [ |
| 14 | + { |
| 15 | + "commit": commit, |
| 16 | + "state": "success", |
| 17 | + "context": "CI", |
| 18 | + "comment": "All tests passed", |
| 19 | + "id": "123", |
| 20 | + "url": f"http://dummy-forgejo/commit/{commit}/status", |
| 21 | + "created": datetime.datetime.strptime( |
| 22 | + "2023-01-01T12:00:00Z", |
| 23 | + "%Y-%m-%dT%H:%M:%SZ", |
| 24 | + ), |
| 25 | + "updated": datetime.datetime.strptime( |
| 26 | + "2023-01-01T12:30:00Z", |
| 27 | + "%Y-%m-%dT%H:%M:%SZ", |
| 28 | + ), |
| 29 | + }, |
| 30 | + ] |
| 31 | + |
| 32 | + |
| 33 | +def fake_set_commit_status( |
| 34 | + self, |
| 35 | + owner, |
| 36 | + repo, |
| 37 | + commit, |
| 38 | + state, |
| 39 | + target_url, |
| 40 | + description, |
| 41 | + context, |
| 42 | +): |
| 43 | + return { |
| 44 | + "commit": commit, |
| 45 | + "state": state, |
| 46 | + "context": context, |
| 47 | + "comment": description, |
| 48 | + "id": "456", |
| 49 | + "url": f"http://dummy-forgejo/commit/{commit}/status", |
| 50 | + "created": datetime.datetime.strptime( |
| 51 | + "2023-02-01T12:00:00Z", |
| 52 | + "%Y-%m-%dT%H:%M:%SZ", |
| 53 | + ), |
| 54 | + "updated": datetime.datetime.strptime( |
| 55 | + "2023-02-01T12:30:00Z", |
| 56 | + "%Y-%m-%dT%H:%M:%SZ", |
| 57 | + ), |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | +class FakePyforgejoApi: |
| 62 | + def __init__(self, api_url, token): |
| 63 | + self.api_url = api_url |
| 64 | + self.token = token |
| 65 | + |
| 66 | + def get_commit_statuses(self, owner, repo, commit): |
| 67 | + return fake_get_commit_statuses(self, owner, repo, commit) |
| 68 | + |
| 69 | + def set_commit_status( |
| 70 | + self, |
| 71 | + owner, |
| 72 | + repo, |
| 73 | + commit, |
| 74 | + state, |
| 75 | + target_url, |
| 76 | + description, |
| 77 | + context, |
| 78 | + ): |
| 79 | + return fake_set_commit_status( |
| 80 | + self, |
| 81 | + owner, |
| 82 | + repo, |
| 83 | + commit, |
| 84 | + state, |
| 85 | + target_url, |
| 86 | + description, |
| 87 | + context, |
| 88 | + ) |
| 89 | + |
| 90 | + |
13 | 91 | class MockProject: |
14 | 92 | forge_api_url = "http://dummy-forgejo/api/v1" |
15 | 93 | owner = "dummy_owner" |
16 | 94 | repo = "dummy_repo" |
| 95 | + token = "dummy_token" |
17 | 96 |
|
18 | | - def get_auth_header(self) -> Dict[str, str]: |
| 97 | + def get_auth_header(self) -> dict[str, str]: |
19 | 98 | return {"Authorization": "Bearer dummy_token"} |
20 | 99 |
|
21 | | -@responses.activate |
| 100 | + |
| 101 | +@pytest.fixture(autouse=True) |
| 102 | +def patch_pyforgejo_api(monkeypatch): |
| 103 | + monkeypatch.setattr( |
| 104 | + "ogr.services.forgejo.commit_flag.PyforgejoApi", |
| 105 | + FakePyforgejoApi, |
| 106 | + ) |
| 107 | + |
| 108 | + |
22 | 109 | def test_get_commit_flag_integration(): |
23 | 110 | project = MockProject() |
24 | 111 | commit = "abcdef123456" |
25 | | - url = f"{project.forge_api_url}/repos/{project.owner}/{project.repo}/commits/{commit}/statuses" |
26 | | - |
27 | | - # Dummy response data simulating Forgejo API output. |
28 | | - dummy_response = [{ |
29 | | - "commit": commit, |
30 | | - "state": "success", |
31 | | - "context": "CI", |
32 | | - "comment": "All tests passed", |
33 | | - "id": "123", |
34 | | - "url": "http://dummy-forgejo/commit/abcdef123456/status", |
35 | | - "created": "2023-01-01T12:00:00Z", |
36 | | - "updated": "2023-01-01T12:30:00Z" |
37 | | - }] |
38 | | - responses.add(responses.GET, url, json=dummy_response, status=200) |
39 | | - |
40 | | - # Call the method under test. |
41 | | - flags: List[CommitFlag] = ForgejoCommitFlag.get(project, commit) |
42 | | - |
43 | | - # Assertions using CommitStatus from packit.ogr.abstract. |
| 112 | + |
| 113 | + flags: list[CommitFlag] = ForgejoCommitFlag.get(project, commit) |
| 114 | + |
44 | 115 | assert len(flags) == 1 |
45 | 116 | flag = flags[0] |
46 | 117 | assert flag.commit == commit |
47 | 118 | assert flag.state == CommitStatus.success |
48 | 119 | assert flag.context == "CI" |
49 | 120 | assert flag.comment == "All tests passed" |
50 | 121 | assert flag.uid == "123" |
51 | | - |
52 | | - expected_created = datetime.datetime.strptime("2023-01-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ") |
53 | | - expected_updated = datetime.datetime.strptime("2023-01-01T12:30:00Z", "%Y-%m-%dT%H:%M:%SZ") |
| 122 | + |
| 123 | + expected_created = datetime.datetime.strptime( |
| 124 | + "2023-01-01T12:00:00Z", |
| 125 | + "%Y-%m-%dT%H:%M:%SZ", |
| 126 | + ) |
| 127 | + expected_updated = datetime.datetime.strptime( |
| 128 | + "2023-01-01T12:30:00Z", |
| 129 | + "%Y-%m-%dT%H:%M:%SZ", |
| 130 | + ) |
54 | 131 | assert flag.created == expected_created |
55 | 132 | assert flag.edited == expected_updated |
56 | 133 |
|
57 | | -@responses.activate |
| 134 | + |
58 | 135 | def test_set_commit_flag_integration(): |
59 | 136 | project = MockProject() |
60 | 137 | commit = "abcdef123456" |
61 | | - url = f"{project.forge_api_url}/repos/{project.owner}/{project.repo}/commits/{commit}/statuses" |
62 | | - |
63 | | - # Dummy response for setting a commit status. |
64 | | - dummy_response = { |
65 | | - "commit": commit, |
66 | | - "state": "success", |
67 | | - "context": "CI", |
68 | | - "comment": "Build succeeded", |
69 | | - "id": "456", |
70 | | - "url": "http://dummy-forgejo/commit/abcdef123456/status", |
71 | | - "created": "2023-02-01T12:00:00Z", |
72 | | - "updated": "2023-02-01T12:30:00Z" |
73 | | - } |
74 | | - responses.add(responses.POST, url, json=dummy_response, status=200) |
75 | | - |
76 | | - # Call the set method to create a new commit flag. |
| 138 | + |
77 | 139 | flag = ForgejoCommitFlag.set( |
78 | 140 | project=project, |
79 | 141 | commit=commit, |
80 | 142 | state=CommitStatus.success, |
81 | 143 | target_url="http://dummy-target", |
82 | 144 | description="Build succeeded", |
83 | | - context="CI" |
| 145 | + context="CI", |
84 | 146 | ) |
85 | | - |
86 | | - # Assertions to verify correct mapping using CommitStatus. |
| 147 | + |
87 | 148 | assert flag.commit == commit |
88 | 149 | assert flag.state == CommitStatus.success |
89 | 150 | assert flag.context == "CI" |
90 | 151 | assert flag.comment == "Build succeeded" |
91 | 152 | assert flag.uid == "456" |
92 | | - |
93 | | - expected_created = datetime.datetime.strptime("2023-02-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ") |
94 | | - expected_updated = datetime.datetime.strptime("2023-02-01T12:30:00Z", "%Y-%m-%dT%H:%M:%SZ") |
| 153 | + |
| 154 | + expected_created = datetime.datetime.strptime( |
| 155 | + "2023-02-01T12:00:00Z", |
| 156 | + "%Y-%m-%dT%H:%M:%SZ", |
| 157 | + ) |
| 158 | + expected_updated = datetime.datetime.strptime( |
| 159 | + "2023-02-01T12:30:00Z", |
| 160 | + "%Y-%m-%dT%H:%M:%SZ", |
| 161 | + ) |
95 | 162 | assert flag.created == expected_created |
96 | 163 | assert flag.edited == expected_updated |
0 commit comments