-
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.
Merge pull request #179 from fga-eps-mds/feature/157-link-homepage-pr…
…odutor #157 Linkar Backend com a homepage do Produtor
- Loading branch information
Showing
5 changed files
with
135 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from base64 import b64decode, b64encode | ||
|
||
def encode_string(decoded_string): | ||
'''Codifica uma string pra base64''' | ||
b_str = decoded_string.encode('utf-8') | ||
b_str = b64encode(b_str) | ||
encoded_strings = b_str.decode('utf-8') | ||
return encoded_strings | ||
|
||
def decode_string(encoded_string): | ||
'''Decodifica uma string em base64''' | ||
b_str = encoded_string.encode('utf-8') | ||
b_str = b64decode(b_str) | ||
decoded_strings = b_str.decode('utf-8') | ||
return decoded_strings |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from rest_framework.test import APITestCase | ||
|
||
from .models import Productor | ||
from ..customer.models import Customer | ||
from ..encode import encode_string | ||
|
||
class ProductorRegisterAPIViewTestCase(APITestCase): | ||
def setUp(self): | ||
|
@@ -165,6 +167,96 @@ def test_only_productors_are_listed(self): | |
self.url_list, | ||
**self.auth_token | ||
) | ||
|
||
self.assertEqual(response.status_code, 200, msg='Falha na listagem de produtores') | ||
self.assertEqual(len(response.data), 3, msg='Quantidade de produtores errada') | ||
|
||
class ProductorRetrieveAPIViewTestCase(APITestCase): | ||
def create_user(self): | ||
self.user_data = { | ||
"username": "Marcelo", | ||
"email": "[email protected]", | ||
"password": "teste" | ||
} | ||
|
||
url_signup = '/signup/customer/' | ||
|
||
response = self.client.post( | ||
url_signup, | ||
{'user': self.user_data}, | ||
format='json' | ||
) | ||
|
||
self.assertEqual(response.status_code, 201, msg='Falha na criação de usuário') | ||
|
||
def create_productors(self): | ||
productors_data = [ | ||
{ | ||
'username': 'João', | ||
'email': '[email protected]', | ||
'password': 'teste' | ||
}, | ||
{ | ||
'username': 'Mario', | ||
'email': '[email protected]', | ||
'password': 'teste' | ||
} | ||
] | ||
|
||
url_signup = '/signup/productor/' | ||
|
||
for prod_data in productors_data: | ||
response = self.client.post( | ||
url_signup, | ||
{'user': prod_data}, | ||
format='json' | ||
) | ||
self.assertEqual(response.status_code, 201, msg='Falha na criação de outros produtores') | ||
|
||
def create_tokens(self): | ||
user_cred = {'email': self.user_data['email'], 'password': self.user_data['password']} | ||
|
||
url_token = '/login/' | ||
|
||
response = self.client.post( | ||
url_token, | ||
user_cred, | ||
format='json' | ||
) | ||
|
||
self.assertEqual(response.status_code, 200, msg='Credenciais inválidas') | ||
|
||
self.creds = {'HTTP_AUTHORIZATION': 'Bearer ' + response.data['access']} | ||
|
||
def setUp(self): | ||
self.create_user() | ||
self.create_productors() | ||
self.create_tokens() | ||
self.url_retrieve = '/productor/retrieve/' | ||
|
||
def tearDown(self): | ||
Productor.objects.all().delete() | ||
Customer.objects.all().delete() | ||
|
||
def test_retrieve_productor(self): | ||
email_query = '[email protected]' | ||
url_retrieve_productor = self.url_retrieve + encode_string(email_query) + '/' | ||
|
||
response = self.client.get( | ||
url_retrieve_productor, | ||
**self.creds | ||
) | ||
|
||
self.assertEqual(response.status_code, 200, msg='Falha na obtenção de produtor específico') | ||
self.assertEqual(response.data['username'], 'João', msg='Dados de produtor estão incoerentes') | ||
|
||
def test_invalid_email(self): | ||
email_query = '[email protected]' | ||
url_retrieve_productor = self.url_retrieve + encode_string(email_query) + '/' | ||
|
||
response = self.client.get( | ||
url_retrieve_productor, | ||
**self.creds | ||
) | ||
|
||
self.assertEqual(response.status_code, 404, msg='Retornando produtor com email inválido') |
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