Skip to content

Commit

Permalink
Merge pull request #19 from tomatte/feature/create_test
Browse files Browse the repository at this point in the history
Feature/create test
  • Loading branch information
Wagratom authored Jul 10, 2024
2 parents d835301 + f0f7ede commit 88a0f64
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 227 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
on: [push, pull_request]

jobs:
continue-integration:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: trans
POSTGRES_PASSWORD: trans
POSTGRES_DB: trans
ports:
- "5432:5432"
options: >-
--health-cmd="pg_isready -U trans"
--health-interval=10s
--health-timeout=5s
--health-retries=5
redis:
image: redis:latest
ports:
- "6379:6379"
options: >-
--health-cmd="redis-cli ping"
--health-interval=10s
--health-timeout=5s
--health-retries=5
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'

- name: Install dependencies
run: |
cd backend
pip install -r requirements.txt
- name: Run tests
env:
POSTGRES_USER: trans
POSTGRES_PASSWORD: trans
POSTGRES_DB: trans
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
REDIS_HOST: localhost
REDIS_PORT: 6379
BACKEND_PORT: 8000
run: |
cd backend
bash startDB.sh
python manage.py test users
2 changes: 1 addition & 1 deletion backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
'NAME': env('POSTGRES_DB'),
'USER': env('POSTGRES_USER'),
'PASSWORD': env('POSTGRES_PASSWORD'),
'HOST': '172.25.232.93',
'HOST': env('POSTGRES_HOST'),
'PORT': env('POSTGRES_PORT'),
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/startDB.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ python manage.py migrate --fake sessions zero
python manage.py showmigrations
python manage.py migrate --fake-initial

python manage.py runserver 0.0.0.0:$BACKEND_PORT
# python manage.py runserver 0.0.0.0:$BACKEND_PORT
298 changes: 156 additions & 142 deletions backend/users/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,164 +7,178 @@ class MyUserViewTest(TransactionTestCase):
def setUp(self):
self.client = Client()
self.create_user()

def tearDown(self):
User.objects.all().delete()
self.client.login(username='test_user1', password='12345')

@classmethod
def create_user(self):
users = [
{'username': 'user1', 'password': '12345', 'nickname': 'user1', 'winners': 2, 'losses': 1},
{'username': 'user2', 'password': '12345', 'nickname': 'user2', 'winners': 1, 'losses': 2},
{'username': 'user3', 'password': '12345', 'nickname': 'user3'},
{'username': 'user4', 'password': '12345', 'nickname': 'user4'},
{'username': 'test_user1', 'password': '12345', 'nickname': 'test_user1', 'winners': 2, 'losses': 1, 'avatar': "http://github/wagratom"},
{'username': 'test_user2', 'password': '12345', 'nickname': 'test_user2', 'winners': 1, 'losses': 2},
{'username': 'test_user3', 'password': '12345', 'nickname': 'test_user3'},
{'username': 'test_user4', 'password': '12345', 'nickname': 'test_user4'},
]
for user in users:
User.objects.create_user(**user)

def send_friend_peding_for_test_user1(self):
for user in ['test_user2', 'test_user3', 'test_user4']:
self.client.login(username=f'{user}', password='12345')
response = self.client.post(reverse('add_friend'), {'username': f'test_user1'})
self.assertEqual(response.status_code, 200)

def accpet_friend_peding_for_test_user1(self):
self.client.login(username='test_user1', password='12345')
for user in ['test_user3', 'test_user4']:
response = self.client.post(reverse('response_friend'), {'username': f'{user}', 'status': 'Accepted'})
self.assertEqual(response.status_code, 200)

###############################################
## Route Name: my_user
###############################################
def test_my_user_route(self):
response = self.client.get(reverse('my_user'))
self.assertEqual(response.status_code, 200)
expected_response = {
"username": 'test_user1',
"nickname": 'test_user1',
"avatar": 'http://github/wagratom'
}
self.assertDictEqual(response.json(), expected_response)

def test_my_user_route_erros(self):
#Test invalid method
response = self.client.post(reverse('my_user'))
self.assertEqual(response.status_code, 405)
self.client.login(username='test_user1', password='12345')

#test user not loged
self.client.logout()
response = self.client.get(reverse('my_user'))
self.assertEqual(response.status_code, 401)


###############################################
## Route Name: get_user
###############################################
def test_get_user_route(self):
response = self.client.post(reverse('get_user'), {'username': 'test_user2'})
self.assertEqual(response.status_code, 200)
expected_response = {
"username": 'test_user2',
"nickname": 'test_user2',
"avatar": ''
}
self.assertDictEqual(response.json(), expected_response)

