11using Grand . Business . Core . Utilities . Authentication ;
22using Grand . Business . Core . Interfaces . Authentication ;
3+ using Grand . Business . Core . Interfaces . Common . Directory ;
34using Grand . Business . Core . Interfaces . Customers ;
45using Grand . Business . Core . Interfaces . Messages ;
56using Grand . Domain . Customers ;
@@ -14,50 +15,105 @@ namespace Grand.Web.Tests.Commands.Customers;
1415[ TestClass ]
1516public class PasswordRecoverySendCommandHandlerTests
1617{
18+ private readonly Customer _customer = new ( ) { Id = "c1" , Active = true } ;
19+ private Mock < ICustomerService > _customerService ;
20+ private Mock < IGroupService > _groupService ;
21+ private PasswordRecoverySendCommandHandler _handler ;
22+ private Mock < ICustomerMessageProviderService > _messages ;
23+ private Mock < IExternalSignInPolicy > _policy ;
24+
25+ [ TestInitialize ]
26+ public void Init ( )
27+ {
28+ _customerService = new Mock < ICustomerService > ( ) ;
29+ _messages = new Mock < ICustomerMessageProviderService > ( ) ;
30+ _policy = new Mock < IExternalSignInPolicy > ( ) ;
31+ _policy . Setup ( x => x . PasswordSignIn ( It . IsAny < Customer > ( ) , SignInSurface . Storefront ) )
32+ . ReturnsAsync ( PasswordSignInDecision . Password ) ;
33+ _groupService = new Mock < IGroupService > ( ) ;
34+ _groupService . Setup ( x => x . IsRegistered ( _customer ) ) . ReturnsAsync ( true ) ;
35+ _handler = new PasswordRecoverySendCommandHandler ( _customerService . Object , _messages . Object , _policy . Object ,
36+ _groupService . Object ) ;
37+ }
38+
39+ private Task < bool > Send ( Customer customer ) =>
40+ _handler . Handle ( new PasswordRecoverySendCommand {
41+ Customer = customer , Store = new Grand . Domain . Stores . Store ( ) , Language = new Language { Id = "l1" }
42+ } , CancellationToken . None ) ;
43+
44+ private void VerifyNoMessage ( )
45+ {
46+ _messages . Verify ( x => x . SendCustomerPasswordRecoveryMessage ( It . IsAny < Customer > ( ) ,
47+ It . IsAny < Grand . Domain . Stores . Store > ( ) , It . IsAny < string > ( ) ) , Times . Never ) ;
48+ _customerService . Verify ( x => x . UpdateUserField ( It . IsAny < Customer > ( ) , It . IsAny < string > ( ) , It . IsAny < string > ( ) ,
49+ It . IsAny < string > ( ) ) , Times . Never ) ;
50+ }
51+
1752 [ TestMethod ]
1853 public async Task ExternalSignInRequired_NoMessage_NoToken ( )
1954 {
20- var customerService = new Mock < ICustomerService > ( ) ;
21- var messages = new Mock < ICustomerMessageProviderService > ( ) ;
22- var policy = new Mock < IExternalSignInPolicy > ( ) ;
23- var customer = new Customer { Id = "c1" } ;
24- policy . Setup ( x => x . PasswordSignIn ( customer , It . IsAny < SignInSurface > ( ) ) ) . ReturnsAsync ( new PasswordSignInDecision ( false , new Mock < IExternalAuthenticationProvider > ( ) . Object ) ) ;
25- var handler = new PasswordRecoverySendCommandHandler ( customerService . Object , messages . Object , policy . Object ) ;
55+ _policy . Setup ( x => x . PasswordSignIn ( _customer , It . IsAny < SignInSurface > ( ) ) )
56+ . ReturnsAsync ( new PasswordSignInDecision ( false , new Mock < IExternalAuthenticationProvider > ( ) . Object ) ) ;
2657
27- Assert . IsTrue ( await handler . Handle ( new PasswordRecoverySendCommand {
28- Customer = customer , Store = new Grand . Domain . Stores . Store ( ) , Language = new Language { Id = "l1" }
29- } , CancellationToken . None ) ) ;
58+ Assert . IsTrue ( await Send ( _customer ) ) ;
3059
31- messages . Verify ( x => x . SendCustomerPasswordRecoveryMessage ( It . IsAny < Customer > ( ) , It . IsAny < Grand . Domain . Stores . Store > ( ) , It . IsAny < string > ( ) ) , Times . Never ) ;
32- customerService . Verify ( x => x . UpdateUserField ( It . IsAny < Customer > ( ) , It . IsAny < string > ( ) , It . IsAny < string > ( ) , It . IsAny < string > ( ) ) , Times . Never ) ;
60+ VerifyNoMessage ( ) ;
3361 }
3462
3563 [ TestMethod ]
3664 public async Task NullCustomer_NoMessage ( )
3765 {
38- var messages = new Mock < ICustomerMessageProviderService > ( ) ;
39- var handler = new PasswordRecoverySendCommandHandler ( new Mock < ICustomerService > ( ) . Object , messages . Object ,
40- new Mock < IExternalSignInPolicy > ( ) . Object ) ;
66+ Assert . IsTrue ( await Send ( null ) ) ;
67+
68+ VerifyNoMessage ( ) ;
69+ }
70+
71+ [ TestMethod ]
72+ public async Task InactiveCustomer_NoMessage ( )
73+ {
74+ _customer . Active = false ;
75+
76+ Assert . IsTrue ( await Send ( _customer ) ) ;
77+
78+ VerifyNoMessage ( ) ;
79+ }
80+
81+ [ TestMethod ]
82+ public async Task DeletedCustomer_NoMessage ( )
83+ {
84+ _customer . Deleted = true ;
85+
86+ Assert . IsTrue ( await Send ( _customer ) ) ;
87+
88+ VerifyNoMessage ( ) ;
89+ }
90+
91+ [ TestMethod ]
92+ public async Task LockedOutCustomer_NoMessage ( )
93+ {
94+ _customer . CannotLoginUntilDateUtc = DateTime . UtcNow . AddMinutes ( 10 ) ;
4195
42- Assert . IsTrue ( await handler . Handle ( new PasswordRecoverySendCommand {
43- Store = new Grand . Domain . Stores . Store ( ) , Language = new Language { Id = "l1" }
44- } , CancellationToken . None ) ) ;
96+ Assert . IsTrue ( await Send ( _customer ) ) ;
4597
46- messages . Verify ( x => x . SendCustomerPasswordRecoveryMessage ( It . IsAny < Customer > ( ) , It . IsAny < Grand . Domain . Stores . Store > ( ) , It . IsAny < string > ( ) ) , Times . Never ) ;
98+ VerifyNoMessage ( ) ;
99+ }
100+
101+ [ TestMethod ]
102+ public async Task NotRegistered_NoMessage ( )
103+ {
104+ _groupService . Setup ( x => x . IsRegistered ( _customer ) ) . ReturnsAsync ( false ) ;
105+
106+ Assert . IsTrue ( await Send ( _customer ) ) ;
107+
108+ VerifyNoMessage ( ) ;
47109 }
48110
49111 [ TestMethod ]
50112 public async Task PasswordAllowed_MessageSent ( )
51113 {
52- var messages = new Mock < ICustomerMessageProviderService > ( ) ;
53- var policy = new Mock < IExternalSignInPolicy > ( ) ;
54- policy . Setup ( x => x . PasswordSignIn ( It . IsAny < Customer > ( ) , SignInSurface . Storefront ) )
55- . ReturnsAsync ( PasswordSignInDecision . Password ) ;
56- var handler = new PasswordRecoverySendCommandHandler ( new Mock < ICustomerService > ( ) . Object , messages . Object ,
57- policy . Object ) ;
58- await handler . Handle ( new PasswordRecoverySendCommand {
59- Customer = new Customer { Id = "c1" } , Store = new Grand . Domain . Stores . Store ( ) , Language = new Language { Id = "l1" }
60- } , CancellationToken . None ) ;
61- messages . Verify ( x => x . SendCustomerPasswordRecoveryMessage ( It . IsAny < Customer > ( ) , It . IsAny < Grand . Domain . Stores . Store > ( ) , "l1" ) , Times . Once ) ;
114+ await Send ( _customer ) ;
115+
116+ _messages . Verify ( x => x . SendCustomerPasswordRecoveryMessage ( _customer , It . IsAny < Grand . Domain . Stores . Store > ( ) ,
117+ "l1" ) , Times . Once ) ;
62118 }
63- }
119+ }
0 commit comments