Esempio n. 1
0
    def test_logout_uncorrect_token(self):
        with self.app.app_context():
            account = Account.find_by_username('buba')
        token = createAccessToken(account.id, account.role)[1:10]
        headers = {'Authorization': f'Bearer {token}'}

        resp = self.client.get("/api/auth/logout", headers=headers)

        self.assertEqual(resp.status_code, 401)
Esempio n. 2
0
    def test_token_required_should_RevokedTokenError(self):
        token = createAccessToken(1, "role")
        authHeader = f'Bearer {token}'
        headers = {'Authorization': authHeader}

        resp = self.client.get("/api/auth/logout", headers=headers)
        self.assertEqual(resp.status_code, 200)

        with self.app.app_context():
            self.assertRaises(RevokedTokenError, _token_required, authHeader)
Esempio n. 3
0
    def test_token_required_valid_token(self):
        token = createAccessToken(1, "role")
        authHeader = f"Baerer {token}"

        with self.app.app_context():
            encoded_token, payload = _token_required(authHeader)

        self.assertEqual(token, encoded_token)
        self.assertEqual(payload['account_id'], 1)
        self.assertEqual(payload['role'], "role")
Esempio n. 4
0
    def test_register_malformed_request(self):
        token = createAccessToken(1, 'admin')
        headers = {'Authorization': f'Bearer {token}'}
        data = {
            'username': '******',
            'password': '******',
        }

        resp = self.client.post("/api/auth/register",
                                json=data,
                                headers=headers)
        resp_data = resp.get_json()

        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp_data['error'], 'malformed_request')
Esempio n. 5
0
    def test_register_user_already_exista(self):
        token = createAccessToken(1, 'admin')
        headers = {'Authorization': f'Bearer {token}'}
        data = {
            'username': '******',
            'password': '******',
            'email': '*****@*****.**',
            'role': 'user'
        }

        resp = self.client.post("/api/auth/register",
                                json=data,
                                headers=headers)
        resp_data = resp.get_json()

        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp_data['error'], "username_already_exists")
Esempio n. 6
0
    def test_register_invalid_role(self):
        token = createAccessToken(1, 'user')
        headers = {'Authorization': f'Bearer {token}'}
        data = {
            'username': '******',
            'password': '******',
            'email': '*****@*****.**',
            'role': 'user'
        }

        resp = self.client.post("/api/auth/register",
                                json=data,
                                headers=headers)
        resp_data = resp.get_json()

        self.assertEqual(resp.status_code, 403)
        self.assertEqual(resp_data['error'], 'not_allowed')
Esempio n. 7
0
    def test_createAccessToken(self):
        access_token = createAccessToken(1, "role")

        self.assertIsNotNone(access_token)