@@ -1156,13 +1156,38 @@ public async Task SetsUnconfirmedEmailWhenEmailIsChanged()
11561156 Users = new [ ] { user }
11571157 } ;
11581158
1159+ service . MockConfig
1160+ . Setup ( x => x . ConfirmEmailAddresses )
1161+ . Returns ( true ) ;
1162+
11591163 await service . ChangeEmailAddress ( user , "[email protected] " ) ; 11601164
11611165 Assert . Equal ( "[email protected] " , user . EmailAddress ) ; 11621166 Assert . Equal ( "[email protected] " , user . UnconfirmedEmailAddress ) ; 11631167 service . FakeEntitiesContext . VerifyCommitChanges ( ) ;
11641168 }
11651169
1170+ [ Fact ]
1171+ public async Task AutomaticallyConfirmsWhenConfirmEmailAddressesConfigurationIsFalse ( )
1172+ {
1173+ var user = new User { Username = "Bob" , EmailAddress = "[email protected] " } ; 1174+ var service = new TestableUserServiceWithDBFaking
1175+ {
1176+ Users = new [ ] { user }
1177+ } ;
1178+
1179+ service . MockConfig
1180+ . Setup ( x => x . ConfirmEmailAddresses )
1181+ . Returns ( false ) ;
1182+
1183+ await service . ChangeEmailAddress ( user , "[email protected] " ) ; 1184+
1185+ Assert . Equal ( "[email protected] " , user . EmailAddress ) ; 1186+ Assert . Null ( user . UnconfirmedEmailAddress ) ;
1187+ Assert . Null ( user . EmailConfirmationToken ) ;
1188+ service . FakeEntitiesContext . VerifyCommitChanges ( ) ;
1189+ }
1190+
11661191 /// <summary>
11671192 /// It has to change the pending confirmation token whenever address changes because otherwise you can do
11681193 /// 1. change address, get confirmation email
@@ -1178,6 +1203,10 @@ public async Task ModifiesConfirmationTokenWhenEmailAddressChanged()
11781203 Users = new User [ ] { user } ,
11791204 } ;
11801205
1206+ service . MockConfig
1207+ . Setup ( x => x . ConfirmEmailAddresses )
1208+ . Returns ( true ) ;
1209+
11811210 await service . ChangeEmailAddress ( user , "[email protected] " ) ; 11821211 Assert . NotNull ( user . EmailConfirmationToken ) ;
11831212 Assert . NotEmpty ( user . EmailConfirmationToken ) ;
@@ -1197,6 +1226,10 @@ public async Task DoesNotModifyAnythingWhenConfirmedEmailAddressNotChanged()
11971226 Users = new User [ ] { user } ,
11981227 } ;
11991228
1229+ service . MockConfig
1230+ . Setup ( x => x . ConfirmEmailAddresses )
1231+ . Returns ( true ) ;
1232+
12001233 await service . ChangeEmailAddress ( user , "[email protected] " ) ; 12011234 Assert . True ( user . Confirmed ) ;
12021235 Assert . Equal ( "[email protected] " , user . EmailAddress ) ; @@ -1221,6 +1254,10 @@ public async Task DoesNotModifyConfirmationTokenWhenUnconfirmedEmailAddressNotCh
12211254 Users = new User [ ] { user } ,
12221255 } ;
12231256
1257+ service . MockConfig
1258+ . Setup ( x => x . ConfirmEmailAddresses )
1259+ . Returns ( true ) ;
1260+
12241261 await service . ChangeEmailAddress ( user , "[email protected] " ) ; 12251262 Assert . Equal ( "pending-token" , user . EmailConfirmationToken ) ;
12261263 }
@@ -1784,11 +1821,16 @@ public class TheAddOrganizationAccountMethod
17841821
17851822 private TestableUserService _service = new TestableUserService ( ) ;
17861823
1787- [ Fact ]
1788- public async Task WithUsernameConflict_ThrowsEntityException ( )
1824+ public static IEnumerable < object [ ] > ConfirmEmailAddresses_Config => MemberDataHelper . AsDataSet ( false , true ) ;
1825+
1826+ [ Theory ]
1827+ [ MemberData ( nameof ( ConfirmEmailAddresses_Config ) ) ]
1828+ public async Task WithUsernameConflict_ThrowsEntityException ( bool confirmEmailAddresses )
17891829 {
17901830 var conflictUsername = "ialreadyexist" ;
17911831
1832+ SetUpConfirmEmailAddressesConfig ( confirmEmailAddresses ) ;
1833+
17921834 _service . MockEntitiesContext
17931835 . Setup ( x => x . Users )
17941836 . Returns ( new [ ] { new User ( conflictUsername ) } . MockDbSet ( ) . Object ) ;
@@ -1802,11 +1844,14 @@ public async Task WithUsernameConflict_ThrowsEntityException()
18021844 Assert . False ( _service . Auditing . WroteRecord < UserAuditRecord > ( ) ) ;
18031845 }
18041846
1805- [ Fact ]
1806- public async Task WithEmailConflict_ThrowsEntityException ( )
1847+ [ Theory ]
1848+ [ MemberData ( nameof ( ConfirmEmailAddresses_Config ) ) ]
1849+ public async Task WithEmailConflict_ThrowsEntityException ( bool confirmEmailAddresses )
18071850 {
18081851 var conflictEmail = "[email protected] " ; 18091852
1853+ SetUpConfirmEmailAddressesConfig ( confirmEmailAddresses ) ;
1854+
18101855 _service . MockEntitiesContext
18111856 . Setup ( x => x . Users )
18121857 . Returns ( new [ ] { new User ( "user" ) { EmailAddress = conflictEmail } } . MockDbSet ( ) . Object ) ;
@@ -1820,25 +1865,31 @@ public async Task WithEmailConflict_ThrowsEntityException()
18201865 Assert . False ( _service . Auditing . WroteRecord < UserAuditRecord > ( ) ) ;
18211866 }
18221867
1823- [ Fact ]
1824- public async Task WhenAdminHasNoTenant_ReturnsNewOrgWithoutPolicy ( )
1868+ [ Theory ]
1869+ [ MemberData ( nameof ( ConfirmEmailAddresses_Config ) ) ]
1870+ public async Task WhenAdminHasNoTenant_ReturnsNewOrgWithoutPolicy ( bool confirmEmailAddresses )
18251871 {
18261872 _service . MockEntitiesContext
18271873 . Setup ( x => x . Users )
18281874 . Returns ( Enumerable . Empty < User > ( ) . MockDbSet ( ) . Object ) ;
18291875
1876+ SetUpConfirmEmailAddressesConfig ( confirmEmailAddresses ) ;
1877+
18301878 var org = await InvokeAddOrganization ( admin : new User ( AdminName ) { Credentials = new Credential [ 0 ] } ) ;
18311879
1832- AssertNewOrganizationReturned ( org , subscribedToPolicy : false ) ;
1880+ AssertNewOrganizationReturned ( org , subscribedToPolicy : false , confirmEmailAddresses : confirmEmailAddresses ) ;
18331881 }
18341882
1835- [ Fact ]
1836- public async Task WhenAdminHasUnsupportedTenant_ReturnsNewOrgWithoutPolicy ( )
1883+ [ Theory ]
1884+ [ MemberData ( nameof ( ConfirmEmailAddresses_Config ) ) ]
1885+ public async Task WhenAdminHasUnsupportedTenant_ReturnsNewOrgWithoutPolicy ( bool confirmEmailAddresses )
18371886 {
18381887 _service . MockEntitiesContext
18391888 . Setup ( x => x . Users )
18401889 . Returns ( Enumerable . Empty < User > ( ) . MockDbSet ( ) . Object ) ;
18411890
1891+ SetUpConfirmEmailAddressesConfig ( confirmEmailAddresses ) ;
1892+
18421893 var mockLoginDiscontinuationConfiguration = new Mock < ILoginDiscontinuationConfiguration > ( ) ;
18431894 mockLoginDiscontinuationConfiguration
18441895 . Setup ( x => x . IsTenantIdPolicySupportedForOrganization ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
@@ -1850,16 +1901,19 @@ public async Task WhenAdminHasUnsupportedTenant_ReturnsNewOrgWithoutPolicy()
18501901
18511902 var org = await InvokeAddOrganization ( ) ;
18521903
1853- AssertNewOrganizationReturned ( org , subscribedToPolicy : false ) ;
1904+ AssertNewOrganizationReturned ( org , subscribedToPolicy : false , confirmEmailAddresses : confirmEmailAddresses ) ;
18541905 }
18551906
1856- [ Fact ]
1857- public async Task WhenSubscribingToPolicyFails_ReturnsNewOrgWithoutPolicy ( )
1907+ [ Theory ]
1908+ [ MemberData ( nameof ( ConfirmEmailAddresses_Config ) ) ]
1909+ public async Task WhenSubscribingToPolicyFails_ReturnsNewOrgWithoutPolicy ( bool confirmEmailAddresses )
18581910 {
18591911 _service . MockEntitiesContext
18601912 . Setup ( x => x . Users )
18611913 . Returns ( Enumerable . Empty < User > ( ) . MockDbSet ( ) . Object ) ;
18621914
1915+ SetUpConfirmEmailAddressesConfig ( confirmEmailAddresses ) ;
1916+
18631917 var mockLoginDiscontinuationConfiguration = new Mock < ILoginDiscontinuationConfiguration > ( ) ;
18641918 mockLoginDiscontinuationConfiguration
18651919 . Setup ( x => x . IsTenantIdPolicySupportedForOrganization ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
@@ -1875,16 +1929,19 @@ public async Task WhenSubscribingToPolicyFails_ReturnsNewOrgWithoutPolicy()
18751929
18761930 var org = await InvokeAddOrganization ( ) ;
18771931
1878- AssertNewOrganizationReturned ( org , subscribedToPolicy : true ) ;
1932+ AssertNewOrganizationReturned ( org , subscribedToPolicy : true , confirmEmailAddresses : confirmEmailAddresses ) ;
18791933 }
18801934
1881- [ Fact ]
1882- public async Task WhenSubscribingToPolicySucceeds_ReturnsNewOrg ( )
1935+ [ Theory ]
1936+ [ MemberData ( nameof ( ConfirmEmailAddresses_Config ) ) ]
1937+ public async Task WhenSubscribingToPolicySucceeds_ReturnsNewOrg ( bool confirmEmailAddresses )
18831938 {
18841939 _service . MockEntitiesContext
18851940 . Setup ( x => x . Users )
18861941 . Returns ( Enumerable . Empty < User > ( ) . MockDbSet ( ) . Object ) ;
18871942
1943+ SetUpConfirmEmailAddressesConfig ( confirmEmailAddresses ) ;
1944+
18881945 var mockLoginDiscontinuationConfiguration = new Mock < ILoginDiscontinuationConfiguration > ( ) ;
18891946 mockLoginDiscontinuationConfiguration
18901947 . Setup ( x => x . IsTenantIdPolicySupportedForOrganization ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
@@ -1900,7 +1957,7 @@ public async Task WhenSubscribingToPolicySucceeds_ReturnsNewOrg()
19001957
19011958 var org = await InvokeAddOrganization ( ) ;
19021959
1903- AssertNewOrganizationReturned ( org , subscribedToPolicy : true ) ;
1960+ AssertNewOrganizationReturned ( org , subscribedToPolicy : true , confirmEmailAddresses : confirmEmailAddresses ) ;
19041961 }
19051962
19061963 private Task < Organization > InvokeAddOrganization ( string orgName = OrgName , string orgEmail = OrgEmail , User admin = null )
@@ -1925,14 +1982,26 @@ private Task<Organization> InvokeAddOrganization(string orgName = OrgName, strin
19251982 return _service . AddOrganizationAsync ( orgName , orgEmail , admin ) ;
19261983 }
19271984
1928- private void AssertNewOrganizationReturned ( Organization org , bool subscribedToPolicy )
1985+ private void AssertNewOrganizationReturned ( Organization org , bool subscribedToPolicy , bool confirmEmailAddresses )
19291986 {
19301987 Assert . Equal ( OrgName , org . Username ) ;
1931- Assert . Equal ( OrgEmail , org . UnconfirmedEmailAddress ) ;
1988+
1989+ if ( confirmEmailAddresses )
1990+ {
1991+ Assert . Null ( org . EmailAddress ) ;
1992+ Assert . Equal ( OrgEmail , org . UnconfirmedEmailAddress ) ;
1993+ Assert . NotNull ( org . EmailConfirmationToken ) ;
1994+ }
1995+ else
1996+ {
1997+ Assert . Null ( org . UnconfirmedEmailAddress ) ;
1998+ Assert . Equal ( OrgEmail , org . EmailAddress ) ;
1999+ Assert . Null ( org . EmailConfirmationToken ) ;
2000+ }
2001+
19322002 Assert . Equal ( OrgCreatedUtc , org . CreatedUtc ) ;
19332003 Assert . True ( org . EmailAllowed ) ;
19342004 Assert . True ( org . NotifyPackagePushed ) ;
1935- Assert . True ( ! string . IsNullOrEmpty ( org . EmailConfirmationToken ) ) ;
19362005
19372006 // Both the organization and the admin must have a membership to each other.
19382007 Func < Membership , bool > hasMembership = m => m . Member . Username == AdminName && m . Organization . Username == OrgName && m . IsAdmin ;
@@ -1949,6 +2018,13 @@ private void AssertNewOrganizationReturned(Organization org, bool subscribedToPo
19492018 ar . AffectedMemberIsAdmin == true ) ) ;
19502019 _service . MockEntitiesContext . Verify ( x => x . SaveChangesAsync ( ) , Times . Once ( ) ) ;
19512020 }
2021+
2022+ private void SetUpConfirmEmailAddressesConfig ( bool confirmEmailAddresses )
2023+ {
2024+ _service . MockConfig
2025+ . Setup ( x => x . ConfirmEmailAddresses )
2026+ . Returns ( confirmEmailAddresses ) ;
2027+ }
19522028 }
19532029 public class TheRejectTransformUserToOrganizationRequestMethod
19542030 {
0 commit comments