def test_decode_token(self): # Check invalid tokens with self.assertRaises(TokenException): auth_utils.decode_token(None) with self.assertRaises(TokenException): auth_utils.decode_token("asfafsasf1241243124") good_user = { "username": "******", "roles": ["role"] } token = auth_utils.create_token(good_user) try: auth_utils.decode_token(token) except Exception: self.fail("Should not be here") # test modified token not acceptable tokenlist = list(token) if tokenlist[0] != '?': tokenlist[0] = '?' else: tokenlist[0] = 'a' token = "".join(tokenlist) with self.assertRaises(TokenException): auth_utils.decode_token(token)
def test_create_token(self): with self.assertRaises(TokenException): auth_utils.create_token(None) bad_user = {"username": None, "roles": ["cool"]} with self.assertRaises(TokenException): auth_utils.create_token(bad_user) bad_user = {"username": "******", "roles": []} with self.assertRaises(TokenException): auth_utils.create_token(bad_user) bad_user = {"username": "******", "roles": None} with self.assertRaises(TokenException): auth_utils.create_token(bad_user) bad_user = { "username": "******", } with self.assertRaises(TokenException): auth_utils.create_token(bad_user) bad_user = {"roles": ["role"]} with self.assertRaises(TokenException): auth_utils.create_token(bad_user) good_user = {"username": "******", "roles": ["role"]} token = auth_utils.create_token(good_user) self.assertIsNotNone(token) try: tokenuser = auth_utils.decode_token(token) self.assertEqual(good_user["username"], tokenuser["user"]) except TokenException: self.fail()
def test_create_token(self): with self.assertRaises(TokenException): auth_utils.create_token(None) bad_user = { "username": None, "roles": ["cool"] } with self.assertRaises(TokenException): auth_utils.create_token(bad_user) bad_user = { "username": "******", "roles": [] } with self.assertRaises(TokenException): auth_utils.create_token(bad_user) bad_user = { "username": "******", "roles": None } with self.assertRaises(TokenException): auth_utils.create_token(bad_user) bad_user = { "username": "******", } with self.assertRaises(TokenException): auth_utils.create_token(bad_user) bad_user = { "roles": ["role"] } with self.assertRaises(TokenException): auth_utils.create_token(bad_user) good_user = { "username": "******", "roles": ["role"] } token = auth_utils.create_token(good_user) self.assertIsNotNone(token) try: tokenuser = auth_utils.decode_token(token) self.assertEqual(good_user["username"], tokenuser["user"]) except TokenException: self.fail()
def test_decode_token(self): # Check invalid tokens with self.assertRaises(TokenException): auth_utils.decode_token(None) with self.assertRaises(TokenException): auth_utils.decode_token("asfafsasf1241243124") good_user = {"username": "******", "roles": ["role"]} token = auth_utils.create_token(good_user) try: auth_utils.decode_token(token) except Exception: self.fail("Should not be here") # test modified token not acceptable tokenlist = list(token) if tokenlist[0] != '?': tokenlist[0] = '?' else: tokenlist[0] = 'a' token = "".join(tokenlist) with self.assertRaises(TokenException): auth_utils.decode_token(token)