def test_get_user_route_error(self):
## test invalid mehod
response = self.client.get(reverse('get_user'))
self.assertEqual(response.status_code, 405)

## test invalid username
response = self.client.post(reverse('get_user'), {'username': 'fake'})
self.assertEqual(response.status_code, 404)

## test invalid parameter in post
response = self.client.post(reverse('get_user'), {'errado': 'fake'})
self.assertEqual(response.status_code, 400)

###############################################
## Route Name: all_users
###############################################
def test_all_users_route(self):
response = self.client.get(reverse('all_users'))
self.assertEqual(response.status_code, 200)
response_names = [user['nickname'] for user in response.json()]
for name in ['test_user1', 'test_user2', 'test_user3', 'test_user4']:
self.assertIn(name, response_names)

# def test_my_user_view(self):
# self.client.login(username='user1', password='12345')

# response = self.client.get(reverse('my_user'))
# self.assertEqual(response.status_code, 200)
# self.assertEqual(response.json()['name'], 'user1')


# def test_my_user_view_not_logged(self):
# response = self.client.get(reverse('my_user'))
# self.assertEqual(response.status_code, 302)

# def test_get_user_view(self):
# self.client.login(username='user1', password='12345')

# response = self.client.post(reverse('get_user'), {'user_id': 3})
# self.assertEqual(response.status_code, 405)

# response = self.client.get(reverse('get_user'), {'user_id': 3})
# self.assertEqual(response.status_code, 200)
# self.assertEqual(response.json()['nickname'], 'user3')


# def test_all_users_view(self):
# self.client.login(username='user1', password='12345')
# response = self.client.post(reverse('all_users'))
def test_all_users_route_error(self):
## invalid method
response = self.client.post(reverse('all_users'))
self.assertEqual(response.status_code, 405)


##############################################
# Route Name: add_friend
##############################################
def test_add_friend_route(self):
response = self.client.post(reverse('add_friend'), {'username': 'test_user2'})
self.assertEqual(response.status_code, 200)

response = self.client.post(reverse('add_friend'), {'username': 'test_user3'})
self.assertEqual(response.status_code, 200)

def test_add_friend_route_error(self):
## test invalid method
response = self.client.get(reverse('add_friend'))
self.assertEqual(response.status_code, 405)

## test invalid username
response = self.client.post(reverse('add_friend'), {'username': 'fake'})
self.assertEqual(response.status_code, 404)

## test invalid parameter in post
response = self.client.post(reverse('add_friend'), {'not-existend': 'fake'})
self.assertEqual(response.status_code, 400)

##############################################
# Route Name: response_friend ACCEPT
##############################################
def test_response_friend_route(self):
self.send_friend_peding_for_test_user1()
self.accpet_friend_peding_for_test_user1()
response = self.client.get(reverse('get_list_friends'))
self.assertEqual(response.status_code, 200)
response_names = [user['nickname'] for user in response.json()]
self.assertListEqual(['test_user3', 'test_user4'], response_names)
self.assertNotIn('test_user2', response_names)

def test_response_friend_route_erro(self):
##test invalid method
response = self.client.get(reverse('response_friend'))
self.assertEqual(response.status_code, 405)
response = self.client.post(reverse('response_friend'), {'username': 'fake', 'status': 'Accepted'})
self.assertEqual(response.status_code, 404)
response = self.client.post(reverse('response_friend'))
self.assertEqual(response.status_code, 400)
response = self.client.post(reverse('response_friend'), {'invalid': 'test_user2', 'status': 'Accepted'})
self.assertEqual(response.status_code, 400)

##############################################
# Route Name: update_nickname
##############################################
def test_update_nickname_route(self):
response = self.client.post(reverse('uptade_nickname'), {'nickname': 'junin'})
self.assertEqual(response.status_code, 200)

def test_update_nickname_error(self):
## test invalid method
response = self.client.get(reverse('uptade_nickname'))
self.assertEqual(response.status_code, 405)

## test invalid parameter
response = self.client.post(reverse('uptade_nickname'), {'invalid': 'nickname'})
self.assertEqual(response.status_code, 400)


# ##############################################
# # Route Name: update_nickname
# ##############################################
# def test_update_avatar_route(self):
# response = self.client.post(reverse('uptade_avatar'), {'avatar': 'junin'})
# self.assertEqual(response.status_code, 200)
# response_names = [user['nickname'] for user in response.json()]
# for name in ['user1', 'user2', 'user3', 'user4']:
# self.assertIn(name, response_names)


