1
1
import django_dynamic_fixture as fixture
2
2
import requests_mock
3
3
from allauth .socialaccount .models import SocialAccount , SocialToken
4
- from allauth .socialaccount .providers .github .views import GitHubOAuth2Adapter
4
+ from allauth .socialaccount .providers .github .provider import GitHubProvider
5
5
from django .contrib .auth .models import User
6
6
from django .test import TestCase
7
7
12
12
RemoteRepository ,
13
13
RemoteRepositoryRelation ,
14
14
)
15
+ from django_dynamic_fixture import get
16
+ from allauth .socialaccount .providers .gitlab .provider import GitLabProvider
15
17
from readthedocs .oauth .services import GitHubService
16
18
from readthedocs .projects .models import Project
17
19
@@ -64,7 +66,7 @@ def setUp(self):
64
66
self .socialaccount = fixture .get (
65
67
SocialAccount ,
66
68
user = self .user ,
67
- provider = GitHubOAuth2Adapter . provider_id ,
69
+ provider = GitHubProvider . id ,
68
70
)
69
71
self .token = fixture .get (
70
72
SocialToken ,
@@ -122,6 +124,25 @@ def test_sync_delete_stale(self, mock_request):
122
124
account = self .socialaccount ,
123
125
)
124
126
127
+ # Project from another provider (GitLab) that has the same ID should not be removed.
128
+ gitlab_socialaccount = fixture .get (
129
+ SocialAccount ,
130
+ user = self .user ,
131
+ provider = GitLabProvider .id ,
132
+ )
133
+ gitlab_repo = fixture .get (
134
+ RemoteRepository ,
135
+ full_name = "user/repo" ,
136
+ remote_id = repo_3 .remote_id ,
137
+ vcs_provider = GitLabProvider .id ,
138
+ )
139
+ fixture .get (
140
+ RemoteRepositoryRelation ,
141
+ remote_repository = gitlab_repo ,
142
+ user = self .user ,
143
+ account = gitlab_socialaccount ,
144
+ )
145
+
125
146
org = fixture .get (
126
147
RemoteOrganization ,
127
148
name = "organization" ,
@@ -133,18 +154,21 @@ def test_sync_delete_stale(self, mock_request):
133
154
account = self .socialaccount ,
134
155
)
135
156
136
- self .assertEqual (RemoteRepository .objects .count (), 3 )
137
- self .assertEqual (RemoteRepositoryRelation .objects .count (), 3 )
157
+ self .assertEqual (RemoteRepository .objects .count (), 4 )
158
+ self .assertEqual (RemoteRepositoryRelation .objects .count (), 4 )
138
159
self .assertEqual (RemoteOrganization .objects .count (), 1 )
139
160
self .assertEqual (RemoteOrganizationRelation .objects .count (), 1 )
140
161
162
+ assert self .socialaccount .remote_repository_relations .count () == 3
163
+ assert self .socialaccount .remote_organization_relations .count () == 1
164
+
141
165
self .service .sync ()
142
166
143
167
# After calling .sync, old-repository remote relation should be deleted,
144
168
# project-linked-repository remote relation should be conserved,
145
169
# and only the one's returned by the API should be present (organization/repository)
146
- self .assertEqual (RemoteRepository .objects .count (), 3 )
147
- self .assertEqual (RemoteRepositoryRelation .objects .count (), 1 )
170
+ self .assertEqual (RemoteRepository .objects .count (), 4 )
171
+ self .assertEqual (RemoteRepositoryRelation .objects .count (), 2 )
148
172
self .assertTrue (
149
173
RemoteRepository .objects .filter (
150
174
full_name = "organization/repository"
@@ -158,6 +182,9 @@ def test_sync_delete_stale(self, mock_request):
158
182
self .assertEqual (RemoteOrganization .objects .count (), 1 )
159
183
self .assertEqual (RemoteOrganizationRelation .objects .count (), 0 )
160
184
185
+ assert self .socialaccount .remote_repository_relations .count () == 1
186
+ assert self .socialaccount .remote_organization_relations .count () == 0
187
+
161
188
@requests_mock .Mocker (kw = "mock_request" )
162
189
def test_sync_repositories (self , mock_request ):
163
190
mock_request .get (
@@ -284,7 +311,7 @@ def test_sync_repositories_only_creates_one_remote_repo_per_vcs_repo(
284
311
user_2_socialaccount = fixture .get (
285
312
SocialAccount ,
286
313
user = user_2 ,
287
- provider = GitHubOAuth2Adapter . provider_id ,
314
+ provider = GitHubProvider . id ,
288
315
)
289
316
fixture .get (
290
317
SocialToken ,
0 commit comments