Skip to content

Commit

Permalink
authome.tests: add some more bad credential tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Percival committed Jul 27, 2017
1 parent 87e7fa1 commit c28a669
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ omit =
venv2/*
authome/migrations/*
authome/wsgi.py
authome/settings.py
manage.py
41 changes: 38 additions & 3 deletions authome/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class AuthTestCase(TestCase):
client = Client()
home_url = reverse('home')
auth_url = reverse('auth')
auth_ip_url = reverse('auth_ip')
auth_dual_url = reverse('auth_dual')
Expand All @@ -22,6 +23,16 @@ def setUp(self):
def basic_auth(self, username, password):
return 'Basic {}'.format(base64.b64encode('{}:{}'.format(username, password).encode('utf-8')).decode('utf-8'))

# @mock.patch('adal.AuthenticationContext.acquire_token_with_username_password')
# def test_home_redirects(self, mock_api_call):
# mock_api_call.return_value = {
# 'userId': self.email
# }

# response = self.client.get(self.home_url)
# self.assertRedirects


@mock.patch('adal.AuthenticationContext.acquire_token_with_username_password')
def test_auth_adal_with_username(self, mock_api_call):
mock_api_call.return_value = {
Expand Down Expand Up @@ -57,9 +68,32 @@ def test_auth_adal_with_invalid_username(self, mock_api_call):
)
self.assertEqual(response.status_code, 401)

def test_auth_adal_without_creds(self):
def test_auth_adal_with_bad_creds(self):
# no credentials
response = self.client.get(self.auth_url)
self.assertEqual(response.status_code, 401)
# malformed Authorization Header
response = self.client.get(self.auth_url,
HTTP_AUTHORIZATION='Basic'
)
self.assertEqual(response.status_code, 401)
response = self.client.get(self.auth_url,
HTTP_AUTHORIZATION='Not a legit header'
)
self.assertEqual(response.status_code, 401)
response = self.client.get(self.auth_url,
HTTP_AUTHORIZATION='Basic πŸ˜­πŸ˜­πŸ˜­πŸ˜•πŸ˜•πŸ˜•'
)
self.assertEqual(response.status_code, 401)
response = self.client.get(self.auth_url,
HTTP_AUTHORIZATION='Basic ==abcdef/+=='
)
self.assertEqual(response.status_code, 401)
# legit header, but invalid payload
response = self.client.get(self.auth_url,
HTTP_AUTHORIZATION='Basic '+base64.b64encode(b'notlegit').decode('utf-8')
)
self.assertEqual(response.status_code, 401)

@mock.patch('adal.AuthenticationContext.acquire_token_with_username_password')
def test_auth_ip_with_username(self, mock_api_call):
Expand Down Expand Up @@ -116,5 +150,6 @@ def test_auth_dual(self, mock_api_call):
self.assertIn('email', response.json())
self.assertEqual(response.json()['email'], self.email)



def test_auth_dual_without_creds(self):
response = self.client.get(self.auth_dual_url)
self.assertEqual(response.status_code, 200)

0 comments on commit c28a669

Please sign in to comment.