def test_get_data_report_should_return_200_when_file_id_exist(self): data_id = 2 keyword = "test-keyword-2" self.new_data = Data(file_id=self.file_id, id=data_id, keyword=keyword, total_adword=self.total_adword, total_link=self.total_link, total_search_result=self.total_search_result, html_code=self.html_code) self.db_session.add(self.new_data) self.db_session.commit() with app.test_client() as client: result = client.get('/data-report/1') assert result.status_code == 200 expected_data = [[ self.keyword, self.total_adword, self.total_link, self.total_search_result, self.html_code, self.file_id ], [ keyword, self.total_adword, self.total_link, self.total_search_result, self.html_code, self.file_id ]] assert (json.loads(result.data), expected_data)
def test_get_csv_should_return_200_when_correct_authorization_and_have_data_for_user_id_and_status_is_true_when_have_data(self): token = generate_jwt(self.user_id) data_id = 1 keyword = "test-keyword" total_adword = 1 total_link = 1 total_search_result = "about 1,000" html_code = "test-html-code" new_data = Data( file_id = self.file_id, id = data_id, keyword = keyword, total_adword = total_adword, total_link = total_link, total_search_result = total_search_result, html_code = html_code ) self.db_session.add(new_data) self.db_session.commit() with app.test_client() as client: result = client.get( '/csv', headers={"Authorization": token} ) assert result.status_code == 200 expected_result = [ [self.file_id, self.filename, self.keywords, ANY, True] ] assert json.loads(result.data) == expected_result
def test_process_csv_should_return_401_if_wrong_authorization(self): token = jwt.encode({'sub': self.user_id}, "wrong_secret", algorithm='HS256') with app.test_client() as client: result = client.get( '/csv', headers={"Authorization": token} ) assert result.status_code == 401
def test_get_csv_should_return_404_when_correct_authorization_but_no_data_for_user_id(self): user_id = 2 token = generate_jwt(user_id) with app.test_client() as client: result = client.get( '/csv', headers={"Authorization": token} ) assert result.status_code == 404
def test_login_should_return_200_when_login_successfully(self): body = { "email": self.email, "password": self.password } with app.test_client() as client: result = client.post( '/login', json=body ) assert result.status_code == 200
def test_login_should_return_400_when_password_incorrect(self): body = { "email": self.email, "password": "******" } with app.test_client() as client: result = client.post( '/login', json=body ) assert result.status_code == 400
def test_post_user_should_return_201_when_success(self): body = { "email": "[email protected]", "password": self.password } with app.test_client() as client: result = client.post( '/user', json=body ) assert result.status_code == 201
def test_post_user_should_return_400_when_email_exist(self): body = { "email": self.email, "password": self.password } with app.test_client() as client: result = client.post( '/user', json=body ) assert result.status_code == 400
def test_get_csv_should_return_200_when_correct_authorization_and_have_data_for_user_id_and_status_is_false_when_no_data(self): token = generate_jwt(self.user_id) with app.test_client() as client: result = client.get( '/csv', headers={"Authorization": token} ) assert result.status_code == 200 expected_result = [ [self.file_id, self.filename, self.keywords, ANY, False] ] assert json.loads(result.data) == expected_result
def test_get_index_should_return_correctly(self): with app.test_client() as client: result = client.get('/') assert result.status_code == 200 expected_result = { "data_report": "/data-report/<file_id>", "html_code": "/html-code/<file_id>/<keyword>", "index": "/", "login": "******", "process_csv": "/csv", "static": "/static/<path:filename>", "user": "******" } assert json.loads(result.data) == expected_result
def test_post_csv_should_return_200_when_correct_authorization(self): self.cur.execute(""" DELETE FROM data; DELETE FROM file; """) self.cnx.commit() token = generate_jwt(self.user_id) body = { "filename": self.filename, "keywords": [1, 2, 3, 4] } with app.test_client() as client: result = client.post( '/csv', headers={"Authorization": token}, json=body ) assert result.status_code == 200
def test_process_csv_should_return_401_if_no_autorization_headers(self): with app.test_client() as client: result = client.get('/csv') assert result.status_code == 401
def test_get_html_code_should_return_200_when_both_file_id_and_keyword_exist( self): with app.test_client() as client: result = client.get(f'/html-code/{self.file_id}/{self.keyword}') assert result.status_code == 200 assert json.loads(result.data) == self.html_code
def test_get_html_code_should_return_404_when_both_file_id_and_keyword_not_exist( self): with app.test_client() as client: result = client.get(f'/html-code/9999/hello-world') assert result.status_code == 404
def test_get_data_report_should_return_404_when_file_id_not_exist(self): with app.test_client() as client: result = client.get('/data-report/9999') assert result.status_code == 404