Пример #1
0
 def test_crud(self):
     with self.app_context():
         user = UserModel("test_username", "test_password")
         self.assertIsNone(user.find_by_username("test_username"))
         self.assertIsNone(user.find_by_id(1))
         user.save_to_db()
         self.assertIsNotNone(user.find_by_username("test_username"))
         self.assertIsNotNone(user.find_by_id(1))
Пример #2
0
 def test_register_user(self):
     with self.app() as client:
         with self.app_context():
             response = client.post('/register', data={'username': "******", 'password': "******"})
             self.assertEqual(response.status_code, 201)
             self.assertIsNotNone(UserModel.find_by_username("test_username"))
             self.assertDictEqual({"message": "User created successfully"}, json.loads(response.data))
Пример #3
0
 def post(self):
     data = UserRegister.parser.parse_args()
     if UserModel.find_by_username(data['username']):
         return {"message": "A user with that name already exists"}, 400
     user = UserModel(**data)
     user.save_to_db()
     return {"message": "User created successfully"}, 201
Пример #4
0
def authenticate(username, password):
    user = UserModel.find_by_username(username)
    if user and safe_str_cmp(user.password, password):
        return user