Skip to content

Commit

Permalink
IMAP4Server failure response message assertion becomes a method.
Browse files Browse the repository at this point in the history
This de-duplicates redundant assertions.
  • Loading branch information
markrwilliams committed Aug 4, 2017
1 parent 9bf0897 commit fa8aa48
Showing 1 changed file with 27 additions and 37 deletions.
64 changes: 27 additions & 37 deletions src/twisted/mail/test/test_imap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,25 @@ def setUp(self):
self.account = realm.theAccount


def assertClientFailureMessage(self, failure, expected):
"""
Assert that the provided failure is an L{IMAP4Exception} with
the given message.
@param failure: A failure whose value L{IMAP4Exception}
@type failure: L{failure.Failure}
@param expected: The expected failure message.
@type expected: L{bytes}
"""
failure.trap(imap4.IMAP4Exception)
message = str(failure.value)
if _PY3:
expected = repr(expected)

self.assertEqual(message, expected)


def test_customChallengers(self):
"""
L{imap4.IMAP4Server} accepts a L{dict} mapping challenge type
Expand Down Expand Up @@ -3459,17 +3478,9 @@ def test_missingPortal(self):
def auth():
return self.client.authenticate(b'secret')

def authFailed(failure):
failure.trap(imap4.IMAP4Exception)
message = str(failure.value)
expected = b"Temporary authentication failure"
if _PY3:
expected = repr(expected)

self.assertEqual(message, expected)

d = self.connected.addCallback(strip(auth))
d.addErrback(authFailed)
d.addErrback(self.assertClientFailureMessage,
b"Temporary authentication failure")
d.addCallbacks(self._cbStopClient, self._ebGeneral)

return defer.gatherResults([d, self.loopback()])
Expand All @@ -3484,7 +3495,7 @@ def test_challengerRaisesException(self):

@implementer(IChallengeResponse)
class ValueErrorAuthChallenge(object):
message = "A challenge failure"
message = b"A challenge failure"

def getChallenge(self):
raise ValueError(self.message)
Expand Down Expand Up @@ -3521,20 +3532,10 @@ def challengeResponse(self, secret, chal):
def auth():
return self.client.authenticate(b'secret')

def authFailed(failure):
failure.trap(imap4.IMAP4Exception)
message = str(failure.value)

expected = (
"Server error: " + ValueErrorAuthChallenge.message
).encode('ascii')

if _PY3:
expected = repr(expected)
self.assertEqual(message, expected)

d = self.connected.addCallback(strip(auth))
d.addErrback(authFailed)
d.addErrback(self.assertClientFailureMessage,
("Server error: "
+ str(ValueErrorAuthChallenge.message)).encode('ascii'))
d.addCallbacks(self._cbStopClient, self._ebGeneral)

return defer.gatherResults([d, self.loopback()])
Expand Down Expand Up @@ -3620,20 +3621,9 @@ def test_unhandledCredentials(self):
def auth():
return self.client.authenticate(b'secret')

def assertIMAP4Exception(failure):
failure.trap(imap4.IMAP4Exception)
message = str(failure.value)

expected = (
"Authentication failed: server misconfigured"
).encode('ascii')

if _PY3:
expected = repr(expected)
self.assertEqual(message, expected)

d1 = self.connected.addCallback(strip(auth))
d1.addErrback(assertIMAP4Exception)
d1.addErrback(self.assertClientFailureMessage,
b"Authentication failed: server misconfigured")
d1.addCallbacks(self._cbStopClient, self._ebGeneral)
d = defer.gatherResults([self.loopback(), d1])
return d
Expand Down

0 comments on commit fa8aa48

Please sign in to comment.