|
| 1 | +from httmock import urlmatch, response, with_httmock |
| 2 | + |
| 3 | +from .test_projects import headers, TestProject |
| 4 | + |
| 5 | + |
| 6 | +@urlmatch( |
| 7 | + scheme="http", |
| 8 | + netloc="localhost", |
| 9 | + path="/api/v4/projects/1/repository/commits/6b2257ea", |
| 10 | + method="get", |
| 11 | +) |
| 12 | +def resp_get_commit(url, request): |
| 13 | + """Mock for commit GET response.""" |
| 14 | + content = """{ |
| 15 | + "id": "6b2257eabcec3db1f59dafbd84935e3caea04235", |
| 16 | + "short_id": "6b2257ea", |
| 17 | + "title": "Initial commit" |
| 18 | + }""" |
| 19 | + content = content.encode("utf-8") |
| 20 | + return response(200, content, headers, None, 5, request) |
| 21 | + |
| 22 | + |
| 23 | +@urlmatch( |
| 24 | + scheme="http", path="/api/v4/projects/1/repository/commits", method="post", |
| 25 | +) |
| 26 | +def resp_create_commit(url, request): |
| 27 | + """Mock for commit create POST response.""" |
| 28 | + content = """{ |
| 29 | + "id": "ed899a2f4b50b4370feeea94676502b42383c746", |
| 30 | + "short_id": "ed899a2f", |
| 31 | + "title": "Commit message" |
| 32 | + }""" |
| 33 | + content = content.encode("utf-8") |
| 34 | + return response(200, content, headers, None, 5, request) |
| 35 | + |
| 36 | + |
| 37 | +@urlmatch( |
| 38 | + scheme="http", path="/api/v4/projects/1/repository/commits/6b2257ea", method="post", |
| 39 | +) |
| 40 | +def resp_revert_commit(url, request): |
| 41 | + """Mock for commit revert POST response.""" |
| 42 | + content = """{ |
| 43 | + "id": "8b090c1b79a14f2bd9e8a738f717824ff53aebad", |
| 44 | + "short_id": "8b090c1b", |
| 45 | + "title":"Revert \\"Initial commit\\"" |
| 46 | + }""" |
| 47 | + content = content.encode("utf-8") |
| 48 | + return response(200, content, headers, None, 5, request) |
| 49 | + |
| 50 | + |
| 51 | +@urlmatch( |
| 52 | + scheme="http", |
| 53 | + netloc="localhost", |
| 54 | + path="/api/v4/projects/1/repository/commits/6b2257ea/signature", |
| 55 | + method="get", |
| 56 | +) |
| 57 | +def resp_get_commit_gpg_signature(url, request): |
| 58 | + """Mock for commit GPG signature GET response.""" |
| 59 | + content = """{ |
| 60 | + "gpg_key_id": 1, |
| 61 | + "gpg_key_primary_keyid": "8254AAB3FBD54AC9", |
| 62 | + "gpg_key_user_name": "John Doe", |
| 63 | + "gpg_key_user_email": "johndoe@example.com", |
| 64 | + "verification_status": "verified", |
| 65 | + "gpg_key_subkey_id": null |
| 66 | + }""" |
| 67 | + content = content.encode("utf-8") |
| 68 | + return response(200, content, headers, None, 5, request) |
| 69 | + |
| 70 | + |
| 71 | +class TestCommit(TestProject): |
| 72 | + """ |
| 73 | + Base class for commit tests. Inherits from TestProject, |
| 74 | + since currently all commit methods are under projects. |
| 75 | + """ |
| 76 | + |
| 77 | + @with_httmock(resp_get_commit) |
| 78 | + def test_get_commit(self): |
| 79 | + commit = self.project.commits.get("6b2257ea") |
| 80 | + self.assertEqual(commit.short_id, "6b2257ea") |
| 81 | + self.assertEqual(commit.title, "Initial commit") |
| 82 | + |
| 83 | + @with_httmock(resp_create_commit) |
| 84 | + def test_create_commit(self): |
| 85 | + data = { |
| 86 | + "branch": "master", |
| 87 | + "commit_message": "Commit message", |
| 88 | + "actions": [{"action": "create", "file_path": "README", "content": "",}], |
| 89 | + } |
| 90 | + commit = self.project.commits.create(data) |
| 91 | + self.assertEqual(commit.short_id, "ed899a2f") |
| 92 | + self.assertEqual(commit.title, data["commit_message"]) |
| 93 | + |
| 94 | + @with_httmock(resp_revert_commit) |
| 95 | + def test_revert_commit(self): |
| 96 | + commit = self.project.commits.get("6b2257ea", lazy=True) |
| 97 | + revert_commit = commit.revert(branch="master") |
| 98 | + self.assertEqual(revert_commit["short_id"], "8b090c1b") |
| 99 | + self.assertEqual(revert_commit["title"], 'Revert "Initial commit"') |
| 100 | + |
| 101 | + @with_httmock(resp_get_commit_gpg_signature) |
| 102 | + def test_get_commit_gpg_signature(self): |
| 103 | + commit = self.project.commits.get("6b2257ea", lazy=True) |
| 104 | + signature = commit.signature() |
| 105 | + self.assertEqual(signature["gpg_key_primary_keyid"], "8254AAB3FBD54AC9") |
| 106 | + self.assertEqual(signature["verification_status"], "verified") |
0 commit comments