def test_user_endpoint_method_wrong(self): data = json.dumps({"username": "******", "password": "******"}) response = login(testCase.mock_request(self, "POST", content=data)) authen = response['Authorization'] headers = { "Content-Type": "application/json", "Authorization": str(authen) } endpoint_response = user_endpoint( testCase.mock_request(self, "VIEW", header=headers), "name001") self.assertEquals(endpoint_response.status_code, 405)
def test_user_endpoint_GET_user_not_exist(self): # get with user not exist data = json.dumps({"username": "******", "password": "******"}) response = login(testCase.mock_request(self, "POST", content=data)) authen = response['Authorization'] headers = { "Content-Type": "application/json", "Authorization": str(authen) } endpoint_response = user_endpoint( testCase.mock_request(self, "GET", header=headers), "username002") self.assertEquals(endpoint_response.status_code, 404)
def test_user_endpoint_GET_pass(self): # test GET request # get authen user = User.objects.get(username="******") data = json.dumps({"username": "******", "password": "******"}) response = login(testCase.mock_request(self, "POST", content=data)) authen = response['Authorization'] headers = { "Content-Type": "application/json", "Authorization": str(authen) } endpoint_response = user_endpoint( testCase.mock_request(self, "GET", header=headers), "name001") self.assertEquals(endpoint_response.status_code, 200) self.assertIn(user.username, endpoint_response.content.decode("utf-8"))
def test_user_endpoint_PUT_username_fail(self): # test 401 data = json.dumps({"username": "******", "password": "******"}) response = login(testCase.mock_request(self, "POST", content=data)) authen = response['Authorization'] data = json.dumps({ "firstName": "name001-2", "lastName": "lastname", "birthDate": str(datetime(2000, 12, 30)), "job": "Author" }) headers = { "Content-Type": "application/json", "Authorization": str(authen) } endpoint_response = user_endpoint( testCase.mock_request(self, "PUT", content=data, header=headers), "username002") self.assertEquals(endpoint_response.status_code, 401)
def test_user_endpoint_GET_authorization_fail(self): # get with wrong or no token endpoint_response = user_endpoint(testCase.mock_request(self, "GET"), "name001") self.assertEquals(endpoint_response.status_code, 401)