13
13
from readthedocs .integrations .models import GitHubWebhook , GitLabWebhook
14
14
from readthedocs .oauth .constants import BITBUCKET , GITHUB , GITLAB
15
15
from readthedocs .oauth .models import RemoteOrganization , RemoteRepository
16
- from readthedocs .oauth .services import (
17
- BitbucketService ,
18
- GitHubService ,
19
- GitLabService ,
20
- )
16
+ from readthedocs .oauth .services import BitbucketService , GitHubService , GitLabService
21
17
from readthedocs .projects import constants
22
18
from readthedocs .projects .models import Project
23
19
@@ -29,8 +25,8 @@ class GitHubOAuthTests(TestCase):
29
25
def setUp (self ):
30
26
self .client .login (username = 'eric' , password = 'test' )
31
27
self .user = User .objects .get (pk = 1 )
32
- self .project = Project .objects .get (slug = ' pip' )
33
- self .org = RemoteOrganization .objects .create (slug = ' rtfd' )
28
+ self .project = Project .objects .get (slug = " pip" )
29
+ self .org = RemoteOrganization .objects .create (slug = " rtfd" , remote_id = 1234 )
34
30
self .privacy = settings .DEFAULT_PRIVACY_LEVEL
35
31
self .service = GitHubService (
36
32
user = self .user ,
@@ -55,9 +51,7 @@ def setUp(self):
55
51
"url" : "https://api.github.com/repos/test/Hello-World/hooks/12345678" ,
56
52
}
57
53
]
58
-
59
- def test_make_project_pass (self ):
60
- repo_json = {
54
+ self .repo_response_data = {
61
55
'name' : 'testrepo' ,
62
56
'full_name' : 'testuser/testrepo' ,
63
57
'id' : '12345678' ,
@@ -67,9 +61,45 @@ def test_make_project_pass(self):
67
61
'ssh_url' :
'ssh://[email protected] :testuser/testrepo.git' ,
68
62
'html_url' : 'https://github.com/testuser/testrepo' ,
69
63
'clone_url' : 'https://github.com/testuser/testrepo.git' ,
64
+ "owner" : {
65
+ "type" : "User" ,
66
+ "id" : 1234 ,
67
+ },
70
68
}
69
+
70
+ def test_create_remote_repository (self ):
71
71
repo = self .service .create_repository (
72
- repo_json , organization = self .org , privacy = self .privacy ,
72
+ self .repo_response_data ,
73
+ privacy = self .privacy ,
74
+ )
75
+ self .assertIsInstance (repo , RemoteRepository )
76
+ self .assertEqual (repo .name , "testrepo" )
77
+ self .assertEqual (repo .full_name , "testuser/testrepo" )
78
+ self .assertEqual (repo .remote_id , "12345678" )
79
+ self .assertEqual (repo .vcs_provider , GITHUB )
80
+ self .assertEqual (repo .description , "Test Repo" )
81
+ self .assertEqual (
82
+ repo .avatar_url ,
83
+ settings .OAUTH_AVATAR_USER_DEFAULT_URL ,
84
+ )
85
+ self .assertIn (self .user , repo .users .all ())
86
+ self .assertEqual (repo .organization , None )
87
+ self .assertEqual (
88
+ repo .clone_url ,
89
+ "https://github.com/testuser/testrepo.git" ,
90
+ )
91
+ self .assertEqual (
92
+ repo .ssh_url ,
93
+ "ssh://[email protected] :testuser/testrepo.git" ,
94
+ )
95
+ self .assertEqual (repo .html_url , "https://github.com/testuser/testrepo" )
96
+
97
+ def test_create_remote_repository_with_organization (self ):
98
+ self .repo_response_data ["owner" ]["type" ] = "Organization"
99
+ repo = self .service .create_repository (
100
+ self .repo_response_data ,
101
+ organization = self .org ,
102
+ privacy = self .privacy ,
73
103
)
74
104
self .assertIsInstance (repo , RemoteRepository )
75
105
self .assertEqual (repo .name , 'testrepo' )
@@ -91,23 +121,103 @@ def test_make_project_pass(self):
91
121
)
92
122
self .assertEqual (repo .html_url , 'https://github.com/testuser/testrepo' )
93
123
94
- def test_make_project_fail (self ):
95
- repo_json = {
96
- 'name' : '' ,
97
- 'full_name' : '' ,
98
- 'id' : '' ,
99
- 'description' : '' ,
100
- 'git_url' : '' ,
101
- 'private' : True ,
102
- 'ssh_url' : '' ,
103
- 'html_url' : '' ,
104
- 'clone_url' : '' ,
105
- }
124
+ def test_skip_creation_remote_repository_on_private_repos (self ):
125
+ self .repo_response_data ["private" ] = True
106
126
github_project = self .service .create_repository (
107
- repo_json , organization = self .org , privacy = self .privacy ,
127
+ self .repo_response_data ,
128
+ organization = self .org ,
129
+ privacy = self .privacy ,
108
130
)
109
131
self .assertIsNone (github_project )
110
132
133
+ def test_project_was_moved_from_a_personal_account_to_an_organization (self ):
134
+ github_project = self .service .create_repository (
135
+ self .repo_response_data ,
136
+ privacy = self .privacy ,
137
+ )
138
+ self .assertEqual (github_project .organization , None )
139
+
140
+ # Project belongs has been moved to an organization.
141
+ self .repo_response_data ["owner" ]["type" ] = "Organization"
142
+
143
+ self .service .create_repository (
144
+ self .repo_response_data ,
145
+ privacy = self .privacy ,
146
+ )
147
+ github_project .refresh_from_db ()
148
+ self .assertEqual (github_project .organization , None )
149
+
150
+ self .service .create_repository (
151
+ self .repo_response_data ,
152
+ organization = self .org ,
153
+ privacy = self .privacy ,
154
+ )
155
+ github_project .refresh_from_db ()
156
+ self .assertEqual (github_project .organization , self .org )
157
+
158
+ def test_project_was_moved_from_an_organization_to_a_personal_account (self ):
159
+ # Project belongs to an organization.
160
+ self .repo_response_data ["owner" ]["type" ] = "Organization"
161
+ github_project = self .service .create_repository (
162
+ self .repo_response_data ,
163
+ organization = self .org ,
164
+ privacy = self .privacy ,
165
+ )
166
+ self .assertEqual (github_project .organization , self .org )
167
+
168
+ # Project belongs has been moved to a personal account.
169
+ self .repo_response_data ["owner" ]["type" ] = "User"
170
+
171
+ # This operation doesn't have any effect.
172
+ self .service .create_repository (
173
+ self .repo_response_data ,
174
+ organization = self .org ,
175
+ privacy = self .privacy ,
176
+ )
177
+ github_project .refresh_from_db ()
178
+ self .assertEqual (github_project .organization , self .org )
179
+
180
+ self .service .create_repository (
181
+ self .repo_response_data ,
182
+ privacy = self .privacy ,
183
+ )
184
+ github_project .refresh_from_db ()
185
+ self .assertEqual (github_project .organization , None )
186
+
187
+ def test_project_was_moved_to_another_organization (self ):
188
+ another_remote_organization = RemoteOrganization .objects .create (
189
+ slug = "another" , remote_id = 4321
190
+ )
191
+
192
+ # Project belongs to an organization.
193
+ self .repo_response_data ["owner" ]["type" ] = "Organization"
194
+ github_project = self .service .create_repository (
195
+ self .repo_response_data ,
196
+ organization = self .org ,
197
+ privacy = self .privacy ,
198
+ )
199
+ self .assertEqual (github_project .organization , self .org )
200
+
201
+ # Project was moved to another organization.
202
+ self .repo_response_data ["owner" ]["id" ] = 4321
203
+
204
+ self .service .create_repository (
205
+ self .repo_response_data ,
206
+ organization = another_remote_organization ,
207
+ privacy = self .privacy ,
208
+ )
209
+ github_project .refresh_from_db ()
210
+ self .assertEqual (github_project .organization , another_remote_organization )
211
+
212
+ # The remote ID doesn't match, the project isn't moved to the organization.
213
+ self .service .create_repository (
214
+ self .repo_response_data ,
215
+ organization = self .org ,
216
+ privacy = self .privacy ,
217
+ )
218
+ github_project .refresh_from_db ()
219
+ self .assertEqual (github_project .organization , another_remote_organization )
220
+
111
221
def test_make_organization (self ):
112
222
org_json = {
113
223
'id' : 12345 ,
@@ -131,20 +241,11 @@ def test_import_with_no_token(self):
131
241
self .assertEqual (services , [])
132
242
133
243
def test_multiple_users_same_repo (self ):
134
- repo_json = {
135
- 'name' : '' ,
136
- 'full_name' : 'testrepo/multiple' ,
137
- 'id' : '12345678' ,
138
- 'description' : '' ,
139
- 'git_url' : '' ,
140
- 'private' : False ,
141
- 'ssh_url' : '' ,
142
- 'html_url' : '' ,
143
- 'clone_url' : '' ,
144
- }
145
-
244
+ self .repo_response_data ["owner" ]["type" ] = "Organization"
146
245
github_project = self .service .create_repository (
147
- repo_json , organization = self .org , privacy = self .privacy ,
246
+ self .repo_response_data ,
247
+ organization = self .org ,
248
+ privacy = self .privacy ,
148
249
)
149
250
150
251
user2 = User .objects .get (pk = 2 )
@@ -153,31 +254,43 @@ def test_multiple_users_same_repo(self):
153
254
account = get (SocialAccount , user = self .user )
154
255
)
155
256
github_project_2 = service .create_repository (
156
- repo_json , organization = self .org , privacy = self .privacy ,
257
+ self .repo_response_data ,
258
+ organization = self .org ,
259
+ privacy = self .privacy ,
157
260
)
158
261
self .assertIsInstance (github_project , RemoteRepository )
159
262
self .assertIsInstance (github_project_2 , RemoteRepository )
160
263
self .assertEqual (github_project_2 , github_project )
161
264
162
265
github_project_3 = self .service .create_repository (
163
- repo_json , organization = self .org , privacy = self .privacy ,
266
+ self .repo_response_data ,
267
+ organization = self .org ,
268
+ privacy = self .privacy ,
164
269
)
165
270
github_project_4 = service .create_repository (
166
- repo_json , organization = self .org , privacy = self .privacy ,
271
+ self .repo_response_data ,
272
+ organization = self .org ,
273
+ privacy = self .privacy ,
167
274
)
168
275
self .assertIsInstance (github_project_3 , RemoteRepository )
169
276
self .assertIsInstance (github_project_4 , RemoteRepository )
170
277
self .assertEqual (github_project , github_project_3 )
171
278
self .assertEqual (github_project_2 , github_project_4 )
172
279
173
280
github_project_5 = self .service .create_repository (
174
- repo_json , organization = self .org , privacy = self .privacy ,
281
+ self .repo_response_data ,
282
+ organization = self .org ,
283
+ privacy = self .privacy ,
175
284
)
176
285
github_project_6 = service .create_repository (
177
- repo_json , organization = self .org , privacy = self .privacy ,
286
+ self .repo_response_data ,
287
+ organization = self .org ,
288
+ privacy = self .privacy ,
178
289
)
179
290
291
+ self .assertIsNotNone (github_project )
180
292
self .assertEqual (github_project , github_project_5 )
293
+ self .assertIsNotNone (github_project_2 )
181
294
self .assertEqual (github_project_2 , github_project_6 )
182
295
183
296
@mock .patch ('readthedocs.oauth.services.github.log' )
@@ -237,15 +350,18 @@ def test_make_private_project(self):
237
350
Test ability to import ``public`` repositories under ``private`` level.
238
351
"""
239
352
repo_json = {
240
- 'name' : 'testrepo' ,
241
- 'full_name' : 'testuser/testrepo' ,
242
- 'id' : '12345678' ,
243
- 'description' : 'Test Repo' ,
244
- 'git_url' : 'git://github.com/testuser/testrepo.git' ,
245
- 'private' : False ,
246
- 'ssh_url' :
'ssh://[email protected] :testuser/testrepo.git' ,
247
- 'html_url' : 'https://github.com/testuser/testrepo' ,
248
- 'clone_url' : 'https://github.com/testuser/testrepo.git' ,
353
+ "name" : "testrepo" ,
354
+ "full_name" : "testuser/testrepo" ,
355
+ "id" : "12345678" ,
356
+ "description" : "Test Repo" ,
357
+ "git_url" : "git://github.com/testuser/testrepo.git" ,
358
+ "private" : False ,
359
+ "ssh_url" :
"ssh://[email protected] :testuser/testrepo.git" ,
360
+ "html_url" : "https://github.com/testuser/testrepo" ,
361
+ "clone_url" : "https://github.com/testuser/testrepo.git" ,
362
+ "owner" : {
363
+ "type" : "User" ,
364
+ },
249
365
}
250
366
repo = self .service .create_repository (repo_json , organization = self .org )
251
367
self .assertIsNotNone (repo )
0 commit comments