1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
<?php
class Swift_Transport_Esmtp_Auth_CramMd5AuthenticatorTest extends \SwiftMailerTestCase
{
private $_agent;
public function setUp()
{
$this->_agent = $this->getMockery('Swift_Transport_SmtpAgent')->shouldIgnoreMissing();
}
public function testKeywordIsCramMd5()
{
/* -- RFC 2195, 2.
The authentication type associated with CRAM is "CRAM-MD5".
*/
$cram = $this->_getAuthenticator();
$this->assertEquals('CRAM-MD5', $cram->getAuthKeyword());
}
public function testSuccessfulAuthentication()
{
$cram = $this->_getAuthenticator();
$this->_agent->shouldReceive('executeCommand')
->once()
->with("AUTH CRAM-MD5\r\n", array(334))
->andReturn('334 '.base64_encode('<foo@bar>')."\r\n");
$this->_agent->shouldReceive('executeCommand')
->once()
->with(\Mockery::any(), array(235));
$this->assertTrue($cram->authenticate($this->_agent, 'jack', 'pass'),
'%s: The buffer accepted all commands authentication should succeed'
);
}
public function testAuthenticationFailureSendRsetAndReturnFalse()
{
$cram = $this->_getAuthenticator();
$this->_agent->shouldReceive('executeCommand')
->once()
->with("AUTH CRAM-MD5\r\n", array(334))
->andReturn('334 '.base64_encode('<foo@bar>')."\r\n");
$this->_agent->shouldReceive('executeCommand')
->once()
->with(\Mockery::any(), array(235))
->andThrow(new Swift_TransportException(""));
$this->_agent->shouldReceive('executeCommand')
->once()
->with("RSET\r\n", array(250));
$this->assertFalse($cram->authenticate($this->_agent, 'jack', 'pass'),
'%s: Authentication fails, so RSET should be sent'
);
}
// -- Private helpers
private function _getAuthenticator()
{
return new Swift_Transport_Esmtp_Auth_CramMd5Authenticator();
}
}
|