Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Felix sprint 2 #6

Merged
merged 4 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflow/django.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Django CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./activmindback
strategy:
max-parallel: 4
matrix:
python-version: [3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run:
python -m pip install --upgrade pip
pip install -r ../requirements.txt
- name: Run Tests
run: python manage.py test
8 changes: 6 additions & 2 deletions activmindback/activmindback/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@
'NAME': 'activmind',
'HOST': 'localhost',
'USER': 'root',
'PASSWORD': 'root'

'PASSWORD': 'root',
'OPTIONS': {
'charset': 'utf8mb4',
'init_command': "SET default_storage_engine=INNODB",
'collation': 'utf8mb4_general_ci',
},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,4 @@ class Migration(migrations.Migration):
name='type',
field=models.IntegerField(blank=True, null=True),
),
migrations.CreateModel(
name='UserInfo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=100)),
('last_name', models.CharField(max_length=100)),
('date_of_birth', models.DateField()),
('address_number', models.IntegerField(null=True)),
('address_street', models.CharField(blank=True, max_length=100)),
('address_city', models.CharField(blank=True, max_length=100)),
('address_postal_code', models.CharField(blank=True, max_length=100)),
('address_country', models.CharField(blank=True, max_length=100)),
('phone_number', models.CharField(blank=True, max_length=100)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
30 changes: 30 additions & 0 deletions activmindback/core/migrations/0017_alter_userinfo_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 5.0.2 on 2024-03-15 18:20

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models

class Migration(migrations.Migration):

dependencies = [
('core', '0015_customuser_user_type'),
]

operations = [
migrations.CreateModel(
name='UserInfo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=100)),
('last_name', models.CharField(max_length=100)),
('date_of_birth', models.DateField()),
('address_number', models.IntegerField(null=True)),
('address_street', models.CharField(blank=True, max_length=100)),
('address_city', models.CharField(blank=True, max_length=100)),
('address_postal_code', models.CharField(blank=True, max_length=100)),
('address_country', models.CharField(blank=True, max_length=100)),
('phone_number', models.CharField(blank=True, max_length=100)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
20 changes: 20 additions & 0 deletions activmindback/core/migrations/0018_alter_userinfo_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.0.2 on 2024-03-16 01:57

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0017_alter_userinfo_table'),
]

operations = [
migrations.AlterField(
model_name='userinfo',
name='user',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='user_info', to=settings.AUTH_USER_MODEL),
),
]
23 changes: 11 additions & 12 deletions activmindback/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ def create_user(self, email, password= None, **extra_fields):
user.save(using=self._db)
return user

def is_valid_password(self, password):


min_length = 8
# regex = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&])(?=.*\d)[A-Za-z\d@$!%*?&]{" + str(int(min_length)) + r",}$"
regex = r"^[^\s]{8,}$"
password_regex = re.compile(regex)
def is_valid_password(self, password):
#min_length = 8
# regex = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&])(?=.*\d)[A-Za-z\d@$!%*?&]{" + str(int(min_length)) + r",}$"
regex = r"^[^\s]{8,}$"
password_regex = re.compile(regex)

return re.match(password_regex, password) is not None
return re.match(password_regex, password) is not None

def create_superuser(self,email,password,**extra_fields):
user = self.create_user(email=email,password=password,**extra_fields)
Expand Down Expand Up @@ -69,6 +67,10 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []

@property
def user_info(self):
return self.userinfo_set.first()

class Meta:
verbose_name = _('user')
Expand All @@ -84,7 +86,7 @@ def create_auth_token(sender, instance=None, created=False, **kwargs):


class UserInfo(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, unique=True, related_name='user_info')

first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
Expand All @@ -95,9 +97,6 @@ class UserInfo(models.Model):
address_postal_code = models.CharField(max_length=100, blank=True)
address_country = models.CharField(max_length=100, blank=True)
phone_number = models.CharField(max_length=100, blank=True)

class Meta:
db_table = 'users_userinfo'

def __str__(self):
return f"{self.user.email}'s info"
Expand Down
6 changes: 6 additions & 0 deletions activmindback/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
django
mysqlclient
djangorestframework
django-debug-toolbar
drf-nested-routers
django-cors-headers
58 changes: 57 additions & 1 deletion activmindback/users/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
from django.test import TestCase

# Create your tests here.

from django.test import TestCase
from rest_framework.test import APIClient
from rest_framework import status
from core.models import CustomUser as User

class CreateUserTest(TestCase):
def setUp(self):
self.client = APIClient()
self.user = User.objects.create_user(email='[email protected]', password='password1234')

def test_first(self):
self.assertEqual(1, 1)

def test_create_user(self):
# Pour l'instant on ne teste que la création d'un utilisateur pas du user_info
user_data = {
'email': '[email protected]',
'password': 'password123',
'user_type': 'A',
'first_name': 'John',
'last_name': 'Doe',
'date_of_birth': '1990-01-01',
# Autres champs requis pour la création de l'utilisateur...
}

# Envoyer une requête POST pour créer un nouvel utilisateur
response = self.client.post('/register/', user_data, format='json')

self.assertEqual(response.status_code, status.HTTP_201_CREATED)

user = User.objects.get(email='[email protected]')

self.assertIsNotNone(user)
self.assertEqual(response.data['email'], user.email)
self.assertTrue(user.check_password('password123'))
self.assertEqual(user.user_type, 'A')
self.assertEqual(user.user_info.first_name, 'John')
self.assertEqual(user.user_info.last_name , 'Doe')
self.assertEqual(str(user.user_info.date_of_birth), '1990-01-01')
# TODO: Add more assertions to test additional fields in user_info


def test_login(self):
url = '/auth/login/'
data = {'email': '[email protected]', 'password': 'password1234'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn('token', response.data)

def test_logout(self):
# Login the user first
self.client.login(username='[email protected]', password='password1234')
url = '/auth/logout/'
response = self.client.post(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['message'], 'Logged out successfully')
Loading