Esempio n. 1
0
 def test_update_user_password(self):
     model = UserSQLiteModel()
     self._create_simple_user(model)
     update_kwargs = {
         'username': '******',
         'password': '******',
     }
     model.update_user(**update_kwargs)
Esempio n. 2
0
 def test_update_user(self):
     model = UserSQLiteModel()
     self._create_simple_user(model)
     update_kwargs = {
         'username': '******',
         'is_superuser': True,
         'is_user': False
     }
     model.update_user(**update_kwargs)
Esempio n. 3
0
 def test_operational_error_in_logger_in_update_user(self, m_logger):
     model = UserSQLiteModel()
     self._create_simple_user(model)
     update_kwargs = {
         'username': '******',
         'unknown': 'field'
     }
     # there we are take the message with the text
     # "[ERROR] near "WHERE": syntax error"
     # which means that defined update under "not existed" field
     model.update_user(**update_kwargs)
Esempio n. 4
0
 def test_get_user_by_username_without_id(self):
     model = UserSQLiteModel()
     self._create_simple_user(model)
     user = model.get_user_by_username('testuser')
     self.assertIsNone(user.id)
     self.assertEquals(user.username, 'testuser')
     self.assertEquals(user.password, generate_password_hash('123456'))
     self.assertEquals(user.first_name, 'test')
     self.assertEquals(user.last_name, 'user')
     self.assertTrue(user.is_active)
     self.assertFalse(user.is_superuser)
     self.assertFalse(user.is_staff)
     self.assertTrue(user.is_user)
Esempio n. 5
0
 def test_get_not_existed_user_by_username(self):
     model = UserSQLiteModel()
     self._create_simple_user(model)
     user = model.get_user_by_username('user')
     self.assertIsNone(user.id)
     self.assertEquals(user.username, '')
     self.assertEquals(user.password, '')
     self.assertEquals(user.first_name, '')
     self.assertEquals(user.last_name, '')
     self.assertTrue(user.is_active)
     self.assertFalse(user.is_superuser)
     self.assertFalse(user.is_staff)
     self.assertFalse(user.is_user)
     self.assertTrue(user.is_anonymous)
Esempio n. 6
0
 def test_get_token_by_not_existed_username(self):
     self._set_user_settings()
     token_backend = InMemoryTokenBackend()
     user_model = UserSQLiteModel()
     user = self._create_simple_user(user_model)
     token_backend.save('api', 'my_test_token', user_id=user.id)
     token = token_backend.get_token_by_username('unknown', user.username)
     self.assertEqual(token, {})
Esempio n. 7
0
 def test_search_criteria_required_in_update_user(self):
     model = UserSQLiteModel()
     self._create_simple_user(model)
     update_kwargs = {
         'is_superuser': True
     }
     self.assertRaises(
         SearchCriteriaRequired, model.update_user, **update_kwargs
     )
Esempio n. 8
0
 def test_not_enough_arguments_exception_in_update_user(self):
     model = UserSQLiteModel()
     self._create_simple_user(model)
     update_kwargs = {
         'username': '******'
     }
     self.assertRaises(
         NotEnoughArguments, model.update_user, **update_kwargs
     )
Esempio n. 9
0
class RegisterUser(MethodBasedView):
    user_manager = UserSQLiteModel()

    def post(self, request, *args, **kwargs):
        user = self.user_manager.get_user_by_username(request.data['username'])
        # only anonymous doesn't have username
        if user.is_authenticated:
            message = "User already created."
        else:
            self.user_manager.create_user(**request.data)
            message = "User created successfully."
        return message
Esempio n. 10
0
 def test_create_user_without_password(self):
     model = UserSQLiteModel()
     kwargs = {
         'username': '******',
         'first_name': 'test',
         'last_name': 'user',
         'is_active': True,
         'is_superuser': False,
         'is_staff': False,
         'is_user': True,
     }
     self.assertRaises(
         RequiredModelFieldsNotDefined, model.create_user, **kwargs
     )
Esempio n. 11
0
 def test_get_token_by_username(self):
     self._set_user_settings()
     token_backend = InMemoryTokenBackend()
     user_model = UserSQLiteModel()
     user = self._create_simple_user(user_model)
     token_data = {
         'name': 'api',
         'token': 'my_test_token',
         'user_id': user.id
     }
     token_backend.save(
         token_data['name'],
         token_data['token'],
         user_id=token_data['user_id']
     )
     db_token = token_backend.get_token_by_username('api', user.username)
     self.assertEqual(db_token['name'], token_data['name'])
     self.assertEqual(db_token['token'], token_data['token'])
     self.assertEqual(db_token['user_id'], token_data['user_id'])
Esempio n. 12
0
class LogIn(MethodBasedView):
    token_manager = JSONWebTokenManager()
    token_backend = InMemoryTokenBackend()
    user_manager = UserSQLiteModel()

    def get_or_create_token(self, user, *args, **kwargs):

        def get_token(user):
            return self.token_backend.get_token_by_username(
                'admin', username=user.username
            )

        def create_token(user, *args, **kwargs):
            jwt_kwargs = {
                "iss": "aiorest-ws",
                "exp": 60,
                "name": kwargs.get('username'),
                "authorized": True,
                "token_name": "admin"
            }
            kwargs.update(jwt_kwargs)
            api_token = self.token_manager.generate({}, **kwargs)
            self.token_backend.save('admin', api_token, user_id=user.id)
            return api_token

        return get_token(user) or create_token(user, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        user = self.user_manager.get_user_by_username(
            request.data['username'], with_id=True
        )
        if user.is_authenticated:
            api_token = self.get_or_create_token(user, *args, **kwargs)
        else:
            api_token = None
        return {'token': api_token}
Esempio n. 13
0
 def test_fields(self):
     model = UserSQLiteModel()
     self.assertEqual(model.fields, USER_MODEL_FIELDS)
Esempio n. 14
0
 def test_get_not_existed_token(self):
     self._set_user_settings()
     backend = InMemoryTokenBackend()
     user_model = UserSQLiteModel()  # NOQA
     db_token = backend.get('unknown_token')
     self.assertEqual(db_token, {})
Esempio n. 15
0
 def test_init_with_default_settings(self):
     model = UserSQLiteModel()  # NOQA
Esempio n. 16
0
 def test_init_with_custom_settings(self):
     settings.DATABASES = DATABASES
     settings._create_database_managers()
     model = UserSQLiteModel()  # NOQA
Esempio n. 17
0
 def test_create_user(self):
     model = UserSQLiteModel()
     self._create_simple_user(model)
Esempio n. 18
0
 def test_save(self):
     self._set_user_settings()
     token_backend = InMemoryTokenBackend()
     user_model = UserSQLiteModel()
     user = self._create_simple_user(user_model)
     token_backend.save('api', 'my_test_token', user_id=user.id)
Esempio n. 19
0
 def test_fields_without_pk(self):
     model = UserSQLiteModel()
     self.assertEqual(model.fields_without_pk, USER_MODEL_FIELDS_WITHOUT_PK)