# def test_add_friend_view(self):
# self.client.login(username='user1', password='12345')

# response = self.client.get(reverse('add_friend'))
# def test_update_avatar(self):
# ## test invalid method
# response = self.client.get(reverse('uptade_avatar'))
# self.assertEqual(response.status_code, 405)

# response = self.client.post(reverse('add_friend'), {'friend_id': 3})
# self.assertEqual(response.status_code, 200)

# response = self.client.post(reverse('add_friend'), {'friend_id': 3})
# ## test invalid parameter
# response = self.client.post(reverse('uptade_avatar'), {'invalid': 'avatar'})
# self.assertEqual(response.status_code, 400)


# def test_accpet_friend_request_view(self):
# self.client.login(username='user1', password='12345')
# self.client.post(reverse('add_friend'), {'friend_id': 1})
# self.client.post(reverse('add_friend'), {'friend_id': 2})

# response = self.client.post(reverse('response_friend'), {'friend_id': 1, 'status': 'accepted'})
# self.assertEqual(response.status_code, 200)


# def test_get_request_received_view(self):
# for username in ['user1', 'user3', 'user4']:
# self.client.login(username=username, password='12345')
# self.client.post(reverse('add_friend'), {'friend_id': 2})

# self.client.login(username='user2', password='12345')
# response = self.client.post(reverse(('get_receive_friends')))

# self.assertEqual(response.status_code, 200)
# response_names = [names['from_user__nickname'] for names in response.json()]
# self.assertEqual(len(response_names), 3)

# for name in ['user1', 'user3', 'user4']:
# self.assertIn(name, response_names)


# def test_get_request_send_view(self):
# self.client.login(username='user1', password='12345')
# self.client.post(reverse('add_friend'), {'friend_id': 2})
# self.client.post(reverse('add_friend'), {'friend_id': 3})

# response = self.client.get(reverse('get_pending_friends'))
# response_names = [user['to_user__nickname'] for user in response.json()]

# self.assertEqual(response.status_code, 200)
# self.assertEqual(len(response_names), 2)
# for name in ['user2', 'user3']:
# self.assertIn(name, response_names)


# def test_response_friend_request_view(self):
# self.client.login(username='user3', password='12345')
# self.client.post(reverse('add_friend'), {'friend_id': 1})

# self.client.login(username='user2', password='12345')
# self.client.post(reverse('add_friend'), {'friend_id': 1})

# self.client.login(username='user1', password='12345')
# response = self.client.post(reverse('response_friend'), {'friend_id': 3, 'status': 'accepted'})

# self.assertEqual(response.status_code, 200)


def test_get_list_friends_view(self):
self.client.login(username='user1', password='12345')

#User1 adicionando 3 users
for i in range(2, 5):
self.client.post(reverse('add_friend'), {'friend_id': i})

#User 2 and user 3 accepting
for i in range(2, 4):
self.client.login(username=f'user{i}', password='12345')
self.client.post(reverse('response_friend'), {'friend_id': 1, 'status': 'accepted'})

#Criando partidas
user1 = User.objects.get(id=1)
user2 = User.objects.get(id=2)

match1 = Match.objects.create()
match2 = Match.objects.create()
match3 = Match.objects.create()

MatchPlayer.objects.create(match=match1, user=user1, score=5, winner=True)
MatchPlayer.objects.create(match=match1, user=user2, score=3, winner=False)

MatchPlayer.objects.create(match=match2, user=user1, score=6, winner=True)
MatchPlayer.objects.create(match=match2, user=user2, score=2, winner=False)

MatchPlayer.objects.create(match=match2, user=user1, score=3, winner=False)
MatchPlayer.objects.create(match=match2, user=user2, score=5, winner=True)

self.client.login(username='user1', password='12345')
response = self.client.post(reverse('get_list_friends'), {'user_id': 1})

response_names = [user['friend']['nickname'] for user in response.json()]

for name in ['user2', 'user3']:
self.assertIn(name, response_names)

# self.assertNotIn('user4', response_names)


# def test_update_avatar(self):
# self.client.login(username='user', password='12345')
# self.client.post(reverse('uptade_nickname'), {'avatar': 'avatar.jpg'})



# response = self.client.post(reverse('remove_friend'), {'friend_id': 2})
# self.assertEqual(response.status_code, 200)
Expand Down
Loading

0 comments on commit 88a0f64

Please sign in to comment.