|
| 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 | +class TestCommit(TestProject): |
| 52 | + """ |
| 53 | + Base class for commit tests. Inherits from TestProject, |
| 54 | + since currently all commit methods are under projects. |
| 55 | + """ |
| 56 | + |
| 57 | + @with_httmock(resp_get_commit) |
| 58 | + def test_get_commit(self): |
| 59 | + commit = self.project.commits.get("6b2257ea") |
| 60 | + self.assertEqual(commit.short_id, "6b2257ea") |
| 61 | + self.assertEqual(commit.title, "Initial commit") |
| 62 | + |
| 63 | + @with_httmock(resp_create_commit) |
| 64 | + def test_create_commit(self): |
| 65 | + data = { |
| 66 | + "branch": "master", |
| 67 | + "commit_message": "Commit message", |
| 68 | + "actions": [{"action": "create", "file_path": "README", "content": "",}], |
| 69 | + } |
| 70 | + commit = self.project.commits.create(data) |
| 71 | + self.assertEqual(commit.short_id, "ed899a2f") |
| 72 | + self.assertEqual(commit.title, data["commit_message"]) |
| 73 | + |
| 74 | + @with_httmock(resp_revert_commit) |
| 75 | + def test_revert_commit(self): |
| 76 | + commit = self.project.commits.get("6b2257ea", lazy=True) |
| 77 | + revert_commit = commit.revert(branch="master") |
| 78 | + self.assertEqual(revert_commit["short_id"], "8b090c1b") |
| 79 | + self.assertEqual(revert_commit["title"], 'Revert "Initial commit"') |
0 commit comments