Skip to content

Commit 6292e52

Browse files
committed
Improves API authentication tests. Relates to ietf-tools#3412. Commit ready for merge.
- Legacy-Id: 19392
1 parent 02b8559 commit 6292e52

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

ietf/ietfauth/tests.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def test_apikey_errors(self):
633633

634634
# bad method
635635
r = self.client.put(key.endpoint, {'apikey':key.hash()})
636-
self.assertEqual(r.status_code, 405)
636+
self.assertContains(r, 'Method not allowed', status_code=405)
637637

638638
# missing apikey
639639
r = self.client.post(key.endpoint, {'dummy':'dummy',})
@@ -643,6 +643,22 @@ def test_apikey_errors(self):
643643
r = self.client.post(key.endpoint, {'apikey':BAD_KEY, 'dummy':'dummy',})
644644
self.assertContains(r, 'Invalid apikey', status_code=403)
645645

646+
# invalid garbage apikey (decode error)
647+
r = self.client.post(key.endpoint, {'apikey':'foobar', 'dummy':'dummy',})
648+
self.assertContains(r, 'Invalid apikey', status_code=403)
649+
650+
# invalid garbage apikey (struct unpack error)
651+
# number of characters in apikey must be divisible by 4
652+
r = self.client.post(key.endpoint, {'apikey':'foob', 'dummy':'dummy',})
653+
self.assertContains(r, 'Invalid apikey', status_code=403)
654+
655+
# invalid apikey (invalidated api key)
656+
unauthorized_url = urlreverse('ietf.api.views.author_tools')
657+
invalidated_apikey = PersonalApiKey.objects.create(
658+
endpoint=unauthorized_url, person=person, valid=False)
659+
r = self.client.post(unauthorized_url, {'apikey': invalidated_apikey})
660+
self.assertContains(r, 'Invalid apikey', status_code=403)
661+
646662
# too long since regular login
647663
person.user.last_login = datetime.datetime.now() - datetime.timedelta(days=settings.UTILS_APIKEY_GUI_LOGIN_LIMIT_DAYS+1)
648664
person.user.save()

0 commit comments

Comments
 (0)