def test_update(test_client: FlaskClient, admin_headers: dict, user_password: str): hash_password = bcrypt.generate_password_hash(user_password) hash_password = hash_password.decode('utf-8') user = User( email='*****@*****.**', password=hash_password, role='admin', active=True, ) session.add(user) session.commit() data = ({ "email": '*****@*****.**', "password": f'{user_password}1', "role": 'client', }) response = test_client.put(f"/api/admin/v1/users/{user.id}/", json=data, headers=admin_headers) actual = response.json['result'] expected = UserClientSchema().dump(user) assert actual == expected
def create_user(role: str) -> User: email = f'{role}@test.com' password = get_password() hash_password = bcrypt.generate_password_hash(password) hash_password = hash_password.decode('utf-8') user = User(email=email, password=hash_password, role=role) session.add(user) session.commit() return user
def test_activate_client(test_client: FlaskClient, admin_headers: dict, user_password: str): hash_password = bcrypt.generate_password_hash(user_password) hash_password = hash_password.decode('utf-8') user = User( email='*****@*****.**', password=hash_password, role='client', active=False, ) session.add(user) session.commit() data = { "email": user.email, "password": user_password, } response = test_client.post('/api/client/v1/login/', json=data) actual = response.status_code expected = 400 assert actual == expected data = { "active": True, } test_client.put(f"/api/admin/v1/users/{user.id}/", json=data, headers=admin_headers) actual = user.active expected = True assert actual == expected data = { "email": user.email, "password": user_password, } response = test_client.post('/api/client/v1/login/', json=data) actual = response.status_code expected = 200 assert actual == expected
def post(self) -> dict: create_params = self.create_params() hash_password = bcrypt.generate_password_hash( create_params["password"]) hash_password = hash_password.decode('utf-8') create_params["password"] = hash_password user = User(**create_params) try: session.add(user) session.commit() user = UserClientSchema().dump(user) response = BasicResponse(user) except IntegrityError: session.rollback() raise BasicError(message='email is already exist.', status=400) return BasicResponseSchema().dump(response)
def put(self, id: int) -> dict: user = session.query(User).get(id) update_params = self.update_params() if update_params.get('password'): hash_password = bcrypt.generate_password_hash( update_params["password"]) hash_password = hash_password.decode('utf-8') update_params["password"] = hash_password for key, value in update_params.items(): setattr(user, key, value) try: session.commit() user = UserClientSchema().dump(user) response = BasicResponse(user) except IntegrityError: session.rollback() raise BasicError(message='email is already exist.', status=400) return BasicResponseSchema().dump(response)