1
- use crate :: models:: { ApiToken , Email , NewUser , User } ;
1
+ use crate :: controllers:: session;
2
+ use crate :: models:: { ApiToken , Email , User } ;
3
+ use crate :: tests:: util:: github:: next_gh_id;
2
4
use crate :: tests:: util:: { MockCookieUser , RequestHelper } ;
3
- use crate :: tests:: { new_user , TestApp } ;
5
+ use crate :: tests:: TestApp ;
4
6
use crate :: util:: token:: HashedToken ;
7
+ use crates_io_github:: GithubUser ;
5
8
use diesel:: prelude:: * ;
6
9
use diesel_async:: RunQueryDsl ;
7
10
use http:: StatusCode ;
@@ -20,20 +23,20 @@ impl crate::tests::util::MockCookieUser {
20
23
#[ tokio:: test( flavor = "multi_thread" ) ]
21
24
async fn updating_existing_user_doesnt_change_api_token ( ) -> anyhow:: Result < ( ) > {
22
25
let ( app, _, user, token) = TestApp :: init ( ) . with_token ( ) . await ;
26
+ let emails = & app. as_inner ( ) . emails ;
23
27
let mut conn = app. db_conn ( ) . await ;
24
28
let gh_id = user. as_model ( ) . gh_id ;
25
29
let token = token. plaintext ( ) ;
26
30
27
31
// Reuse gh_id but use new gh_login and gh_access_token
28
- assert_ok ! (
29
- NewUser :: builder( )
30
- . gh_id( gh_id)
31
- . gh_login( "bar" )
32
- . gh_access_token( "bar_token" )
33
- . build( )
34
- . create_or_update( None , & app. as_inner( ) . emails, & mut conn)
35
- . await
36
- ) ;
32
+ let gh_user = GithubUser {
33
+ id : gh_id,
34
+ login : "bar" . to_string ( ) ,
35
+ name : None ,
36
+ email : None ,
37
+ avatar_url : None ,
38
+ } ;
39
+ assert_ok ! ( session:: save_user_to_database( & gh_user, "bar_token" , emails, & mut conn) . await ) ;
37
40
38
41
// Use the original API token to find the now updated user
39
42
let hashed_token = assert_ok ! ( HashedToken :: parse( token. expose_secret( ) ) ) ;
@@ -58,17 +61,26 @@ async fn updating_existing_user_doesnt_change_api_token() -> anyhow::Result<()>
58
61
#[ tokio:: test( flavor = "multi_thread" ) ]
59
62
async fn github_without_email_does_not_overwrite_email ( ) -> anyhow:: Result < ( ) > {
60
63
let ( app, _) = TestApp :: init ( ) . empty ( ) . await ;
64
+ let emails = & app. as_inner ( ) . emails ;
61
65
let mut conn = app. db_conn ( ) . await ;
62
66
63
67
// Simulate logging in via GitHub with an account that has no email.
68
+
64
69
// Because faking GitHub is terrible, call what GithubUser::save_to_database does directly.
65
70
// Don't use app.db_new_user because it adds a verified email.
66
- let u = new_user ( "arbitrary_username" )
67
- . create_or_update ( None , & app. as_inner ( ) . emails , & mut conn)
68
- . await ?;
71
+ let gh_id = next_gh_id ( ) ;
72
+ let gh_user = GithubUser {
73
+ id : gh_id,
74
+ login : "arbitrary_username" . to_string ( ) ,
75
+ name : None ,
76
+ email : None ,
77
+ avatar_url : None ,
78
+ } ;
79
+
80
+ let u =
81
+ session:: save_user_to_database ( & gh_user, "some random token" , emails, & mut conn) . await ?;
69
82
70
83
let user_without_github_email = MockCookieUser :: new ( & app, u) ;
71
- let user_without_github_email_model = user_without_github_email. as_model ( ) ;
72
84
73
85
let json = user_without_github_email. show_me ( ) . await ;
74
86
// Check that the setup is correct and the user indeed has no email
@@ -80,16 +92,18 @@ async fn github_without_email_does_not_overwrite_email() -> anyhow::Result<()> {
80
92
. await ;
81
93
82
94
// Simulate the same user logging in via GitHub again, still with no email in GitHub.
83
- let u = NewUser :: builder ( )
84
- // Use the same github ID to link to the existing account
85
- . gh_id ( user_without_github_email_model. gh_id )
86
- . gh_login ( "arbitrary_username" )
87
- . gh_access_token ( "some random token" )
88
- . build ( ) ;
89
95
90
- let u = u
91
- . create_or_update ( None , & app. as_inner ( ) . emails , & mut conn)
92
- . await ?;
96
+ let gh_user = GithubUser {
97
+ id : gh_id,
98
+ login : "arbitrary_username" . to_string ( ) ,
99
+ name : None ,
100
+ email : None ,
101
+ avatar_url : None ,
102
+ } ;
103
+
104
+ let u =
105
+ session:: save_user_to_database ( & gh_user, "some random token" , emails, & mut conn) . await ?;
106
+
93
107
let again_user_without_github_email = MockCookieUser :: new ( & app, u) ;
94
108
95
109
let json = again_user_without_github_email. show_me ( ) . await ;
@@ -120,16 +134,17 @@ async fn github_with_email_does_not_overwrite_email() -> anyhow::Result<()> {
120
134
121
135
let emails = app. as_inner ( ) . emails . clone ( ) ;
122
136
123
- let u = NewUser :: builder ( )
137
+ let gh_user = GithubUser {
124
138
// Use the same github ID to link to the existing account
125
- . gh_id ( model. gh_id )
126
- . gh_login ( "arbitrary_username" )
127
- . gh_access_token ( "some random token" )
128
- . build ( ) ;
139
+ id : model. gh_id ,
140
+ login : "arbitrary_username" . to_string ( ) ,
141
+ name : None ,
142
+ email : Some ( new_github_email. to_string ( ) ) ,
143
+ avatar_url : None ,
144
+ } ;
129
145
130
- let u = u
131
- . create_or_update ( Some ( new_github_email) , & emails, & mut conn)
132
- . await ?;
146
+ let u =
147
+ session:: save_user_to_database ( & gh_user, "some random token" , & emails, & mut conn) . await ?;
133
148
134
149
let user_with_different_email_in_github = MockCookieUser :: new ( & app, u) ;
135
150
@@ -175,10 +190,18 @@ async fn test_confirm_user_email() -> anyhow::Result<()> {
175
190
// email directly into the database and we want to test the verification flow here.
176
191
let email = "potato2@example.com" ;
177
192
178
- let emails = app. as_inner ( ) . emails . clone ( ) ;
179
- let u = new_user ( "arbitrary_username" )
180
- . create_or_update ( Some ( email) , & emails, & mut conn)
181
- . await ?;
193
+ let emails = & app. as_inner ( ) . emails ;
194
+
195
+ let gh_user = GithubUser {
196
+ id : next_gh_id ( ) ,
197
+ login : "arbitrary_username" . to_string ( ) ,
198
+ name : None ,
199
+ email : Some ( email. to_string ( ) ) ,
200
+ avatar_url : None ,
201
+ } ;
202
+
203
+ let u =
204
+ session:: save_user_to_database ( & gh_user, "some random token" , emails, & mut conn) . await ?;
182
205
183
206
let user = MockCookieUser :: new ( & app, u) ;
184
207
let user_model = user. as_model ( ) ;
@@ -214,10 +237,18 @@ async fn test_existing_user_email() -> anyhow::Result<()> {
214
237
// email directly into the database and we want to test the verification flow here.
215
238
let email = "potahto@example.com" ;
216
239
217
- let emails = app. as_inner ( ) . emails . clone ( ) ;
218
- let u = new_user ( "arbitrary_username" )
219
- . create_or_update ( Some ( email) , & emails, & mut conn)
220
- . await ?;
240
+ let emails = & app. as_inner ( ) . emails ;
241
+
242
+ let gh_user = GithubUser {
243
+ id : next_gh_id ( ) ,
244
+ login : "arbitrary_username" . to_string ( ) ,
245
+ name : None ,
246
+ email : Some ( email. to_string ( ) ) ,
247
+ avatar_url : None ,
248
+ } ;
249
+
250
+ let u =
251
+ session:: save_user_to_database ( & gh_user, "some random token" , emails, & mut conn) . await ?;
221
252
222
253
update ( Email :: belonging_to ( & u) )
223
254
// Users created before we added verification will have
0 commit comments