def test_authentication_req(self): tester = app.test_client(self) app.config['TESTING'] = True # valid_credentials = base64.b64encode(b'testuser:testpassword').decode('utf-8') # response = self.app.get('/user', headers={'Authorization': 'Basic ' + valid_credentials}) #data1 = '{“name”: “Shovon”, “balance”: 100}' # response = tester.get('/user7', data = data1) # print("Testing scenario: Cannot get user details without basic authentication") # print("status_code:") # print(response.status_code) # self.assertEqual(response.status_code,200) data = { "email_address": '*****@*****.**', "first_name": 'admin', "password": '******', "last_name": 'jj' } # { # # "email_address": "*****@*****.**", # "first_name": "user", # "id": "f6921112-000d-4df5-937f-68788512c606", # "password": "******", # "last_name": "lname7" # } #response = tester.post("/auth/register", data={"username":, "password": "******"}) response = tester.post("/user", data=json.dumps(data)) print(response.status_code) valid_credentials = base64.b64encode( b'[email protected]:Network!7!').decode('utf-8') response1 = tester.get( '/user', headers={'Authorization': 'Basic ' + valid_credentials}) print(response1.status_code)
def test_read_user(self): tester = app.test_client(self) app.config['TESTING'] = True valid_credentials = base64.b64encode( b'[email protected]:Network!7!').decode('utf-8') response = tester.get( '/user', headers={'Authorization': 'Basic ' + valid_credentials}) print(response) self.assertEqual(response.status_code, 200)
def setUp(self): try: self.connection = psycopg2.connect(host='localhost', dbname='test_db', user='******', password='******', port=5432) except: print("Unable to connect to the database") self.client = app.test_client()
def test_authentication_req(self): tester = app.test_client(self) #valid_credentials = base64.b64encode(b'testuser:testpassword').decode('utf-8') #response = self.app.get('/api/v1/login/', headers={'Authorization': 'Basic ' + valid_credentials}) response = tester.get('/user', method='GET') print( "Testing scenario: Cannot get user details without basic authentication" ) print("status_code:") print(response.status_code) self.assertEqual(response.status_code, 401)
def test_create_user(self): tester = app.test_client(self) app.config['TESTING'] = True data = { "email_address": '*****@*****.**', "first_name": 'admin', "password": '******', "last_name": 'jj' } response = tester.post("/user", data=json.dumps(data)) print(response) self.assertEqual(response.status_code, 200)
def test_update_user(self): tester = app.test_client(self) app.config['TESTING'] = True data = { "email_address": '*****@*****.**', "first_name": 'admin', "password": '******', "last_name": 'jj' } valid_credentials = base64.b64encode( b'[email protected]:Network!7!').decode('utf-8') response = tester.put( '/user', data=json.dumps(data), headers={'Authorization': 'Basic ' + valid_credentials}) print(response) self.assertEqual(response.status_code, 204)
class Test_API: client = app.test_client() @pytest.fixture(autouse=True, scope='session') def setUp(self): app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' db.create_all() yield db db.drop_all() def test_data_in_db_before_addition(self): user = User.query.all() assert len(user) == 0 def test_create(self): url = "/create" headers = { 'Content-Type': "application/json", 'cache-control': "no-cache" } payload = "{\n\t\"username\": \"test01\",\n\t\"password\": \"test\", \n\t\"useremail\":\"[email protected]\"}" response = self.client.post(url, data=payload, headers=headers) assert response.status_code == 200 assert response.json['Message'].strip() == 'Created successfully' def test_data_in_db_after_addition(self): user = User.query.all() assert user[0].id == 1 assert user[0].username == "test01" def test_read(self): url = "/read" headers = { 'Content-Type': "application/json", 'cache-control': "no-cache" } response = self.client.get(url, headers=headers) assert response.status_code == 200 assert response.json['password'].startswith("sha256") def test_update(self): url = "/update/1" headers = { 'Content-Type': "application/json", 'cache-control': "no-cache" } payload = "{\n\t\"username\": \"updated_username\"}" response = self.client.put(url, data=payload, headers=headers) assert response.status_code == 200 assert response.json['Message'] == 'Updated successfully' def test_updated_data_in_db(self): user = User.query.all() assert user[0].id == 1 assert user[0].username == "updated_username" def test_invalid_update(self): url = "/update/1255" headers = { 'Content-Type': "application/json", 'cache-control': "no-cache" } payload = "{\n\t\"username\": \"updated_username\"}" response = self.client.put(url, data=payload, headers=headers) assert response.status_code == 400 assert response.json['Message'].strip() == 'Check your user id' def test_delete(self): url = "/delete/1" headers = { 'Content-Type': "application/json", 'cache-control': "no-cache" } # payload = "{\n\t\"username\": \"updated_username\"}" response = self.client.delete(url, headers=headers) assert response.status_code == 204 def test_delete_in_db(self): user = User.query.all() assert len(user) == 0 def test_invalid_delete(self): url = "/delete/1255" headers = { 'Content-Type': "application/json", 'cache-control': "no-cache" } # payload = "{\n\t\"username\": \"updated_username\"}" response = self.client.delete(url, headers=headers) assert response.status_code == 400 assert response.json['Message'].strip() == 'Check your user id'
class Test_UserAPI: client = app.test_client() @pytest.fixture(autouse=True, scope='session') def setUp(self): app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' db.create_all() yield db db.drop_all() def test_successful_registration(self): url = "/register" payload = "{\n\t\"username\": \"test01\",\n\t\"useremail\": \"[email protected]\",\n\t\"password\": \"test\"}" headers = { 'Content-Type': "application/json", 'cache-control': "no-cache" } response = self.client.post(url, data=payload, headers=headers) assert response.status_code == 200 assert response.json['Message'].strip() == 'Registration successful' def test_failed_registration(self): url = "/register" payload = "{\n\t\"username\": \"test01\",\n\t\"useremail\": \"[email protected]\",\n\t\"password\": \"test\"}" headers = { 'Content-Type': "application/json", 'cache-control': "no-cache" } response = self.client.post(url, data=payload, headers=headers) assert response.status_code == 400 assert response.json['Message'].strip() == 'Check the fields entered' def test_successful__user_login(self): valid_credentials = base64.b64encode(b'test01:test').decode('UTF-8') response = self.client.post( '/login', headers={'Authorization': 'Basic ' + valid_credentials}) assert response.status_code == 200 assert response.json['Message'].strip() == 'Login success' with self.client.session_transaction() as session: assert len(session) == 2 assert session['username'] == 'test01' def test_user_logout(self): url = "/logout" response = self.client.post(url) assert response.status_code == 200 assert response.json['Message'].strip() == 'Logout successful' with self.client.session_transaction() as session: assert len(session) == 1 def test_failed_user_login(self): valid_credentials = base64.b64encode(b'test01:test01').decode('UTF-8') response = self.client.post( '/login', headers={'Authorization': 'Basic ' + valid_credentials}) assert response.status_code == 400 assert response.json['Message'].strip() == 'Could not verify' with self.client.session_transaction() as session: assert len(session) == 1 def test_failed_user_logout(self): url = "/logout" response = self.client.post(url) assert response.status_code == 400 assert response.json['Message'].strip() == 'Already logged out' with self.client.session_transaction() as session: assert len(session) == 1