31
31
32
32
class GitlabTest (unittest .TestCase ):
33
33
def setUp (self ):
34
+ # set up the connection
34
35
self .git = gitlab .Gitlab (host = host )
35
36
36
- def test_login (self ):
37
- """
38
- Test to see if login works with proper credentials
39
- """
40
- self .assertTrue (self .git .login (user = user , password = password ))
41
-
42
- def test_badlogin (self ):
43
- """
44
- Test to see if login fails with no credentials
45
- """
37
+ # test the failure first
46
38
self .failUnlessRaises (gitlab .exceptions .HttpError , self .git .login , user = "caca" , password = "caca" )
47
39
40
+ # now test login and leave it set up for the rest of the tests
41
+ self .assertTrue (self .git .login (user , password ))
42
+
43
+ # create a project to use on the testing
44
+ self .project = self .git .createproject ("Pyapi-gitlab" )
45
+ self .project_id = self .project ['id' ]
46
+ assert isinstance (self .project , dict )
47
+
48
+ # create a user to use in the testing
49
+ self .user = self .git .createuser ("caca" , "test" , "testpass" , "test@test.com" )
50
+ self .user_id = self .user ['id' ]
51
+ assert isinstance (self .user , dict )
52
+
53
+ def tearDown (self ):
54
+ # remove projects and users
55
+ self .assertTrue (self .git .deleteuser (self .user_id ))
56
+ self .assertTrue (self .git .deleteproject (self .project_id ))
57
+
48
58
def test_getusers (self ):
49
- self .git .login (user = user , password = password )
50
59
# get all users
51
60
assert isinstance (self .git .getusers (), list ) # compatible with 2.6
52
61
self .assertTrue (self .git .getusers ())
62
+
53
63
# get X pages
54
64
assert isinstance (self .git .getusers (page = 2 ), list ) # compatible with 2.6
55
65
assert isinstance (self .git .getusers (per_page = 4 ), list ) # compatible with 2.6
56
66
self .assertEqual (self .git .getusers (page = 800 ), list ("" )) # check against empty list
57
67
self .assertTrue (self .git .getusers (per_page = 43 )) # check against false
58
68
59
69
def test_currentuser (self ):
60
- self .git .login (user = user , password = password )
61
70
assert isinstance (self .git .currentuser (), dict ) # compatible with 2.6
62
71
self .assertTrue (self .git .currentuser ())
63
72
64
- def test_addremoveusers (self ):
65
- self .git .login (user = user , password = password )
66
- newuser = self .git .createuser ("dfgfegre" , "erreret" , "123456" ,
67
- "retetert@6yhyhyth5.com" , "skype" ,
68
- "linkedin" , "twitter" , "25" ,
69
- bio = "bio" )
70
- assert isinstance (newuser , dict )
71
- # this below doesn't really matter. Gilab always answers a 404
72
- #self.assertTrue(self.git.edituser(newuser['id'], twitter="tweeeeet", skype="Microsoft", username="Changed"))
73
- self .assertTrue (self .git .deleteuser (newuser ['id' ]))
74
-
75
- # def test_sshkeys(self):
76
- # self.git.login(user=user, password=password)
77
- # self.git.addsshkey(title="testkey", key=key)
78
- # assert isinstance(self.git.getsshkeys(), list) # compatible with 2.6
79
- # pass the id of the first key
80
- # assert isinstance(self.git.getsshkey(id_=self.git.getsshkeys()[0]['id']), dict) # compatible with 2.6
81
- # self.assertTrue(self.git.getsshkey(id_=self.git.getsshkeys()[0]['id']))
82
- # self.assertTrue(self.git.deletesshkey(id_=self.git.getsshkeys()[0]['id']))
83
- # self.assertTrue(self.git.addsshkey(title="test key", key=key))
84
- # self.assertTrue(self.git.deletesshkey(id_=self.git.getsshkeys()[0]['id']))
85
- # self.assertTrue(self.git.addsshkeyuser(id_=self.git.currentuser()['id'], title="testkey", key=key))
86
- # self.assertTrue(self.git.deletesshkey(id_=self.git.getsshkeys()[0]['id']))
87
-
88
73
def test_project (self ):
89
- self .git .login (user = user , password = password )
90
- # we won't test the creation of the project as there is no way of deleting it trougth the api
91
- # so we would end with a million test projects. Next Gitlab version allows to delete projects
92
- #self.assertTrue(self.git.createproject("Test-pyapy-gitlab"))
74
+ # test project
93
75
assert isinstance (self .git .getprojects (), list )
94
76
assert isinstance (self .git .getprojects (page = 5 ), list )
95
77
assert isinstance (self .git .getprojects (per_page = 7 ), list )
96
- assert isinstance (self .git .getproject (self .git . getprojects ()[ 0 ][ 'id' ] ), dict )
78
+ assert isinstance (self .git .getproject (self .project_id ), dict )
97
79
self .assertFalse (self .git .getproject ("wrong" ))
98
- assert isinstance (self .git .getprojectevents (self .git .getprojects ()[0 ]['id' ]), list )
99
- assert isinstance (self .git .getprojectevents (self .git .getprojects ()[0 ]['id' ], page = 3 ), list )
100
- assert isinstance (self .git .getprojectevents (self .git .getprojects ()[0 ]['id' ], per_page = 4 ), list )
101
- self .assertTrue (self .git .addprojectmember (id_ = 2 , user_id = 3 , access_level = "reporter" , sudo = 1 ))
102
- assert isinstance (self .git .listprojectmembers (id_ = 2 ), list )
103
- self .assertTrue (self .git .editprojectmember (id_ = 2 , user_id = 3 , access_level = "master" , sudo = 2 ))
104
- self .assertTrue (self .git .deleteprojectmember (id_ = 2 , user_id = 3 ))
105
- self .assertTrue (self .git .addprojecthook (id_ = 2 , url = "http://test.com" ))
106
- assert isinstance (self .git .getprojecthooks (id_ = 2 ), list )
107
- assert isinstance (self .git .getprojecthook (id_ = 2 , hook_id = self .git .getprojecthooks (id_ = 2 )[0 ]['id' ]), dict )
108
- self .assertTrue (self .git .editprojecthook (id_ = 2 , hook_id = self .git .getprojecthooks (id_ = 2 )[0 ]['id' ],
109
- url = "http://anothest.com" ))
110
- self .assertTrue (self .git .deleteprojecthook (id_ = 2 , hook_id = self .git .getprojecthooks (id_ = 2 )[0 ]['id' ]))
80
+
81
+ # test events
82
+ assert isinstance (self .git .getprojectevents (self .project_id ), list )
83
+ assert isinstance (self .git .getprojectevents (self .project_id , page = 3 ), list )
84
+ assert isinstance (self .git .getprojectevents (self .project_id , per_page = 4 ), list )
85
+
86
+ # add-remove project members
87
+ self .assertTrue (self .git .addprojectmember (id_ = self .project_id , user_id = 1 , access_level = "reporter" , sudo = 1 ))
88
+ assert isinstance (self .git .listprojectmembers (id_ = self .project_id ), list )
89
+ self .assertTrue (self .git .editprojectmember (id_ = self .project_id , user_id = 1 , access_level = "master" , sudo = 2 ))
90
+ self .assertTrue (self .git .deleteprojectmember (id_ = self .project_id , user_id = 1 ))
91
+
92
+ # Hooks testing
93
+ self .assertTrue (self .git .addprojecthook (self .project_id , "http://web.com" ))
94
+ assert isinstance (self .git .getprojecthooks (self .project_id ), list )
95
+ assert isinstance (self .git .getprojecthook (self .project_id ,
96
+ self .git .getprojecthooks (self .project_id )[0 ]['id' ]), dict )
97
+ self .assertTrue (self .git .editprojecthook (self .project_id ,
98
+ self .git .getprojecthooks (self .project_id )[0 ]['id' ], "http://another.com" ))
99
+ self .assertTrue (self .git .deleteprojecthook (self .project_id ,
100
+ self .git .getprojecthooks (self .project_id )[0 ]['id' ]))
101
+
102
+ # Delete the created project
103
+ self .assertTrue (self .git .deleteproject (self .project_id ))
111
104
112
105
def test_branch (self ):
113
- self .git .login (user = user , password = password )
114
- assert isinstance (self .git .listbranches (id_ = 2 ), list )
115
- assert isinstance (self .git .listbranch (id_ = 2 , branch = "master" ), dict )
116
- self .assertTrue (self .git .protectbranch (id_ = 2 , branch = "master" ))
117
- self .assertTrue (self .git .unprotectbranch (id_ = 2 , branch = "master" ))
106
+ assert isinstance (self .git .listbranches (id_ = self .project_id ), list )
107
+ assert isinstance (self .git .listbranch (id_ = self .project_id , branch = "master" ), dict )
108
+ self .assertTrue (self .git .protectbranch (id_ = self .project_id , branch = "master" ))
109
+ self .assertTrue (self .git .unprotectbranch (id_ = self .project_id , branch = "master" ))
118
110
119
111
def test_deploykeys (self ):
120
- self .git .login (user = user , password = password )
121
112
self .assertTrue (self .git .adddeploykey (id_ = 2 , title = "test" , key = key ))
122
113
assert isinstance (self .git .listdeploykey (id_ = 2 , key_id = 110 ), dict )
123
114
assert isinstance (self .git .listdeploykeys (id_ = 2 ), list )
124
115
125
116
def test_snippets (self ):
126
- self .git .login (user = user , password = password )
127
117
self .assertTrue (self .git .createsnippet (1 , "test" , "test" , "codeee" ))
128
118
assert isinstance (self .git .getsnippets (1 ), list )
129
119
assert isinstance (self .git .getsnippet (1 , self .git .getsnippets (1 )[0 ]['id' ]), dict )
130
120
self .assertTrue (self .git .deletesnippet (1 , self .git .getsnippets (1 )[0 ]['id' ]))
131
121
132
122
def test_repositories (self ):
133
- self .git .login (user = user , password = password )
134
123
assert isinstance (self .git .getrepositories (2 ), list )
135
124
assert isinstance (self .git .getrepositorybranch (2 , "master" ), dict )
136
125
assert isinstance (self .git .protectrepositorybranch (2 , "master" ), dict )
@@ -145,18 +134,15 @@ def test_repositories(self):
145
134
assert isinstance (str (self .git .getrawblob (2 , self .git .listrepositorycommits (2 )[0 ]['id' ], "setup.py" )), str )
146
135
147
136
def test_search (self ):
148
- self .git .login (user = user , password = password )
149
137
assert isinstance (self .git .searchproject ("gitlab" ), list )
150
138
151
139
def test_filearchive (self ):
152
- self .git .login (user = user , password = password )
153
140
# test it works
154
141
self .assertTrue (self .git .getfilearchive (2 ))
155
142
# test for failure
156
143
self .failUnlessRaises (gitlab .exceptions .HttpError , self .git .getfilearchive , 999999 )
157
144
158
145
def test_group (self ):
159
- self .git .login (user = user , password = password )
160
146
self .assertTrue (self .git .creategroup ("test_group" , "test" ))
161
147
assert isinstance (self .git .getgroups (), list )
162
148
print self .git .getgroups ()
0 commit comments