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
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))
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))
def setUp(self): super(TestItem, self).setUp() with self.app() as client: with self.app_context(): # get the JWT token UserModel("souvik", "1234").save_to_db() auth_response = client.post( "/auth", data=json.dumps({ "username": "******", "password": "******" }), headers={"Content-Type": "application/json"}) auth_token = json.loads(auth_response.data)["access_token"] #self.access_token = "JWT {}".format(auth_token) self.access_token = f"JWT {auth_token}"
def test_create_user(self): user = UserModel("test_username", "test_password") self.assertEqual(user.username, "test_username") self.assertEqual(user.password, "test_password")
def authenticate(username, password): user = UserModel.find_by_username(username) if user and safe_str_cmp(user.password, password): return user
def identity(payload): user_id = payload["identity"] return UserModel.find_by_id(user_id)