From 36aba7b18b7538a315dd1afc3fa40d63971239c6 Mon Sep 17 00:00:00 2001 From: the catalyst Date: Sat, 15 Feb 2020 00:06:00 +0530 Subject: [PATCH 1/2] Add tests for user api endpoints --- .../core/tests/tests_User_endpoint_tests.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 components/core/tests/tests_User_endpoint_tests.py diff --git a/components/core/tests/tests_User_endpoint_tests.py b/components/core/tests/tests_User_endpoint_tests.py new file mode 100644 index 00000000..976bfd00 --- /dev/null +++ b/components/core/tests/tests_User_endpoint_tests.py @@ -0,0 +1,60 @@ +import unittest +import requests +import logging as logger +from flask import g + +headers = { + 'Host': 'localhost:5000', + 'Accept': 'application/json, text/plain, */*', + 'Accept-Language': 'en-US,en;q=0.5', + 'Accept-Encoding': 'gzip, deflate', + 'Content-Type': 'application/x-www-form-urlencoded', + 'Origin': 'http://localhost:3000', +} +user_data = { + "user_name": "user", + "password": "password", + "email": "fdddsfasdsadsasdsdj@dgd.com", + "auth": "0" +} + +regular_user_data = { + "user_name": "reg_user", + "password": "password", + "email": "fdddsfasdsadsasdcsasdj@dgd.com", +} + +correctlogin = "user_name=rand&password=pass" + +url = "http://localhost:5000/api" + +class TestFlaskAPIUsingRequests(unittest.TestCase): + @classmethod + def setUpClass(cls): + super(TestFlaskAPIUsingRequests, cls).setUpClass() + resp = requests.post(url + '/login', correctlogin, headers=headers) + cls.token = resp.headers['token'] + + def test_api_user_login_returns_auth_level(self): + resp = requests.post(url + '/user', json=user_data, + headers={'Content-Type': 'application/json', 'token': self.token, + 'Access-Control-Expose-Headers': 'token', + 'Access-Control-Allow-Origin': 'http://localhost:3000'}) + self.assertEqual(resp.status_code, 200) + + def test_api_regular_user_login_returns_auth_level(self): + resp = requests.post(url + '/regularuser', json=regular_user_data, + headers={'Content-Type': 'application/json'}) + self.assertEqual(resp.status_code, 200) + + def test_api_remove_user_request(self): + resp = requests.delete(url + '/user/user', + headers={'token': self.token, 'Access-Control-Expose-Headers': 'token', + 'Access-Control-Allow-Origin': 'http://localhost:3000'}) + self.assertEqual(resp.status_code, 200) + + +if __name__ == "__main__": + unittest.main() + + From 4fa0511e327b26103419fb5bd819e98ee77c28e3 Mon Sep 17 00:00:00 2001 From: the catalyst Date: Sat, 15 Feb 2020 01:05:17 +0530 Subject: [PATCH 2/2] Fix bug in usermanager.py to allow deletion of users through api endpoint --- components/core/UserManager.py | 2 +- components/core/tests/tests_User_endpoint_tests.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/components/core/UserManager.py b/components/core/UserManager.py index 74d5fe0e..1e73eae1 100644 --- a/components/core/UserManager.py +++ b/components/core/UserManager.py @@ -95,7 +95,7 @@ def remove_user(username): cursor = db.cursor() sql = "DELETE from user WHERE user_name=%s;" try: - cursor.execute(sql, (username)) + cursor.execute(sql, (username,)) db.commit() except MySQLdb.Error as e: db.rollback() diff --git a/components/core/tests/tests_User_endpoint_tests.py b/components/core/tests/tests_User_endpoint_tests.py index 976bfd00..fa4e701a 100644 --- a/components/core/tests/tests_User_endpoint_tests.py +++ b/components/core/tests/tests_User_endpoint_tests.py @@ -14,14 +14,14 @@ user_data = { "user_name": "user", "password": "password", - "email": "fdddsfasdsadsasdsdj@dgd.com", + "email": "user@email.com", "auth": "0" } regular_user_data = { "user_name": "reg_user", "password": "password", - "email": "fdddsfasdsadsasdcsasdj@dgd.com", + "email": "regular_user@email.com", } correctlogin = "user_name=rand&password=pass" @@ -35,14 +35,14 @@ def setUpClass(cls): resp = requests.post(url + '/login', correctlogin, headers=headers) cls.token = resp.headers['token'] - def test_api_user_login_returns_auth_level(self): + def test_api_user_login(self): resp = requests.post(url + '/user', json=user_data, headers={'Content-Type': 'application/json', 'token': self.token, 'Access-Control-Expose-Headers': 'token', 'Access-Control-Allow-Origin': 'http://localhost:3000'}) self.assertEqual(resp.status_code, 200) - def test_api_regular_user_login_returns_auth_level(self): + def test_api_regular_user_login(self): resp = requests.post(url + '/regularuser', json=regular_user_data, headers={'Content-Type': 'application/json'}) self.assertEqual(resp.status_code, 200)