From 27a40f28a713b7dc3ac6a64db99f586963d1e6f0 Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Tue, 19 Oct 2021 12:15:33 -0300 Subject: [PATCH] =?UTF-8?q?Simplificar=20autentica=C3=A7=C3=A3o=20nos=20te?= =?UTF-8?q?stes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/tests.py | 84 ++++++++++++---------------------------------------- 1 file changed, 19 insertions(+), 65 deletions(-) diff --git a/api/tests.py b/api/tests.py index 9260549..ec9209f 100755 --- a/api/tests.py +++ b/api/tests.py @@ -60,7 +60,17 @@ class MainTestCase(TestCase): db.session.add_all(trainers) db.session.add_all(pokemons) db.session.commit() + self.client = app.test_client() + + login = { + "email": "joaooliveira@hotmail.com", + "password": "senha", + } + auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) + self.assert_200(auth) + self.token_joao = auth.get_json()["token"] + return app def test_post_trainer(self): @@ -230,97 +240,55 @@ class MainTestCase(TestCase): self.assert_401(response) def test_post_pokemon(self): - login = { - "email": "joaooliveira@hotmail.com", - "password": "senha", - } - auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) - self.assert_200(auth) - token = auth.get_json()["token"] data = { "name": "Dummy", "level": 2, "pokemon_id": 12 } - response = self.client.post("/trainer/2/pokemon", json=data, headers={"Authorization":token}, follow_redirects=True) + response = self.client.post("/trainer/2/pokemon", json=data, headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_status(response, 201) self.assertIn(b"Dummy", response.data) self.assertIn(b"pokemon_data", response.data) def test_post_pokemon_trainer_not_found(self): - login = { - "email": "joaooliveira@hotmail.com", - "password": "senha", - } - auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) - self.assert_200(auth) - token = auth.get_json()["token"] data = { "name": "Dummy", "level": 2, "pokemon_id": 12 } - response = self.client.post("/trainer/200/pokemon", json=data, headers={"Authorization":token}, follow_redirects=True) + response = self.client.post("/trainer/200/pokemon", json=data, headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_403(response) # adicionando pokemon pra outro trainer def test_post_pokemon_forbidden(self): - login = { - "email": "joaooliveira@hotmail.com", - "password": "senha", - } - auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) - self.assert_200(auth) - token = auth.get_json()["token"] data = { "name": "Dummy", "level": 2, "pokemon_id": 12 } - response = self.client.post("/trainer/1/pokemon", json=data, headers={"Authorization":token}, follow_redirects=True) + response = self.client.post("/trainer/1/pokemon", json=data, headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_403(response) def test_post_pokemon_no_species(self): - login = { - "email": "joaooliveira@hotmail.com", - "password": "senha", - } - auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) - self.assert_200(auth) - token = auth.get_json()["token"] data = { "name": "Dumb", "level": 2, "pokemon_id": 12000 } - response = self.client.post("/trainer/2/pokemon", json=data, headers={"Authorization":token}, follow_redirects=True) + response = self.client.post("/trainer/2/pokemon", json=data, headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_404(response) def test_delete_pokemon_trainer_not_found(self): - login = { - "email": "joaooliveira@hotmail.com", - "password": "senha", - } - auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) - self.assert_200(auth) - token = auth.get_json()["token"] - response = self.client.delete("/trainer/200/pokemon/1", headers={"Authorization":token}, follow_redirects=True) + response = self.client.delete("/trainer/200/pokemon/1", headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_403(response) def test_delete_pokemon_no_auth(self): - login = { - "email": "joaooliveira@hotmail.com", - "password": "senha", - } - auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) - self.assert_200(auth) - token = auth.get_json()["token"] data = { "name": "Dummier", "level": 2, "pokemon_id": 12 } - response = self.client.post("/trainer/2/pokemon", json=data, headers={"Authorization":token}, follow_redirects=True) + response = self.client.post("/trainer/2/pokemon", json=data, headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_status(response, 201) response = self.client.delete("/trainer/2/pokemon/{}".format(response.get_json()["id"]), follow_redirects=True) self.assert_401(response) @@ -337,33 +305,19 @@ class MainTestCase(TestCase): self.assert_404(response) def test_delete_pokemon(self): - login = { - "email": "joaooliveira@hotmail.com", - "password": "senha", - } - auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) - self.assert_200(auth) - token = auth.get_json()["token"] data = { "name": "Dummier", "level": 2, "pokemon_id": 12 } - response = self.client.post("/trainer/2/pokemon", json=data, headers={"Authorization":token}, follow_redirects=True) + response = self.client.post("/trainer/2/pokemon", json=data, headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_status(response, 201) - response = self.client.delete("/trainer/2/pokemon/{}".format(response.get_json()["id"]), headers={"Authorization":token}, follow_redirects=True) + response = self.client.delete("/trainer/2/pokemon/{}".format(response.get_json()["id"]), headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_200(response) # deletando pokemon de outro trainer def test_delete_pokemon_forbidden(self): - login = { - "email": "joaooliveira@hotmail.com", - "password": "senha", - } - auth = self.client.post("/trainer/authenticate", json=login, follow_redirects=True) - self.assert_200(auth) - token = auth.get_json()["token"] - response = self.client.delete("/trainer/1/pokemon/1", headers={"Authorization":token}, follow_redirects=True) + response = self.client.delete("/trainer/1/pokemon/1", headers={"Authorization":self.token_joao}, follow_redirects=True) self.assert_403(response)