Skip to content

Commit

Permalink
SaslTestCase: Use tearDown() method instead of finally: blocks
Browse files Browse the repository at this point in the history
It's much cleaner
  • Loading branch information
progval committed Sep 5, 2023
1 parent f8dd8d7 commit eb31ed3
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions test/test_irclib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,14 +1496,17 @@ class SaslTestCase(SupyTestCase, CapNegMixin):
def setUp(self):
pass

def tearDown(self):
conf.supybot.networks.test.sasl.username.setValue('')
conf.supybot.networks.test.sasl.password.setValue('')
conf.supybot.networks.test.certfile.setValue('')

def testPlain(self):
try:
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')
self.irc = irclib.Irc('test')
finally:
conf.supybot.networks.test.sasl.username.setValue('')
conf.supybot.networks.test.sasl.password.setValue('')
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')

self.irc = irclib.Irc('test')

self.assertEqual(self.irc.sasl_current_mechanism, None)
self.irc.sasl_next_mechanisms = ['scram-sha-256', 'plain']

Expand Down Expand Up @@ -1531,15 +1534,12 @@ def testPlain(self):
self.endCapNegociation()

def testExternalFallbackToPlain(self):
try:
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')
conf.supybot.networks.test.certfile.setValue('foo')
self.irc = irclib.Irc('test')
finally:
conf.supybot.networks.test.sasl.username.setValue('')
conf.supybot.networks.test.sasl.password.setValue('')
conf.supybot.networks.test.certfile.setValue('')
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')
conf.supybot.networks.test.certfile.setValue('foo')

self.irc = irclib.Irc('test')

self.assertEqual(self.irc.sasl_current_mechanism, None)
self.irc.sasl_next_mechanisms = ['external', 'plain']

Expand Down Expand Up @@ -1567,15 +1567,12 @@ def testExternalFallbackToPlain(self):
self.endCapNegociation()

def testFilter(self):
try:
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')
conf.supybot.networks.test.certfile.setValue('foo')
self.irc = irclib.Irc('test')
finally:
conf.supybot.networks.test.sasl.username.setValue('')
conf.supybot.networks.test.sasl.password.setValue('')
conf.supybot.networks.test.certfile.setValue('')
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')
conf.supybot.networks.test.certfile.setValue('foo')

self.irc = irclib.Irc('test')

self.assertEqual(self.irc.sasl_current_mechanism, None)
self.irc.sasl_next_mechanisms = ['external', 'plain']

Expand All @@ -1597,13 +1594,11 @@ def testFilter(self):
self.endCapNegociation()

def testReauthenticate(self):
try:
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')
self.irc = irclib.Irc('test')
finally:
conf.supybot.networks.test.sasl.username.setValue('')
conf.supybot.networks.test.sasl.password.setValue('')
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')

self.irc = irclib.Irc('test')

self.assertEqual(self.irc.sasl_current_mechanism, None)
self.irc.sasl_next_mechanisms = ['plain']

Expand All @@ -1622,16 +1617,12 @@ def testReauthenticate(self):
self.irc.takeMsg() # None. But even if it was CAP REQ sasl, it would be ok
self.assertEqual(self.irc.takeMsg(), None)

try:
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')
self.irc.feedMsg(ircmsgs.IrcMsg(command='CAP',
args=('*', 'DEL', 'sasl')))
self.irc.feedMsg(ircmsgs.IrcMsg(command='CAP',
args=('*', 'NEW', 'sasl=PLAIN')))
finally:
conf.supybot.networks.test.sasl.username.setValue('')
conf.supybot.networks.test.sasl.password.setValue('')
conf.supybot.networks.test.sasl.username.setValue('jilles')
conf.supybot.networks.test.sasl.password.setValue('sesame')
self.irc.feedMsg(ircmsgs.IrcMsg(command='CAP',
args=('*', 'DEL', 'sasl')))
self.irc.feedMsg(ircmsgs.IrcMsg(command='CAP',
args=('*', 'NEW', 'sasl=PLAIN')))

m = self.irc.takeMsg()
self.assertEqual(m.command, 'CAP', 'Expected CAP, got %r.' % m)
Expand Down

0 comments on commit eb31ed3

Please sign in to comment.