|
| 1 | +import pytest |
| 2 | +import respx |
| 3 | +from httpx.status_codes import StatusCode |
| 4 | + |
| 5 | +from gitlab import Gitlab |
| 6 | + |
| 7 | + |
| 8 | +class TestProjectSnippets: |
| 9 | + @pytest.fixture |
| 10 | + def gl(self): |
| 11 | + return Gitlab( |
| 12 | + "http://localhost", |
| 13 | + private_token="private_token", |
| 14 | + ssl_verify=True, |
| 15 | + api_version=4, |
| 16 | + ) |
| 17 | + |
| 18 | + @respx.mock |
| 19 | + @pytest.mark.asyncio |
| 20 | + async def test_list_project_snippets(self, gl): |
| 21 | + title = "Example Snippet Title" |
| 22 | + visibility = "private" |
| 23 | + request = respx.get( |
| 24 | + "http://localhost/api/v4/projects/1/snippets", |
| 25 | + content=[ |
| 26 | + { |
| 27 | + "title": title, |
| 28 | + "description": "More verbose snippet description", |
| 29 | + "file_name": "example.txt", |
| 30 | + "content": "source code with multiple lines", |
| 31 | + "visibility": visibility, |
| 32 | + } |
| 33 | + ], |
| 34 | + status_code=StatusCode.OK, |
| 35 | + ) |
| 36 | + |
| 37 | + project = await gl.projects.get(1, lazy=True) |
| 38 | + snippets = await project.snippets.list() |
| 39 | + assert len(snippets) == 1 |
| 40 | + assert snippets[0].title == title |
| 41 | + assert snippets[0].visibility == visibility |
| 42 | + |
| 43 | + @respx.mock |
| 44 | + @pytest.mark.asyncio |
| 45 | + async def test_get_project_snippet(self, gl): |
| 46 | + title = "Example Snippet Title" |
| 47 | + visibility = "private" |
| 48 | + request = respx.get( |
| 49 | + "http://localhost/api/v4/projects/1/snippets/1", |
| 50 | + content={ |
| 51 | + "title": title, |
| 52 | + "description": "More verbose snippet description", |
| 53 | + "file_name": "example.txt", |
| 54 | + "content": "source code with multiple lines", |
| 55 | + "visibility": visibility, |
| 56 | + }, |
| 57 | + status_code=StatusCode.OK, |
| 58 | + ) |
| 59 | + |
| 60 | + project = await gl.projects.get(1, lazy=True) |
| 61 | + snippet = await project.snippets.get(1) |
| 62 | + assert snippet.title == title |
| 63 | + assert snippet.visibility == visibility |
| 64 | + |
| 65 | + @respx.mock |
| 66 | + @pytest.mark.asyncio |
| 67 | + async def test_create_update_project_snippets(self, gl): |
| 68 | + title = "Example Snippet Title" |
| 69 | + new_title = "new-title" |
| 70 | + visibility = "private" |
| 71 | + request_update = respx.put( |
| 72 | + "http://localhost/api/v4/projects/1/snippets", |
| 73 | + content={ |
| 74 | + "title": new_title, |
| 75 | + "description": "More verbose snippet description", |
| 76 | + "file_name": "example.txt", |
| 77 | + "content": "source code with multiple lines", |
| 78 | + "visibility": visibility, |
| 79 | + }, |
| 80 | + status_code=StatusCode.OK, |
| 81 | + ) |
| 82 | + |
| 83 | + request_create = respx.post( |
| 84 | + "http://localhost/api/v4/projects/1/snippets", |
| 85 | + content={ |
| 86 | + "title": title, |
| 87 | + "description": "More verbose snippet description", |
| 88 | + "file_name": "example.txt", |
| 89 | + "content": "source code with multiple lines", |
| 90 | + "visibility": visibility, |
| 91 | + }, |
| 92 | + status_code=StatusCode.OK, |
| 93 | + ) |
| 94 | + |
| 95 | + project = await gl.projects.get(1, lazy=True) |
| 96 | + snippet = await project.snippets.create( |
| 97 | + { |
| 98 | + "title": title, |
| 99 | + "file_name": title, |
| 100 | + "content": title, |
| 101 | + "visibility": visibility, |
| 102 | + } |
| 103 | + ) |
| 104 | + assert snippet.title == title |
| 105 | + assert snippet.visibility == visibility |
| 106 | + |
| 107 | + snippet.title = new_title |
| 108 | + await snippet.save() |
| 109 | + assert snippet.title == new_title |
| 110 | + assert snippet.visibility == visibility |
0 commit comments