def test_get(self, client): ''' Tests the GET method. Checks that the response status code is 200, and then checks that all of the expected attributes and controls are present, and the controls work. ''' # Invalid route resp = client.get(self.resource_URL(user="******")) assert resp.status_code == 404 body = json.loads(resp.data) assert body["@error"]["@message"] == "Not found" assert str(body["@error"]["@messages"]) == "[\'No user was " \ "found with URI {}\']" \ .format("Jaana1") _check_profile("profile", client, body, "error-profile") # Valid route ./Joonas/ resp = client.get(self.resource_URL(user="******")) assert resp.status_code == 200 body = json.loads(resp.data) assert body["name"] == "Joonas" _check_namespace(client, body) _check_control_get_method("self", client, body) _check_profile("profile", client, body, "user-profile") _check_control_get_method("collection", client, body) _check_control_put_method("edit", client, body, _get_user_json()) _check_control_get_method("cyequ:equipment-owned", client, body)
def test_put(self, client): """ Tests the PUT method. Checks all of the possible erroe codes, and also checks that a valid request receives a 204 response. Also tests that when name is changed, the user can be found from a its new URI. """ valid = utils._get_user_json() validmod = utils._get_modified_user_json() wrong_bank = { "username": "******", "password": "******", "bankAccount": ["en kerros"] } resp = client.put(self.RESOURCE_URL, json=wrong_bank) assert resp.status_code == 404 # test with wrong content type resp = client.put(self.RESOURCE_URL, data=json.dumps(valid)) assert resp.status_code == 415 resp = client.put(self.INVALID_URL, json=valid) assert resp.status_code == 404 # test with another users username valid["username"] = "******" resp = client.put(self.RESOURCE_URL, json=valid) assert resp.status_code == 409 # test with valid (only change model) validmod["password"] = "******" resp = client.put(self.RESOURCE_URL, json=validmod) assert resp.status_code == 204 # remove field for 400 valid.pop("username") resp = client.put(self.RESOURCE_URL, json=valid) assert resp.status_code == 400 valid = utils._get_user_json() resp = client.put(self.RESOURCE_URL, json=valid) resp = client.get(self.MODIFIED_URL) assert resp.status_code == 200 body = json.loads(resp.data) assert body["username"] == valid["username"]
def test_put(self, client): ''' Tests the PUT method. Checks all of the possible error codes, and also checks that a valid request receives a 204 response. Also tests that when name is changed, the sensor can be found from a its new URI. ''' # Invalid route resp = client.get(self.resource_URL(user="******")) assert resp.status_code == 404 body = json.loads(resp.data) assert body["@error"]["@message"] == "Not found" assert str(body["@error"]["@messages"]) == "[\'No user was " \ "found with URI {}\']" \ .format("Jaana1") _check_profile("profile", client, body, "error-profile") # Valid route ./Joonas/ -> ./Jenni/ valid = _get_user_json() # Test for unsupported media type (content-type header) resp = client.put(self.resource_URL(user="******"), data=json.dumps(valid)) assert resp.status_code == 415 # Tests for invalid JSON document resp = client.put(self.resource_URL(user="******"), json="invalid") assert resp.status_code == 400 # Remove required fields valid.pop("name") resp = client.put(self.resource_URL(user="******"), json=valid) assert resp.status_code == 400 # Test with valid content valid = _get_user_json() resp = client.put(self.resource_URL(user="******"), json=valid) assert resp.status_code == 204 # Test changed resp = client.get(self.resource_URL(user="******")) body = json.loads(resp.data) assert body["name"] == valid["name"] # Test existing valid = _get_user_json(name="Janne") resp = client.put(self.resource_URL(user="******"), json=valid) assert resp.status_code == 409
def test_post(self, client): ''' Tests the POST method. Checks all of the possible error codes, and also checks that a valid request receives a 201 response with a location header that leads into the newly created resource. ''' valid = _get_user_json() # Test for unsupported media type (content-type header) resp = client.post(self.RESOURCE_URL, data=json.dumps(valid)) assert resp.status_code == 415 # Test for invalid JSON document resp = client.post(self.RESOURCE_URL, json="invalid") assert resp.status_code == 400 # Remove required fields valid.pop("name") resp = client.post(self.RESOURCE_URL, json=valid) assert resp.status_code == 400 # Test with valid valid = _get_user_json() resp = client.post(self.RESOURCE_URL, json=valid) assert resp.status_code == 201 # Test Location header # Plus 3, because we know the id of newly created user is 3, since # there are initially two users in the db. header_URI = valid["name"].replace(" ", "%20") + "3" assert resp.headers["Location"] \ .endswith(self.RESOURCE_URL + header_URI + "/") # noqa: E501 # Follow location header and test response resp = client.get(resp.headers["Location"]) assert resp.status_code == 200 # See if POSTed exists body = json.loads(resp.data) assert body["name"] == "Jenni" # POST again for 409 resp = client.post(self.RESOURCE_URL, json=valid) assert resp.status_code == 409
def test_post(self, client): """ Tests the POST method. Checks all of the possible error codes, and also checks that a valid request receives a 201 response with a location header that leads into the newly created resource. """ valid = utils._get_user_json() wrong_bank = { "username": "******", "password": "******", "bankAccount": ["en kerros"] } resp = client.post(self.RESOURCE_URL, json=wrong_bank) assert resp.status_code == 404 # test with wrong content type resp = client.post(self.RESOURCE_URL, data=json.dumps(valid)) assert resp.status_code == 415 # test with valid and see that it exists afterward resp = client.post(self.RESOURCE_URL, json=valid) assert resp.status_code == 201 assert resp.headers["Location"].endswith(self.RESOURCE_URL + valid["username"] + "/") resp = client.get(resp.headers["Location"]) assert resp.status_code == 200 body = json.loads(resp.data) assert body["username"] == "user3" assert body["bankAccount"] == ["FI01"] # send same data again for 409 resp = client.post(self.RESOURCE_URL, json=valid) assert resp.status_code == 409 #remove bankAccount fiel for 400 valid.pop("bankAccount") print(valid) resp = client.post(self.RESOURCE_URL, json=valid) assert resp.status_code == 400 # remove username field for 400 valid.pop("username") resp = client.post(self.RESOURCE_URL, json=valid) assert resp.status_code == 400
def test_get(self, client): ''' Tests the GET method. Checks that the response status code is 200, and then checks that all of the expected attributes and controls are present, and the controls work. Also checks that all of the items from the DB population are present, and their controls. ''' resp = client.get(self.RESOURCE_URL) assert resp.status_code == 200 body = json.loads(resp.data) _check_namespace(client, body) _check_control_get_method("self", client, body) _check_control_post_method("cyequ:add-user", client, body, _get_user_json()) assert len(body["items"]) == 2 for item in body["items"]: _check_control_get_method("self", client, item) _check_profile("profile", client, item, "user-profile") assert "name" in item