@@ -720,7 +720,7 @@ public async Task WillShowTheViewWithErrorsIfTheModelStateIsInvalid()
720720 public async Task WillInvalidateModelStateAndShowTheViewWhenAnEntityExceptionIsThrow ( )
721721 {
722722 GetMock < AuthenticationService > ( )
723- . Setup ( x => x . Register ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < Credential > ( ) ) )
723+ . Setup ( x => x . Register ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < Credential > ( ) , It . IsAny < bool > ( ) ) )
724724 . Throws ( new EntityException ( "aMessage" ) ) ;
725725
726726 var controller = GetController < AuthenticationController > ( ) ;
@@ -756,7 +756,7 @@ public async Task WillCreateAndLogInTheUserWhenNotLinking()
756756 var authenticationService = GetMock < AuthenticationService > ( ) ;
757757 var controller = GetController < AuthenticationController > ( ) ;
758758
759- authenticationService . Setup ( x => x . Register ( authUser . User . Username , authUser . User . UnconfirmedEmailAddress , It . IsAny < Credential > ( ) ) )
759+ authenticationService . Setup ( x => x . Register ( authUser . User . Username , authUser . User . UnconfirmedEmailAddress , It . IsAny < Credential > ( ) , It . IsAny < bool > ( ) ) )
760760 . CompletesWith ( authUser ) ;
761761
762762 authenticationService
@@ -807,7 +807,7 @@ public async Task WillNotSendConfirmationEmailWhenConfirmEmailAddressesIsOff()
807807 configurationService . Current . ConfirmEmailAddresses = false ;
808808
809809 GetMock < AuthenticationService > ( )
810- . Setup ( x
=> x . Register ( "theUsername" , "[email protected] " , It . IsAny < Credential > ( ) ) ) 810+ . Setup ( x
=> x . Register ( "theUsername" , "[email protected] " , It . IsAny < Credential > ( ) , It . IsAny < bool > ( ) ) ) 811811 . CompletesWith ( authUser ) ;
812812
813813 var controller = GetController < AuthenticationController > ( ) ;
@@ -830,6 +830,182 @@ public async Task WillNotSendConfirmationEmailWhenConfirmEmailAddressesIsOff()
830830 } , "/theReturnUrl" , linkingAccount : false ) ;
831831
832832 // Assert
833+ GetMock < AuthenticationService > ( )
834+ . Verify ( x
=> x . Register ( "theUsername" , "[email protected] " , It . IsAny < Credential > ( ) , false ) ) ; 835+
836+ GetMock < IMessageService > ( )
837+ . Verify ( x => x . SendNewAccountEmailAsync (
838+ It . IsAny < User > ( ) ,
839+ It . IsAny < string > ( ) ) , Times . Never ( ) ) ;
840+ }
841+
842+ [ Fact ]
843+ public async Task WillNotAutoConfirmAndWillSendConfirmationEmailWhenNotExternalCredential ( )
844+ {
845+ // Arrange
846+ var authUser = new AuthenticatedUser (
847+ new User ( "theUsername" )
848+ {
849+ UnconfirmedEmailAddress = "[email protected] " , 850+ EmailConfirmationToken = "t0k3n"
851+ } ,
852+ new Credential ( ) ) ;
853+
854+ var authenticationServiceMock = GetMock < AuthenticationService > ( ) ;
855+ var controller = GetController < AuthenticationController > ( ) ;
856+ authenticationServiceMock
857+ . Setup ( x => x . Register ( authUser . User . Username , authUser . User . UnconfirmedEmailAddress , It . IsAny < Credential > ( ) , It . IsAny < bool > ( ) ) )
858+ . CompletesWith ( authUser ) ;
859+ authenticationServiceMock
860+ . Setup ( x => x . CreateSessionAsync ( controller . OwinContext , authUser , false ) )
861+ . Returns ( Task . FromResult ( 0 ) )
862+ . Verifiable ( ) ;
863+ authenticationServiceMock
864+ . Setup ( x => x . ReadExternalLoginCredential ( controller . OwinContext ) )
865+ . CompletesWith ( new AuthenticateExternalLoginResult ( )
866+ {
867+ ExternalIdentity = new ClaimsIdentity ( ) ,
868+ Credential = new Credential ( ) ,
869+ UserInfo = new IdentityInformation ( "" , "" , authUser . User . UnconfirmedEmailAddress , "" )
870+ } ) ;
871+
872+ // Act
873+ var result = await controller . Register (
874+ new LogOnViewModel ( )
875+ {
876+ Register = new RegisterViewModel
877+ {
878+ Username = "theUsername" ,
879+ EmailAddress = authUser . User . UnconfirmedEmailAddress ,
880+ }
881+ } , "/theReturnUrl" , linkingAccount : true ) ;
882+
883+ // Assert
884+ authenticationServiceMock . VerifyAll ( ) ;
885+
886+ GetMock < AuthenticationService > ( )
887+ . Verify ( x => x . Register ( authUser . User . Username , authUser . User . UnconfirmedEmailAddress , It . IsAny < Credential > ( ) , false ) ) ;
888+
889+ GetMock < IMessageService > ( )
890+ . Verify ( x => x . SendNewAccountEmailAsync (
891+ authUser . User ,
892+ TestUtility . GallerySiteRootHttps + "account/confirm/" + authUser . User . Username + "/" + authUser . User . EmailConfirmationToken ) ) ;
893+
894+ ResultAssert . IsSafeRedirectTo ( result , "/theReturnUrl" ) ;
895+ }
896+
897+ [ Fact ]
898+ public async Task WillNotAutoConfirmAndWillSendConfirmationEmailWhenModelRegisterEmailAndExternalCredentialEmailNotMatch ( )
899+ {
900+ // Arrange
901+ var authUser = new AuthenticatedUser (
902+ new User ( "theUsername" )
903+ {
904+ UnconfirmedEmailAddress = "[email protected] " , 905+ EmailConfirmationToken = "t0k3n"
906+ } ,
907+ new Credential ( ) ) ;
908+
909+ var externalCred = new CredentialBuilder ( ) . CreateExternalCredential ( "MicrosoftAccount" , "blorg" , "Bloog" ) ;
910+
911+ var authenticationServiceMock = GetMock < AuthenticationService > ( ) ;
912+ var controller = GetController < AuthenticationController > ( ) ;
913+ authenticationServiceMock
914+ . Setup ( x
=> x . Register ( authUser . User . Username , "[email protected] " , externalCred , It . IsAny < bool > ( ) ) ) 915+ . CompletesWith ( authUser ) ;
916+ authenticationServiceMock
917+ . Setup ( x => x . CreateSessionAsync ( controller . OwinContext , authUser , false ) )
918+ . Returns ( Task . FromResult ( 0 ) )
919+ . Verifiable ( ) ;
920+ authenticationServiceMock
921+ . Setup ( x => x . ReadExternalLoginCredential ( controller . OwinContext ) )
922+ . CompletesWith ( new AuthenticateExternalLoginResult ( )
923+ {
924+ ExternalIdentity = new ClaimsIdentity ( ) ,
925+ Credential = externalCred ,
926+ UserInfo = new IdentityInformation ( "" , "" , "[email protected] " , "" ) 927+ } ) ;
928+
929+ // Act
930+ var result = await controller . Register (
931+ new LogOnViewModel ( )
932+ {
933+ Register = new RegisterViewModel
934+ {
935+ Username = "theUsername" ,
936+ EmailAddress = "[email protected] " , 937+ }
938+ } , "/theReturnUrl" , linkingAccount : true ) ;
939+
940+ // Assert
941+ authenticationServiceMock . VerifyAll ( ) ;
942+
943+ GetMock < AuthenticationService > ( )
944+ . Verify ( x
=> x . Register ( authUser . User . Username , "[email protected] " , externalCred , false ) ) ; 945+
946+ GetMock < IMessageService > ( )
947+ . Verify ( x => x . SendNewAccountEmailAsync (
948+ authUser . User ,
949+ TestUtility . GallerySiteRootHttps + "account/confirm/" + authUser . User . Username + "/" + authUser . User . EmailConfirmationToken ) ) ;
950+
951+ ResultAssert . IsSafeRedirectTo ( result , "/theReturnUrl" ) ;
952+ }
953+
954+ [ Fact ]
955+ public async Task WillAutoConfirmAndWillNotSendConfirmationEmailWhenIsExternalCredentialAndModelRegisterEmailAndExternalCredentialEmailMatch ( )
956+ {
957+ // Arrange
958+ var authUser = new AuthenticatedUser (
959+ new User ( "theUsername" )
960+ {
961+ UnconfirmedEmailAddress = "[email protected] " , 962+ EmailConfirmationToken = "t0k3n"
963+ } ,
964+ new Credential ( ) ) ;
965+
966+ var externalCred = new CredentialBuilder ( ) . CreateExternalCredential ( "MicrosoftAccount" , "blorg" , "Bloog" ) ;
967+
968+ var authenticationServiceMock = GetMock < AuthenticationService > ( ) ;
969+ var controller = GetController < AuthenticationController > ( ) ;
970+ authenticationServiceMock
971+ . Setup ( x => x . Register ( authUser . User . Username , authUser . User . UnconfirmedEmailAddress , externalCred , It . IsAny < bool > ( ) ) )
972+ . CompletesWith ( authUser )
973+ . Callback ( ( ) =>
974+ {
975+ authUser . User . EmailAddress = authUser . User . UnconfirmedEmailAddress ;
976+ authUser . User . UnconfirmedEmailAddress = null ;
977+ } ) ;
978+
979+ authenticationServiceMock
980+ . Setup ( x => x . CreateSessionAsync ( controller . OwinContext , authUser , false ) )
981+ . Returns ( Task . FromResult ( 0 ) )
982+ . Verifiable ( ) ;
983+ authenticationServiceMock
984+ . Setup ( x => x . ReadExternalLoginCredential ( controller . OwinContext ) )
985+ . CompletesWith ( new AuthenticateExternalLoginResult ( )
986+ {
987+ ExternalIdentity = new ClaimsIdentity ( ) ,
988+ Credential = externalCred ,
989+ UserInfo = new IdentityInformation ( "" , "" , authUser . User . UnconfirmedEmailAddress , "" )
990+ } ) ;
991+
992+ // Act
993+ var result = await controller . Register (
994+ new LogOnViewModel ( )
995+ {
996+ Register = new RegisterViewModel
997+ {
998+ Username = "theUsername" ,
999+ EmailAddress = authUser . User . UnconfirmedEmailAddress ,
1000+ }
1001+ } , "/theReturnUrl" , linkingAccount : true ) ;
1002+
1003+ // Assert
1004+ authenticationServiceMock . VerifyAll ( ) ;
1005+
1006+ GetMock < AuthenticationService > ( )
1007+ . Verify ( x => x . Register ( authUser . User . Username , authUser . User . EmailAddress , externalCred , true ) ) ;
1008+
8331009 GetMock < IMessageService > ( )
8341010 . Verify ( x => x . SendMessageAsync (
8351011 It . IsAny < NewAccountMessage > ( ) , It . IsAny < bool > ( ) , It . IsAny < bool > ( ) ) ,
@@ -868,7 +1044,7 @@ public async Task GivenExpiredExternalAuth_ItRedirectsBackToLogOnWithExternalAut
8681044 GetMock < AuthenticationService > ( )
8691045 . Verify ( x => x . CreateSessionAsync ( It . IsAny < IOwinContext > ( ) , It . IsAny < AuthenticatedUser > ( ) , false ) , Times . Never ( ) ) ;
8701046 GetMock < AuthenticationService > ( )
871- . Verify ( x => x . Register ( "theUsername" , "theEmailAddress" , It . IsAny < Credential > ( ) ) , Times . Never ( ) ) ;
1047+ . Verify ( x => x . Register ( "theUsername" , "theEmailAddress" , It . IsAny < Credential > ( ) , It . IsAny < bool > ( ) ) , Times . Never ( ) ) ;
8721048 }
8731049
8741050 [ Fact ]
@@ -888,7 +1064,7 @@ public async Task GivenValidExternalAuth_ItCreatesAccountAndLinksCredential()
8881064 var authenticationServiceMock = GetMock < AuthenticationService > ( ) ;
8891065 var controller = GetController < AuthenticationController > ( ) ;
8901066 authenticationServiceMock
891- . Setup ( x => x . Register ( authUser . User . Username , authUser . User . UnconfirmedEmailAddress , externalCred ) )
1067+ . Setup ( x => x . Register ( authUser . User . Username , authUser . User . UnconfirmedEmailAddress , externalCred , It . IsAny < bool > ( ) ) )
8921068 . CompletesWith ( authUser ) ;
8931069 authenticationServiceMock
8941070 . Setup ( x => x . CreateSessionAsync ( controller . OwinContext , authUser , false ) )
@@ -900,7 +1076,8 @@ public async Task GivenValidExternalAuth_ItCreatesAccountAndLinksCredential()
9001076 . CompletesWith ( new AuthenticateExternalLoginResult ( )
9011077 {
9021078 ExternalIdentity = new ClaimsIdentity ( ) ,
903- Credential = externalCred
1079+ Credential = externalCred ,
1080+ UserInfo = new IdentityInformation ( "" , "" , "" , "" )
9041081 } ) ;
9051082
9061083 // Simulate the model state error that will be added when doing an external account registration (since password is not present)
@@ -959,7 +1136,7 @@ public async Task GivenAdminLogsInWithExternalIdentity_ItChallengesWhenNotUsingR
9591136 externalCred ) ;
9601137
9611138 GetMock < AuthenticationService > ( )
962- . Setup ( x => x . Register ( "theUsername" , "theEmailAddress" , externalCred ) )
1139+ . Setup ( x => x . Register ( "theUsername" , "theEmailAddress" , externalCred , It . IsAny < bool > ( ) ) )
9631140 . CompletesWith ( authUser ) ;
9641141
9651142 EnableAllAuthenticators ( Get < AuthenticationService > ( ) ) ;
@@ -985,7 +1162,8 @@ public async Task GivenAdminLogsInWithExternalIdentity_ItChallengesWhenNotUsingR
9851162 . CompletesWith ( new AuthenticateExternalLoginResult ( )
9861163 {
9871164 ExternalIdentity = new ClaimsIdentity ( ) ,
988- Credential = externalCred
1165+ Credential = externalCred ,
1166+ UserInfo = new IdentityInformation ( "" , "" , "theEmailAddress" , "" )
9891167 } ) ;
9901168
9911169 // Act
0 commit comments