1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9use strict;
10use warnings;
11use utf8;
12
13use vars (qw($Self));
14
15my $Selenium = $Kernel::OM->Get('Kernel::System::UnitTest::Selenium');
16
17$Selenium->RunTest(
18    sub {
19
20        my $Helper          = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
21        my $TestEmailObject = $Kernel::OM->Get('Kernel::System::Email::Test');
22        my $MailQueueObject = $Kernel::OM->Get('Kernel::System::MailQueue');
23
24        my %MailQueueCurrentItems = map { $_->{ID} => $_ } @{ $MailQueueObject->List() || [] };
25
26        my $MailQueueClean = sub {
27            my $Items = $MailQueueObject->List();
28            MAIL_QUEUE_ITEM:
29            for my $Item ( @{$Items} ) {
30                next MAIL_QUEUE_ITEM if $MailQueueCurrentItems{ $Item->{ID} };
31                $MailQueueObject->Delete(
32                    ID => $Item->{ID},
33                );
34            }
35
36            return;
37        };
38
39        my $MailQueueProcess = sub {
40            my %Param = @_;
41
42            my $EmailObject = $Kernel::OM->Get('Kernel::System::Email');
43
44            # Process all items except the ones already present before the tests.
45            my $Items = $MailQueueObject->List();
46            MAIL_QUEUE_ITEM:
47            for my $Item ( @{$Items} ) {
48                next MAIL_QUEUE_ITEM if $MailQueueCurrentItems{ $Item->{ID} };
49                $MailQueueObject->Send( %{$Item} );
50            }
51
52            # Clean any garbage.
53            $MailQueueClean->();
54
55            return;
56        };
57
58        # Make sure we start with a clean mail queue.
59        $MailQueueClean->();
60
61        # Use test email backend.
62        $Helper->ConfigSettingChange(
63            Valid => 1,
64            Key   => 'SendmailModule',
65            Value => 'Kernel::System::Email::Test',
66        );
67
68        $Helper->ConfigSettingChange(
69            Key   => 'CheckEmailAddresses',
70            Value => 0,
71        );
72
73        # Clean up test email.
74        my $Success = $TestEmailObject->CleanUp();
75        $Self->True(
76            $Success,
77            'Initial cleanup',
78        );
79
80        $Self->IsDeeply(
81            $TestEmailObject->EmailsGet(),
82            [],
83            'Test email empty after initial cleanup',
84        );
85
86        # Create test customer user.
87        my $TestCustomerUser = $Helper->TestCustomerUserCreate(
88        ) || die "Did not get test customer user";
89
90        my $ScriptAlias = $Kernel::OM->Get('Kernel::Config')->Get('ScriptAlias');
91
92        # Navigate to customer login screen.
93        $Selenium->VerifiedGet("${ScriptAlias}customer.pl?");
94        $Selenium->delete_all_cookies();
95
96        # Click on 'Forgot password'.
97        $Selenium->VerifiedGet("${ScriptAlias}customer.pl?");
98        $Selenium->find_element( "#ForgotPassword", 'css' )->click();
99
100        $Selenium->WaitFor( JavaScript => "return \$('#ResetUser').length" );
101
102        # Request new password.
103        $Selenium->find_element( "#ResetUser",                   'css' )->send_keys($TestCustomerUser);
104        $Selenium->find_element( "#Reset button[type='submit']", 'css' )->VerifiedClick();
105
106        # Check for password recovery message.
107        $Self->True(
108            $Selenium->find_element( ".SuccessBox span", 'css' ),
109            "Password recovery message found on screen for valid customer",
110        );
111
112        # Process mail queue items.
113        $MailQueueProcess->();
114
115        # Check if password recovery email is sent.
116        my $Emails = $TestEmailObject->EmailsGet();
117        $Self->Is(
118            scalar @{$Emails},
119            1,
120            "Password recovery email sent for valid customer user $TestCustomerUser",
121        );
122
123        # Clean up test email again.
124        $Success = $TestEmailObject->CleanUp();
125        $Self->True(
126            $Success,
127            'Second cleanup',
128        );
129
130        $Self->IsDeeply(
131            $TestEmailObject->EmailsGet(),
132            [],
133            'Test email empty after second cleanup',
134        );
135
136        # Update test customer to invalid status.
137        $Success = $Kernel::OM->Get('Kernel::System::CustomerUser')->CustomerUserUpdate(
138            Source         => 'CustomerUser',
139            ID             => $TestCustomerUser,
140            UserCustomerID => $TestCustomerUser,
141            UserLogin      => $TestCustomerUser,
142            UserFirstname  => $TestCustomerUser,
143            UserLastname   => $TestCustomerUser,
144            UserPassword   => $TestCustomerUser,
145            UserEmail      => $TestCustomerUser . '@localunittest.com',
146            ValidID        => 2,
147            UserID         => 1,
148        );
149        $Self->True(
150            $Success,
151            "$TestCustomerUser set to invalid",
152        );
153
154        # Click on 'Forgot password' again.
155        $Selenium->find_element( "#ForgotPassword", 'css' )->click();
156
157        $Selenium->WaitFor( JavaScript => "return \$('#ResetUser').length" );
158
159        # Request new password.
160        $Selenium->find_element( "#ResetUser",                   'css' )->send_keys($TestCustomerUser);
161        $Selenium->find_element( "#Reset button[type='submit']", 'css' )->VerifiedClick();
162
163        # Check for password recovery message for invalid customer user, for security measures it
164        # should be visible.
165        $Self->True(
166            $Selenium->find_element( ".SuccessBox span", 'css' ),
167            "Password recovery message found on screen for invalid customer",
168        );
169
170        # Process mail queue items.
171        $MailQueueProcess->();
172
173        # Check if password recovery email is sent to invalid customer user.
174        $Emails = $TestEmailObject->EmailsGet();
175        $Self->Is(
176            scalar @{$Emails},
177            0,
178            "Password recovery email NOT sent for invalid customer user $TestCustomerUser",
179        );
180    }
181);
182
1831;
184