Exemplo n.º 1
0
    def test_encode_decode_jwt(self):
        """decoding and encoding jwt"""
        data = {constants.USER_IDENTIFIER: "12345678910"}

        with self.app.app_context():
            signed_jwt = encode(data)
            decoded_jwt = decode(signed_jwt)
        self.assertEqual(data, decoded_jwt)
Exemplo n.º 2
0
    def test_authentication_jwt_user_urn_missing_fail(self):
        """Authenticate request with missing user_urn claim"""

        data = {}

        with self.app.app_context():
            signed_jwt = encode(data)
            with self.assertRaises(BadRequest):
                check_jwt(signed_jwt)
Exemplo n.º 3
0
    def test_authenticate_request_with_correct_header_data(self):
        """Authenticate request using authenticate function and with correct header data"""
        expected_res = {'status': "ok"}
        data = {constants.USER_IDENTIFIER: "12345678910",
                "role": "internal"}

        with self.app.app_context():
            signed_jwt = encode(data)
            res = authenticate(headers={'Authorization': signed_jwt})
        self.assertEqual(res, expected_res)
Exemplo n.º 4
0
    def test_authentication_jwt_pass(self):
        """Authenticate request using correct JWT"""
        expected_res = {'status': "ok"}
        data = {constants.USER_IDENTIFIER: "ce12b958-2a5f-44f4-a6da-861e59070a31",
                "role": "internal"}

        with self.app.app_context():
            jwt = encode(data)
            res = check_jwt(jwt)

        self.assertEqual(res, expected_res)
Exemplo n.º 5
0
    def setUp(self):
        """setup test environment"""
        self.app = application.create_app()
        self.client = self.app.test_client()
        self.engine = create_engine(self.app.config['SQLALCHEMY_DATABASE_URI'])

        internal_token_data = {constants.USER_IDENTIFIER: AppTestCase.SPECIFIC_INTERNAL_USER,
                               "role": "internal"}

        external_token_data = {constants.USER_IDENTIFIER: AppTestCase.SPECIFIC_EXTERNAL_USER,
                               "role": "respondent", "claims": [{'business_id': 'f1a5e99c-8edf-489a-9c72-6cabe6c387fc',
                                                                 'surveys': [AppTestCase.BRES_SURVEY]
                                                                 }]
                               }

        with self.app.app_context():
            internal_signed_jwt = encode(internal_token_data)
            external_signed_jwt = encode(external_token_data)

        self.internal_user_header = {'Content-Type': 'application/json', 'Authorization': internal_signed_jwt}
        self.external_user_header = {'Content-Type': 'application/json', 'Authorization': external_signed_jwt}

        self.test_message = {'msg_to': ['0a7ad740-10d5-4ecb-b7ca-3c0384afb882'],
                             'msg_from': AppTestCase.SPECIFIC_INTERNAL_USER,
                             'subject': 'MyMessage',
                             'body': 'hello',
                             'thread_id': "",
                             'collection_case': 'ACollectionCase',
                             'collection_exercise': 'ACollectionExercise',
                             'business_id': 'f1a5e99c-8edf-489a-9c72-6cabe6c387fc',
                             'survey': self.BRES_SURVEY}

        with self.app.app_context():
            database.db.init_app(current_app)
            database.db.drop_all()
            database.db.create_all()
            self.db = database.db

        party.use_mock_service()
        internal_user_service.use_mock_service()
 def token_data(self, value):
     """Token data setter that makes sure that the headers are updated if the token data changes"""
     self._token_data = value
     self._headers['Authorization'] = encode(self._token_data)