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;
24use crate :: tests:: util:: { MockCookieUser , RequestHelper } ;
3- use crate :: tests:: { new_user , TestApp } ;
5+ use crate :: tests:: TestApp ;
46use crate :: util:: token:: HashedToken ;
7+ use crates_io_github:: GithubUser ;
58use diesel:: prelude:: * ;
69use diesel_async:: RunQueryDsl ;
710use http:: StatusCode ;
@@ -20,20 +23,20 @@ impl crate::tests::util::MockCookieUser {
2023#[ tokio:: test( flavor = "multi_thread" ) ]
2124async fn updating_existing_user_doesnt_change_api_token ( ) -> anyhow:: Result < ( ) > {
2225 let ( app, _, user, token) = TestApp :: init ( ) . with_token ( ) . await ;
26+ let emails = & app. as_inner ( ) . emails ;
2327 let mut conn = app. db_conn ( ) . await ;
2428 let gh_id = user. as_model ( ) . gh_id ;
2529 let token = token. plaintext ( ) ;
2630
2731 // 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 ) ;
3740
3841 // Use the original API token to find the now updated user
3942 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<()>
5861#[ tokio:: test( flavor = "multi_thread" ) ]
5962async fn github_without_email_does_not_overwrite_email ( ) -> anyhow:: Result < ( ) > {
6063 let ( app, _) = TestApp :: init ( ) . empty ( ) . await ;
64+ let emails = & app. as_inner ( ) . emails ;
6165 let mut conn = app. db_conn ( ) . await ;
6266
6367 // Simulate logging in via GitHub with an account that has no email.
68+
6469 // Because faking GitHub is terrible, call what GithubUser::save_to_database does directly.
6570 // 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 ?;
6982
7083 let user_without_github_email = MockCookieUser :: new ( & app, u) ;
71- let user_without_github_email_model = user_without_github_email. as_model ( ) ;
7284
7385 let json = user_without_github_email. show_me ( ) . await ;
7486 // 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<()> {
8092 . await ;
8193
8294 // 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 ( ) ;
8995
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+
93107 let again_user_without_github_email = MockCookieUser :: new ( & app, u) ;
94108
95109 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<()> {
120134
121135 let emails = app. as_inner ( ) . emails . clone ( ) ;
122136
123- let u = NewUser :: builder ( )
137+ let gh_user = GithubUser {
124138 // 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+ } ;
129145
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 ?;
133148
134149 let user_with_different_email_in_github = MockCookieUser :: new ( & app, u) ;
135150
@@ -175,10 +190,18 @@ async fn test_confirm_user_email() -> anyhow::Result<()> {
175190 // email directly into the database and we want to test the verification flow here.
176191 let email = "potato2@example.com" ;
177192
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 ?;
182205
183206 let user = MockCookieUser :: new ( & app, u) ;
184207 let user_model = user. as_model ( ) ;
@@ -214,10 +237,18 @@ async fn test_existing_user_email() -> anyhow::Result<()> {
214237 // email directly into the database and we want to test the verification flow here.
215238 let email = "potahto@example.com" ;
216239
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 ?;
221252
222253 update ( Email :: belonging_to ( & u) )
223254 // Users created before we added verification will have
0 commit comments