def post(self): data = marshal(request.get_json(), login_or_register_marshal_model) email = data['email'] password = data['password'] # check for existing user user = User.query.filter_by(email=email).first() if user is not None: return abort(400, 'Account for email {} already exists'.format(email)) # check for valid signup email if not email.endswith('rutgers.edu'): return abort(400, 'Must provide a valid rutgers.edu email address') # check for valid password if len(password) not in range(10, 33): return abort(400, 'Invalid password. Must be between 10 and 32 characters (inclusive)') # finally, create the user and send the verification email user = User(email=email, password=guard.encrypt_password(password)) db.session.add(user) db.session.commit() verification_email.queue(email) return 'OK', 201
def special_deleted_account(app): with app.app_context(): db.session.add(User(email=app.config['DELETED_ACCOUNT_EMAIL'], password='******', is_verified=False)) db.session.commit()
def test_invalid_user_model(app, email, password, is_verified): with app.app_context(): user = User(email=email, password=password, is_verified=is_verified) with pytest.raises(IntegrityError) as excinfo: db.session.add(user) db.session.commit() db.session.rollback() found = User.query.filter_by(email=email).first() assert found is None
def test_valid_comment_model(app): with app.app_context(): title = 'example title content' content = 'example comment content' author = User(email='email', password='******', is_verified=True) course = Course(name='name', offering_unit='1', subject='2', course_number='3') semester = Semester(year=2018, season='fall') category = Category(name='name') db.session.add(author) db.session.add(course) db.session.add(semester) db.session.add(category) db.session.commit() post = Post(title=title, content='example post content', author_id=author.id, semester_id=semester.id, category_id=category.id, course_id=course.id) db.session.add(post) db.session.commit() comment = Comment(content=content, post_id=post.id, author_id=author.id) db.session.add(comment) db.session.commit() found = Comment.query.filter_by(id=comment.id).first() assert found is not None assert found.content == content assert found.timestamp is not None assert found.author.email == author.email
def another_test_user(app): TestUser = namedtuple( 'TestUser', ['email', 'password', 'id', 'auth_headers'] ) with app.app_context(): email = '*****@*****.**' password = '******' user = User(email=email, password=guard.encrypt_password(password), is_verified=True) db.session.add(user) db.session.commit() headers = {'Authorization': 'Bearer %s' % guard.encode_jwt_token(user)} return TestUser(email=email, password=password, id=user.id, auth_headers=headers)
def test_valid_user_model(app, email, password, is_verified): with app.app_context(): user = User(email=email, password=password, is_verified=is_verified) db.session.add(user) db.session.commit() found = User.query.filter_by(email=email).first() assert found is not None # test user properties assert found.email == email assert found.password == password assert found.is_verified == is_verified # test courses relationship # create example courses courses = [] for n in range(5): course = Course(name='n%d' % n, offering_unit='ou%d' % n, subject='s%d' % n, course_number='cn%d' % n) courses.append(course) db.session.add(course) db.session.commit() # add courses to user # thanks to https://stackoverflow.com/a/45047925 user.courses.extend(courses) db.session.commit() assert len(user.courses) == len(courses) for course in courses: assert course in user.courses
def test_valid_post_model(app): with app.app_context(): title = 'example title content' content = 'example post content' is_archived = False due_date = date(2018, 1, 1) author = User(email='email', password='******', is_verified=True) course = Course(name='name', offering_unit='1', subject='2', course_number='3') semester = Semester(year=2018, season='fall') category = Category(name='name') db.session.add(author) db.session.add(course) db.session.add(semester) db.session.add(category) db.session.commit() post = Post(title=title, content=content, is_archived=is_archived, due_date=due_date, author_id=author.id, semester_id=semester.id, category_id=category.id, course_id=course.id) db.session.add(post) db.session.commit() found = Post.query.filter_by(id=post.id).first() assert found is not None # test Post object properties assert found.title == title assert found.content == content assert found.timestamp is not None assert found.is_archived == is_archived assert found.due_date == due_date # test relationships assert found.author.email == author.email assert found.course.name == course.name assert found.semester.year == semester.year assert found.category.name == category.name # test comments comments = [ Comment(content='c%d' % n, post_id=post.id, author_id=author.id) for n in range(0, 5) ] for comment in comments: db.session.add(comment) db.session.commit() assert post.comments is not None assert len(post.comments) == len(comments) for comment in comments: assert comment in post.comments # test cheers users = [User(email='%d' % n, password='******') for n in range(0, 100)] for user in users: db.session.add(user) post.cheers.append(user) db.session.commit() assert post.cheers is not None assert len(post.cheers) == len(users) for cheer in users: assert cheer in post.cheers