| 1 |
# Copyright (c) 2016, Menno Smits |
| 2 |
# Released subject to the New BSD License |
| 3 |
# Please see http://en.wikipedia.org/wiki/BSD_licenses |
| 4 |
|
| 5 |
from imapclient.exceptions import LoginError |
| 6 |
|
| 7 |
from .imapclient_test import IMAPClientTest |
| 8 |
|
| 9 |
|
| 10 |
class TestPlainLogin(IMAPClientTest): |
| 11 |
def assert_authenticate_call(self, expected_auth_string): |
| 12 |
authenticate = self.client._imap.authenticate |
| 13 |
self.assertEqual(authenticate.call_count, 1) |
| 14 |
auth_type, auth_func = authenticate.call_args[0] |
| 15 |
self.assertEqual(auth_type, "PLAIN") |
| 16 |
self.assertEqual(auth_func(None), expected_auth_string) |
| 17 |
|
| 18 |
def test_simple(self): |
| 19 |
self.client._imap.authenticate.return_value = ("OK", [b"Success"]) |
| 20 |
result = self.client.plain_login("user", "secret") |
| 21 |
self.assertEqual(result, b"Success") |
| 22 |
self.assert_authenticate_call("\0user\0secret") |
| 23 |
|
| 24 |
def test_fail(self): |
| 25 |
self.client._imap.authenticate.return_value = ("NO", [b"Boom"]) |
| 26 |
self.assertRaises(LoginError, self.client.plain_login, "user", "secret") |
| 27 |
|
| 28 |
def test_with_authorization_identity(self): |
| 29 |
self.client._imap.authenticate.return_value = ("OK", [b"Success"]) |
| 30 |
result = self.client.plain_login("user", "secret", "authid") |
| 31 |
self.assertEqual(result, b"Success") |
| 32 |
self.assert_authenticate_call("authid\0user\0secret") |