def test_create_admin(cli_runner, db, request): first_name = "admin first name" last_name = "admin last name" email = "*****@*****.**" password = "******" input_stream = "" input_stream += first_name + "\n" # First name input_stream += last_name + "\n" # Last name input_stream += email + "\n" # Email input_stream += password + "\n" # Password input_stream += password + "\n" # Password confirmation cli_runner.invoke(create_admin, input=input_stream) user = UserModel.find_by_email("*****@*****.**") assert user is not None assert user.firstName == first_name assert user.lastName == last_name assert check_password_hash(pwhash=user.password, password=password) def teardown(): user.delete() request.addfinalizer(teardown)
def add_participant(workshop_id, data) -> (dict, int): workshop = WorkshopModel.find_by_id(workshop_id=workshop_id) # Check if given workshop_id exists in DB if workshop is None: raise EntityNotFoundError # Deserialize data schema = ParticipantSchema() data, err_msg, err_code = schema.loads_or_400(data) if err_msg: return err_msg, err_code # Check if there is already a participant with this email in DB user = UserModel.find_by_email(data.get("email")) if user is None: participant = UserModel( email=data.get("email"), firstName=data.get("firstName"), lastName=data.get("lastName"), role=[Roles.PARTICIPANT.value], workshopParticipations=[workshop_id], ) status = WorkshopParticipantStatus.CREATED.value else: # If user already exists, check if it's a participant if Roles.PARTICIPANT.value in user.role: # Raise error if participant already registred in workshop if user.userId in [p.user.id for p in workshop.participants]: raise InvalidDataError( msg="Participant already registered for this workshop" ) status = WorkshopParticipantStatus.EXISTING.value else: # add participant role otherwise user.add_participant_role() status = WorkshopParticipantStatus.CREATED.value participant = user participant.workshopParticipations.append(workshop_id) # Append participant to workshop workshop.participants.append( WorkshopParticipantModel(user=participant.userId, status=status) ) # Persist in DB participant.save() workshop.save() return schema.dump(participant), 200
def test_post_coaches_with_valid_data(client, auth, admin, data, request): headers = auth.login(email="*****@*****.**") response = client.post("/api/v1/coaches", headers=headers, data=json.dumps(data)) assert response.status_code == 200 assert UserModel.find_by_email(data["email"]) is not None def teardown(): coach = UserModel.find_by_email(data["email"]) coach.delete() request.addfinalizer(teardown)
def forgotten_password(data: bytes) -> (dict, int): # Deserialize data data, err_msg, err_code = ForgottenPasswordSchema().loads_or_400(data) if err_msg: return err_msg, err_code # Get user from email user = UserModel.find_by_email(email=data["email"]) if user is None: raise EmailNotFoundError user.send_reset_password_mail() return None, 204
def create_coach(data: bytes) -> (dict, int): schema = CoachSchema() data, err_msg, err_code = schema.loads_or_400(data) if err_msg: return err_msg, err_code role = data.pop("role") # Check if user already exist user = UserModel.find_by_email(data["email"]) if user is not None: if Roles.COACH.value in user.role: # raise exception if user is already a coach raise UserAlreadyExistsError else: # Otherwise, update role user.role.append(Roles.COACH.value) else: user = UserModel(**data) user.role = [Roles.COACH.value] # Setting admin role if needed if role == "admin": user.role.append(Roles.ADMIN.value) # Encrypt password if user.password and user.password != "": user.password = generate_password_hash(user.password) # Create user in DB user.save() # Generate default action card batches default_action_card_batches = ActionCardBatchModel.find_default_batches() for default_action_card_batch in default_action_card_batches: ActionCardBatchModel( coachId=user.id, name=default_action_card_batch.name, actionCardIds=default_action_card_batch.actionCardIds, type=default_action_card_batch.type, ).save() return schema.dump(user), 200
def login(data: bytes) -> (dict, int): # Deserialize data data, err_msg, err_code = LoginSchema().loads_or_400(data) if err_msg: return err_msg, err_code user = UserModel.find_by_email(email=data["email"]) if user is None: raise EmailNotFoundError if not check_password_hash(pwhash=user.password, password=data["password"]): raise InvalidPasswordError # Generate access token access_token = create_access_token(identity=user.id, fresh=True) output_data = {"access_token": access_token} return output_data, 200
def teardown(): coach = UserModel.find_by_email(data["email"]) coach.delete()