|
| 1 | +import unittest |
| 2 | +import gitlab |
| 3 | +import os |
| 4 | +import pickle |
| 5 | +import tempfile |
| 6 | +import json |
| 7 | +import unittest |
| 8 | +import requests |
| 9 | +from gitlab import * # noqa |
| 10 | +from gitlab.v4.objects import * # noqa |
| 11 | +from httmock import HTTMock, urlmatch, response # noqa |
| 12 | + |
| 13 | + |
| 14 | +headers = {"content-type": "application/json"} |
| 15 | + |
| 16 | + |
| 17 | +class TestProjectSnippets(unittest.TestCase): |
| 18 | + def setUp(self): |
| 19 | + self.gl = Gitlab( |
| 20 | + "http://localhost", |
| 21 | + private_token="private_token", |
| 22 | + ssl_verify=True, |
| 23 | + api_version=4, |
| 24 | + ) |
| 25 | + |
| 26 | + def test_list_project_snippets(self): |
| 27 | + title = "Example Snippet Title" |
| 28 | + visibility = "private" |
| 29 | + |
| 30 | + @urlmatch( |
| 31 | + scheme="http", |
| 32 | + netloc="localhost", |
| 33 | + path="/api/v4/projects/1/snippets", |
| 34 | + method="get", |
| 35 | + ) |
| 36 | + def resp_list_snippet(url, request): |
| 37 | + content = """[{ |
| 38 | + "title": "%s", |
| 39 | + "description": "More verbose snippet description", |
| 40 | + "file_name": "example.txt", |
| 41 | + "content": "source code with multiple lines", |
| 42 | + "visibility": "%s"}]""" % ( |
| 43 | + title, |
| 44 | + visibility, |
| 45 | + ) |
| 46 | + content = content.encode("utf-8") |
| 47 | + return response(200, content, headers, None, 25, request) |
| 48 | + |
| 49 | + with HTTMock(resp_list_snippet): |
| 50 | + snippets = self.gl.projects.get(1, lazy=True).snippets.list() |
| 51 | + self.assertEqual(len(snippets), 1) |
| 52 | + self.assertEqual(snippets[0].title, title) |
| 53 | + self.assertEqual(snippets[0].visibility, visibility) |
| 54 | + |
| 55 | + def test_get_project_snippets(self): |
| 56 | + title = "Example Snippet Title" |
| 57 | + visibility = "private" |
| 58 | + |
| 59 | + @urlmatch( |
| 60 | + scheme="http", |
| 61 | + netloc="localhost", |
| 62 | + path="/api/v4/projects/1/snippets/1", |
| 63 | + method="get", |
| 64 | + ) |
| 65 | + def resp_get_snippet(url, request): |
| 66 | + content = """{ |
| 67 | + "title": "%s", |
| 68 | + "description": "More verbose snippet description", |
| 69 | + "file_name": "example.txt", |
| 70 | + "content": "source code with multiple lines", |
| 71 | + "visibility": "%s"}""" % ( |
| 72 | + title, |
| 73 | + visibility, |
| 74 | + ) |
| 75 | + content = content.encode("utf-8") |
| 76 | + return response(200, content, headers, None, 25, request) |
| 77 | + |
| 78 | + with HTTMock(resp_get_snippet): |
| 79 | + snippet = self.gl.projects.get(1, lazy=True).snippets.get(1) |
| 80 | + self.assertEqual(snippet.title, title) |
| 81 | + self.assertEqual(snippet.visibility, visibility) |
| 82 | + |
| 83 | + def test_create_update_project_snippets(self): |
| 84 | + title = "Example Snippet Title" |
| 85 | + visibility = "private" |
| 86 | + |
| 87 | + @urlmatch( |
| 88 | + scheme="http", |
| 89 | + netloc="localhost", |
| 90 | + path="/api/v4/projects/1/snippets", |
| 91 | + method="put", |
| 92 | + ) |
| 93 | + def resp_update_snippet(url, request): |
| 94 | + content = """{ |
| 95 | + "title": "%s", |
| 96 | + "description": "More verbose snippet description", |
| 97 | + "file_name": "example.txt", |
| 98 | + "content": "source code with multiple lines", |
| 99 | + "visibility": "%s"}""" % ( |
| 100 | + title, |
| 101 | + visibility, |
| 102 | + ) |
| 103 | + content = content.encode("utf-8") |
| 104 | + return response(200, content, headers, None, 25, request) |
| 105 | + |
| 106 | + @urlmatch( |
| 107 | + scheme="http", |
| 108 | + netloc="localhost", |
| 109 | + path="/api/v4/projects/1/snippets", |
| 110 | + method="post", |
| 111 | + ) |
| 112 | + def resp_create_snippet(url, request): |
| 113 | + content = """{ |
| 114 | + "title": "%s", |
| 115 | + "description": "More verbose snippet description", |
| 116 | + "file_name": "example.txt", |
| 117 | + "content": "source code with multiple lines", |
| 118 | + "visibility": "%s"}""" % ( |
| 119 | + title, |
| 120 | + visibility, |
| 121 | + ) |
| 122 | + content = content.encode("utf-8") |
| 123 | + return response(200, content, headers, None, 25, request) |
| 124 | + |
| 125 | + with HTTMock(resp_create_snippet, resp_update_snippet): |
| 126 | + snippet = self.gl.projects.get(1, lazy=True).snippets.create( |
| 127 | + { |
| 128 | + "title": title, |
| 129 | + "file_name": title, |
| 130 | + "content": title, |
| 131 | + "visibility": visibility, |
| 132 | + } |
| 133 | + ) |
| 134 | + self.assertEqual(snippet.title, title) |
| 135 | + self.assertEqual(snippet.visibility, visibility) |
| 136 | + title = "new-title" |
| 137 | + snippet.title = title |
| 138 | + snippet.save() |
| 139 | + self.assertEqual(snippet.title, title) |
| 140 | + self.assertEqual(snippet.visibility, visibility) |
0 commit comments