-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60adeb7
commit d43fb19
Showing
23 changed files
with
346 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# backend/app/api/signup.py | ||
|
||
from flask import request, jsonify | ||
from flask_login import login_user | ||
from app import db | ||
from . import api | ||
from app.models.user import User | ||
|
||
@api.route('/signup', methods=['POST']) | ||
def signup(): | ||
# Check if there is already a user registered | ||
if User.query.first() is not None: | ||
return jsonify({'error': 'Only one user is allowed. Please login!'}), 400 | ||
|
||
data = request.get_json() | ||
if not data: | ||
return jsonify({'error': 'Invalid input'}), 400 | ||
|
||
email = data.get('email') | ||
password = data.get('password') | ||
password2 = data.get('password2') | ||
|
||
errors = {} | ||
|
||
if not email: | ||
errors['email'] = 'Email is required.' | ||
if not password: | ||
errors['password'] = 'Password is required.' | ||
if password != password2: | ||
errors['password2'] = 'Passwords do not match.' | ||
if User.query.filter_by(email=email).first(): | ||
errors['email'] = 'Please use a different email address.' | ||
|
||
if errors: | ||
return jsonify({'errors': errors}), 400 | ||
|
||
user = User(email=email) | ||
user.set_password(password) | ||
db.session.add(user) | ||
db.session.commit() | ||
login_user(user, remember=True) | ||
return jsonify({'success': True}), 201 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from app.tests.base import BaseTestCase | ||
from app import db | ||
from app.models.user import User | ||
|
||
class SignupTestCase(BaseTestCase): | ||
def init_user(self): | ||
pass | ||
|
||
def test_successful_signup(self): | ||
"""Test signing up with valid data.""" | ||
response = self.client.post('/api/signup', json={ | ||
'email': 'test@example.com', | ||
'password': 'password123', | ||
'password2': 'password123' | ||
}) | ||
# self.assertEqual(response.status_code, 201) | ||
data = response.get_json() | ||
self.assertTrue(data.get('success')) | ||
|
||
# Ensure the user is created in the database | ||
user = User.query.filter_by(email='test@example.com').first() | ||
self.assertIsNotNone(user) | ||
self.assertTrue(user.check_password('password123')) | ||
|
||
def test_signup_with_missing_data(self): | ||
"""Test signup with missing email and password fields.""" | ||
response = self.client.post('/api/signup', json={}) | ||
self.assertEqual(response.status_code, 400) | ||
data = response.get_json() | ||
self.assertEqual(data['error'], 'Invalid input') | ||
|
||
def test_signup_with_password_mismatch(self): | ||
"""Test signup when passwords do not match.""" | ||
response = self.client.post('/api/signup', json={ | ||
'email': 'test@example.com', | ||
'password': 'password123', | ||
'password2': 'differentpassword' | ||
}) | ||
self.assertEqual(response.status_code, 400) | ||
data = response.get_json() | ||
self.assertIn('errors', data) | ||
self.assertIn('password2', data['errors']) | ||
self.assertEqual(data['errors']['password2'], 'Passwords do not match.') | ||
|
||
def test_signup_only_one_user(self): | ||
"""Test signup with an email that is already in use.""" | ||
# Create an existing user | ||
user = User(email='test@example.com') | ||
user.set_password('password123') | ||
db.session.add(user) | ||
db.session.commit() | ||
response = self.client.post('/api/signup', json={ | ||
'email': 'test@example.com', | ||
'password': 'newpassword', | ||
'password2': 'newpassword' | ||
}) | ||
self.assertEqual(response.status_code, 400) | ||
data = response.get_json() | ||
self.assertIn('error', data) | ||
self.assertEqual(data['error'], 'Only one user is allowed. Please login!') | ||
|
||
def test_signup_with_invalid_input(self): | ||
"""Test signup with invalid JSON input.""" | ||
response = self.client.post('/api/signup', data="Not a JSON") | ||
self.assertEqual(response.status_code, 415) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
backend/migrations/versions/c355701b0e9a_remove_username.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""remove username | ||
Revision ID: c355701b0e9a | ||
Revises: 701d50ac6812 | ||
Create Date: 2024-09-25 21:00:04.138591 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'c355701b0e9a' | ||
down_revision = '701d50ac6812' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('user', schema=None) as batch_op: | ||
batch_op.drop_column('username') | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('user', schema=None) as batch_op: | ||
batch_op.add_column(sa.Column('username', sa.VARCHAR(length=150), nullable=True)) | ||
|
||
# ### end Alembic commands ### |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.