示例#1
0
    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)
示例#2
0
    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()
示例#3
0
    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()
示例#4
0
    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)