Skip to content

Commit

Permalink
Fix Email test issue discovered in silverstripe#5271.
Browse files Browse the repository at this point in the history
Updated/added tests for changed- and forgot-password Emails.
Updated fixture and tests to no longer use a real Email address.
  • Loading branch information
bummzack committed Apr 11, 2016
1 parent 1d3b621 commit 9146450
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
12 changes: 6 additions & 6 deletions dev/SapphireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,6 @@ public function setUp() {

$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';

// Set up email
$this->originalMailer = Email::mailer();
$this->mailer = new TestMailer();
Injector::inst()->registerService($this->mailer, 'Mailer');
Config::inst()->remove('Email', 'send_all_emails_to');

// Todo: this could be a special test model
$this->model = DataModel::inst();

Expand Down Expand Up @@ -288,6 +282,12 @@ public function setUp() {

// Clear requirements
Requirements::clear();

// Set up email
$this->originalMailer = Email::mailer();
$this->mailer = new TestMailer();
Injector::inst()->registerService($this->mailer, 'Mailer');
Config::inst()->remove('Email', 'send_all_emails_to');
}

/**
Expand Down
34 changes: 30 additions & 4 deletions tests/security/MemberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,42 @@ public function testPasswordChangeLogging() {
* Test that changed passwords will send an email
*/
public function testChangedPasswordEmaling() {
Config::inst()->update('Member', 'notify_password_change', true);

$this->clearEmails();

$member = $this->objFromFixture('Member', 'test');
$this->assertNotNull($member);
$valid = $member->changePassword('32asDF##$$%%');
$this->assertTrue($valid->valid());
/*
$this->assertEmailSent("sam@silverstripe.com", null, "/changed password/",
'/sam@silverstripe\.com.*32asDF##\$\$%%/');
*/

$this->assertEmailSent('testuser@example.com', null, 'Your password has been changed',
'/testuser@example\.com/');

}

/**
* Test that triggering "forgotPassword" sends an Email with a reset link
*/
public function testForgotPasswordEmaling() {
$this->clearEmails();
$this->autoFollowRedirection = false;

$member = $this->objFromFixture('Member', 'test');
$this->assertNotNull($member);

// Initiate a password-reset
$response = $this->post('Security/LostPasswordForm', array('Email' => $member->Email));

$this->assertEquals($response->getStatusCode(), 302);

// We should get redirected to Security/passwordsent
$this->assertContains('Security/passwordsent/testuser@example.com',
urldecode($response->getHeader('Location')));

// Check existance of reset link
$this->assertEmailSent("testuser@example.com", null, 'Your password reset link',
'/Security\/changepassword\?m='.$member->ID.'&t=[^"]+/');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/security/MemberTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Member:
test:
FirstName: Test
Surname: User
Email: sam@silverstripe.com
Email: testuser@example.com
Password: 1nitialPassword
PasswordExpiry: 2030-01-01
Groups: =>Group.securityadminsgroup
Expand Down
46 changes: 23 additions & 23 deletions tests/security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ public function testExternalBackUrlRedirectionDisallowed() {
*/
public function testExpiredPassword() {
/* BAD PASSWORDS ARE LOCKED OUT */
$badResponse = $this->doTestLoginForm('sam@silverstripe.com' , 'badpassword');
$badResponse = $this->doTestLoginForm('testuser@example.com' , 'badpassword');
$this->assertEquals(302, $badResponse->getStatusCode());
$this->assertRegExp('/Security\/login/', $badResponse->getHeader('Location'));
$this->assertNull($this->session()->inst_get('loggedInAs'));

/* UNEXPIRED PASSWORD GO THROUGH WITHOUT A HITCH */
$goodResponse = $this->doTestLoginForm('sam@silverstripe.com' , '1nitialPassword');
$goodResponse = $this->doTestLoginForm('testuser@example.com' , '1nitialPassword');
$this->assertEquals(302, $goodResponse->getStatusCode());
$this->assertEquals(
Controller::join_links(Director::absoluteBaseURL(), 'test/link'),
Expand Down Expand Up @@ -336,7 +336,7 @@ public function testExpiredPassword() {
}

public function testChangePasswordForLoggedInUsers() {
$goodResponse = $this->doTestLoginForm('sam@silverstripe.com' , '1nitialPassword');
$goodResponse = $this->doTestLoginForm('testuser@example.com' , '1nitialPassword');

// Change the password
$this->get('Security/changepassword?BackURL=test/back');
Expand All @@ -349,7 +349,7 @@ public function testChangePasswordForLoggedInUsers() {
$this->assertEquals($this->idFromFixture('Member', 'test'), $this->session()->inst_get('loggedInAs'));

// Check if we can login with the new password
$goodResponse = $this->doTestLoginForm('sam@silverstripe.com' , 'changedPassword');
$goodResponse = $this->doTestLoginForm('testuser@example.com' , 'changedPassword');
$this->assertEquals(302, $goodResponse->getStatusCode());
$this->assertEquals(
Controller::join_links(Director::absoluteBaseURL(), 'test/link'),
Expand All @@ -368,9 +368,9 @@ public function testChangePasswordFromLostPassword() {

// Request new password by email
$response = $this->get('Security/lostpassword');
$response = $this->post('Security/LostPasswordForm', array('Email' => 'sam@silverstripe.com'));
$response = $this->post('Security/LostPasswordForm', array('Email' => 'testuser@example.com'));

$this->assertEmailSent('sam@silverstripe.com');
$this->assertEmailSent('testuser@example.com');

// Load password link from email
$admin = DataObject::get_by_id('Member', $admin->ID);
Expand All @@ -390,7 +390,7 @@ public function testChangePasswordFromLostPassword() {
$this->assertEquals($this->idFromFixture('Member', 'test'), $this->session()->inst_get('loggedInAs'));

// Check if we can login with the new password
$goodResponse = $this->doTestLoginForm('sam@silverstripe.com' , 'changedPassword');
$goodResponse = $this->doTestLoginForm('testuser@example.com' , 'changedPassword');
$this->assertEquals(302, $goodResponse->getStatusCode());
$this->assertEquals($this->idFromFixture('Member', 'test'), $this->session()->inst_get('loggedInAs'));

Expand All @@ -408,7 +408,7 @@ public function testRepeatedLoginAttemptsLockingPeopleOut() {

// Login with a wrong password for more than the defined threshold
for($i = 1; $i <= Member::config()->lock_out_after_incorrect_logins+1; $i++) {
$this->doTestLoginForm('sam@silverstripe.com' , 'incorrectpassword');
$this->doTestLoginForm('testuser@example.com' , 'incorrectpassword');
$member = DataObject::get_by_id("Member", $this->idFromFixture('Member', 'test'));

if($i < Member::config()->lock_out_after_incorrect_logins) {
Expand Down Expand Up @@ -438,7 +438,7 @@ public function testRepeatedLoginAttemptsLockingPeopleOut() {
}
}

$this->doTestLoginForm('sam@silverstripe.com' , '1nitialPassword');
$this->doTestLoginForm('testuser@example.com' , '1nitialPassword');
$this->assertNull(
$this->session()->inst_get('loggedInAs'),
'The user can\'t log in after being locked out, even with the right password'
Expand All @@ -448,7 +448,7 @@ public function testRepeatedLoginAttemptsLockingPeopleOut() {
$member = DataObject::get_by_id("Member", $this->idFromFixture('Member', 'test'));
$member->LockedOutUntil = date('Y-m-d H:i:s', time() - 30);
$member->write();
$this->doTestLoginForm('sam@silverstripe.com' , '1nitialPassword');
$this->doTestLoginForm('testuser@example.com' , '1nitialPassword');
$this->assertEquals(
$this->session()->inst_get('loggedInAs'),
$member->ID,
Expand All @@ -460,7 +460,7 @@ public function testRepeatedLoginAttemptsLockingPeopleOut() {

// Login again with wrong password, but less attempts than threshold
for($i = 1; $i < Member::config()->lock_out_after_incorrect_logins; $i++) {
$this->doTestLoginForm('sam@silverstripe.com' , 'incorrectpassword');
$this->doTestLoginForm('testuser@example.com' , 'incorrectpassword');
}
$this->assertNull($this->session()->inst_get('loggedInAs'));
$this->assertContains(
Expand All @@ -469,7 +469,7 @@ public function testRepeatedLoginAttemptsLockingPeopleOut() {
'The user can retry with a wrong password after the lockout expires'
);

$this->doTestLoginForm('sam@silverstripe.com' , '1nitialPassword');
$this->doTestLoginForm('testuser@example.com' , '1nitialPassword');
$this->assertEquals(
$this->session()->inst_get('loggedInAs'),
$member->ID,
Expand All @@ -484,8 +484,8 @@ public function testAlternatingRepeatedLoginAttempts() {

// ATTEMPTING LOG-IN TWICE WITH ONE ACCOUNT AND TWICE WITH ANOTHER SHOULDN'T LOCK ANYBODY OUT

$this->doTestLoginForm('sam@silverstripe.com' , 'incorrectpassword');
$this->doTestLoginForm('sam@silverstripe.com' , 'incorrectpassword');
$this->doTestLoginForm('testuser@example.com' , 'incorrectpassword');
$this->doTestLoginForm('testuser@example.com' , 'incorrectpassword');

$this->doTestLoginForm('noexpiry@silverstripe.com' , 'incorrectpassword');
$this->doTestLoginForm('noexpiry@silverstripe.com' , 'incorrectpassword');
Expand All @@ -499,7 +499,7 @@ public function testAlternatingRepeatedLoginAttempts() {
// BUT, DOING AN ADDITIONAL LOG-IN WITH EITHER OF THEM WILL LOCK OUT, SINCE THAT IS THE 3RD FAILURE IN
// THIS SESSION

$this->doTestLoginForm('sam@silverstripe.com' , 'incorrectpassword');
$this->doTestLoginForm('testuser@example.com' , 'incorrectpassword');
$member1 = DataObject::get_by_id("Member", $this->idFromFixture('Member', 'test'));
$this->assertNotNull($member1->LockedOutUntil);

Expand All @@ -512,16 +512,16 @@ public function testUnsuccessfulLoginAttempts() {
Security::config()->login_recording = true;

/* UNSUCCESSFUL ATTEMPTS WITH WRONG PASSWORD FOR EXISTING USER ARE LOGGED */
$this->doTestLoginForm('sam@silverstripe.com', 'wrongpassword');
$this->doTestLoginForm('testuser@example.com', 'wrongpassword');
$attempt = DataObject::get_one('LoginAttempt', array(
'"LoginAttempt"."Email"' => 'sam@silverstripe.com'
'"LoginAttempt"."Email"' => 'testuser@example.com'
));
$this->assertTrue(is_object($attempt));
$member = DataObject::get_one('Member', array(
'"Member"."Email"' => 'sam@silverstripe.com'
'"Member"."Email"' => 'testuser@example.com'
));
$this->assertEquals($attempt->Status, 'Failure');
$this->assertEquals($attempt->Email, 'sam@silverstripe.com');
$this->assertEquals($attempt->Email, 'testuser@example.com');
$this->assertEquals($attempt->Member(), $member);

/* UNSUCCESSFUL ATTEMPTS WITH NONEXISTING USER ARE LOGGED */
Expand All @@ -541,16 +541,16 @@ public function testSuccessfulLoginAttempts() {
Security::config()->login_recording = true;

/* SUCCESSFUL ATTEMPTS ARE LOGGED */
$this->doTestLoginForm('sam@silverstripe.com', '1nitialPassword');
$this->doTestLoginForm('testuser@example.com', '1nitialPassword');
$attempt = DataObject::get_one('LoginAttempt', array(
'"LoginAttempt"."Email"' => 'sam@silverstripe.com'
'"LoginAttempt"."Email"' => 'testuser@example.com'
));
$member = DataObject::get_one('Member', array(
'"Member"."Email"' => 'sam@silverstripe.com'
'"Member"."Email"' => 'testuser@example.com'
));
$this->assertTrue(is_object($attempt));
$this->assertEquals($attempt->Status, 'Success');
$this->assertEquals($attempt->Email, 'sam@silverstripe.com');
$this->assertEquals($attempt->Email, 'testuser@example.com');
$this->assertEquals($attempt->Member(), $member);
}

Expand Down

0 comments on commit 9146450

Please sign in to comment.