|
| 1 | +importunittest |
| 2 | +importgitlab |
| 3 | +importos |
| 4 | +importpickle |
| 5 | +importtempfile |
| 6 | +importjson |
| 7 | +importunittest |
| 8 | +importrequests |
| 9 | +fromgitlabimport*# noqa |
| 10 | +fromgitlab.v4.objectsimport*# noqa |
| 11 | +fromhttmockimportHTTMock,urlmatch,response# noqa |
| 12 | + |
| 13 | + |
| 14 | +headers= {"content-type":"application/json"} |
| 15 | + |
| 16 | + |
| 17 | +classTestProjectSnippets(unittest.TestCase): |
| 18 | +defsetUp(self): |
| 19 | +self.gl=Gitlab( |
| 20 | +"http://localhost", |
| 21 | +private_token="private_token", |
| 22 | +ssl_verify=True, |
| 23 | +api_version=4, |
| 24 | + ) |
| 25 | + |
| 26 | +deftest_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 | +defresp_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 | +returnresponse(200,content,headers,None,25,request) |
| 48 | + |
| 49 | +withHTTMock(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 | +deftest_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 | +defresp_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 | +returnresponse(200,content,headers,None,25,request) |
| 77 | + |
| 78 | +withHTTMock(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 | +deftest_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 | +defresp_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 | +returnresponse(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 | +defresp_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 | +returnresponse(200,content,headers,None,25,request) |
| 124 | + |
| 125 | +withHTTMock(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) |