@@ -42,7 +42,8 @@ public void Init()
4242 _externalAuthenticationService = CreateService ( UserRegistrationType . Standard ) ;
4343 }
4444
45- private ExternalAuthenticationService CreateService ( UserRegistrationType userRegistrationType )
45+ private ExternalAuthenticationService CreateService ( UserRegistrationType userRegistrationType ,
46+ bool usernamesEnabled = false )
4647 {
4748 var externalAuthenticationSetting = new ExternalAuthenticationSettings {
4849 ActiveAuthenticationMethodSystemNames = [
@@ -53,7 +54,8 @@ private ExternalAuthenticationService CreateService(UserRegistrationType userReg
5354 return new ExternalAuthenticationService ( _customerManagerServiceMock . Object , _customerServiceMock . Object ,
5455 _groupServiceMock . Object , _mediatorMock . Object , _externalAuthenticationRecordRepository ,
5556 _externalAuthenticationProviders ,
56- new CustomerSettings { UserRegistrationType = userRegistrationType } , externalAuthenticationSetting ) ;
57+ new CustomerSettings { UserRegistrationType = userRegistrationType , UsernamesEnabled = usernamesEnabled } ,
58+ externalAuthenticationSetting ) ;
5759 }
5860
5961
@@ -167,6 +169,69 @@ public async Task Authenticate_UnknownIdentity_RegistersPassedCustomerInPassedSt
167169 Assert . AreSame ( guest , result . Customer ) ;
168170 }
169171
172+ [ TestMethod ]
173+ public async Task Authenticate_UnknownIdentityWithEmailOfExistingCustomer_RegistersNobody ( )
174+ {
175+ _customerServiceMock . Setup ( c => c . GetCustomerByEmail ( "taken@test.com" , "store1" ) )
176+ . ReturnsAsync ( new Customer { Id = "existing" , Email = "taken@test.com" } ) ;
177+ var guest = new Customer { Id = "guest" } ;
178+ var parameters = new ExternalAuthParam {
179+ ProviderSystemName = "ExternalAuthenticationProviderTest" , Identifier = "new" , Email = "taken@test.com"
180+ } ;
181+
182+ var result =
183+ await _externalAuthenticationService . Authenticate ( parameters , guest , new Store { Id = "store1" } , new Language ( ) ) ;
184+
185+ Assert . AreEqual ( ExternalAuthStatus . EmailAlreadyExists , result . Status ) ;
186+ Assert . IsNull ( result . Customer ) ;
187+ _customerManagerServiceMock . Verify ( m => m . RegisterCustomer ( It . IsAny < RegistrationRequest > ( ) ) , Times . Never ) ;
188+ Assert . IsFalse ( _externalAuthenticationRecordRepository . Table . Any ( ) ) ;
189+ }
190+
191+ [ TestMethod ]
192+ public async Task Authenticate_UnknownIdentityWithEmailUsedAsUsername_RegistersNobody ( )
193+ {
194+ _customerServiceMock . Setup ( c => c . GetCustomerByUsername ( "taken@test.com" , "store1" ) )
195+ . ReturnsAsync ( new Customer { Id = "existing" , Username = "taken@test.com" , Email = "other@test.com" } ) ;
196+ var parameters = new ExternalAuthParam {
197+ ProviderSystemName = "ExternalAuthenticationProviderTest" , Identifier = "new" , Email = "taken@test.com"
198+ } ;
199+
200+ var result = await CreateService ( UserRegistrationType . Standard , usernamesEnabled : true )
201+ . Authenticate ( parameters , new Customer { Id = "guest" } , new Store { Id = "store1" } , new Language ( ) ) ;
202+
203+ Assert . AreEqual ( ExternalAuthStatus . EmailAlreadyExists , result . Status ) ;
204+ _customerManagerServiceMock . Verify ( m => m . RegisterCustomer ( It . IsAny < RegistrationRequest > ( ) ) , Times . Never ) ;
205+ }
206+
207+ [ TestMethod ]
208+ public async Task Authenticate_UnknownIdentityWithoutEmail_RegistersNobody ( )
209+ {
210+ var parameters = new ExternalAuthParam { ProviderSystemName = "ExternalAuthenticationProviderTest" , Identifier = "new" } ;
211+
212+ var result = await _externalAuthenticationService . Authenticate ( parameters , new Customer { Id = "guest" } ,
213+ new Store { Id = "store1" } , new Language ( ) ) ;
214+
215+ Assert . AreEqual ( ExternalAuthStatus . EmailRequired , result . Status ) ;
216+ _customerManagerServiceMock . Verify ( m => m . RegisterCustomer ( It . IsAny < RegistrationRequest > ( ) ) , Times . Never ) ;
217+ Assert . IsFalse ( _externalAuthenticationRecordRepository . Table . Any ( ) ) ;
218+ }
219+
220+ [ TestMethod ]
221+ public async Task Authenticate_UnknownIdentityWithEmailOfExistingCustomerAndRegistrationDisabled_IsEmailAlreadyExists ( )
222+ {
223+ _customerServiceMock . Setup ( c => c . GetCustomerByEmail ( "taken@test.com" , "store1" ) )
224+ . ReturnsAsync ( new Customer { Id = "existing" , Email = "taken@test.com" } ) ;
225+ var parameters = new ExternalAuthParam {
226+ ProviderSystemName = "ExternalAuthenticationProviderTest" , Identifier = "new" , Email = "taken@test.com"
227+ } ;
228+
229+ var result = await CreateService ( UserRegistrationType . Disabled )
230+ . Authenticate ( parameters , new Customer { Id = "guest" } , new Store { Id = "store1" } , new Language ( ) ) ;
231+
232+ Assert . AreEqual ( ExternalAuthStatus . EmailAlreadyExists , result . Status ) ;
233+ }
234+
170235 [ TestMethod ]
171236 public async Task Authenticate_UnknownIdentityWithAdminApproval_IsAwaitingApproval ( )
172237 {
0 commit comments