def test_edit_story(client: client): # edit story (without auth) response = client.put("/api/story/{0}".format(VARS["story_private"]["id"]), json={"public": True}) assert_json_status(response, Unauthorized.code) # edit story (with auth) response = client.put("/api/story/{0}".format(VARS["story_private"]["id"]), json={"public": True}, headers=with_auth_headers()) assert_json_status(response, OK_STATUS) # check if story is now public response = client.get("/api/story/{0}".format(VARS["story_private"]["id"])) assert_json_status(response, OK_STATUS)
def test_users( self, client, initial_data, # noqa: F811 user1_data, # noqa: F811 basic_auth_admin_headers, basic_auth_user_headers): # noqa: F811 self.client = client self.basic_auth_admin_headers = basic_auth_admin_headers # Create user1 (should fail) res = client.post( "/api/v1/pdnsadmin/users", headers=basic_auth_user_headers, data=json.dumps(user1_data), content_type="application/json", ) assert res.status_code == 401 # Create user1 (as admin) res = client.post( "/api/v1/pdnsadmin/users", headers=basic_auth_admin_headers, data=json.dumps(user1_data), content_type="application/json", ) data = res.get_json(force=True) assert res.status_code == 201 assert len(data) == 1 # Check user user1 = self.check_user(user1_data, data[0]) user1_id = user1["id"] # Update to defaults (should fail) res = client.put( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(user1_data), headers=basic_auth_user_headers, content_type="application/json", ) assert res.status_code == 401 # Delete user (should fail) res = client.delete( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(user1_data), headers=basic_auth_user_headers, content_type="application/json", ) assert res.status_code == 401 # Cleanup (delete user as admin) res = client.delete( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(user1_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204
def test_accounts( self, client, initial_data, # noqa: F811 account_data, # noqa: F811 basic_auth_admin_headers, basic_auth_user_headers): # noqa: F811 self.client = client self.basic_auth_admin_headers = basic_auth_admin_headers # Create account (should fail) res = client.post( "/api/v1/pdnsadmin/accounts", headers=basic_auth_user_headers, data=json.dumps(account_data), content_type="application/json", ) assert res.status_code == 401 # Create account (as admin) res = client.post( "/api/v1/pdnsadmin/accounts", headers=basic_auth_admin_headers, data=json.dumps(account_data), content_type="application/json", ) data = res.get_json(force=True) assert res.status_code == 201 # Check account data = self.check_account(account_data) account_id = data["id"] # Update to defaults (should fail) res = client.put( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(account_data), headers=basic_auth_user_headers, content_type="application/json", ) assert res.status_code == 401 # Delete account (should fail) res = client.delete( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(account_data), headers=basic_auth_user_headers, content_type="application/json", ) assert res.status_code == 401 # Cleanup (delete account as admin) res = client.delete( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(account_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204
def test_put_should_be_successful(client, db): user_not_written.pop("password") user_not_written.pop("phonenumber") user_not_written.pop("birthdate") res = client.put("/users", json=user_not_written) q = db.session.query(User).filter_by(id=7).first() assert res.status_code == 204 assert q.firstname == user_not_written["firstname"] assert q.lastname == user_not_written["lastname"] assert q.fiscalcode == user_not_written["fiscalcode"] assert q.email == user_not_written["email"]
def test_account_users( self, client, initial_data, # noqa: F811 account_data, user1_data, # noqa: F811 basic_auth_admin_headers, basic_auth_user_headers): # noqa: F811 self.client = client self.basic_auth_admin_headers = basic_auth_admin_headers # Create account res = client.post( "/api/v1/pdnsadmin/accounts", headers=basic_auth_admin_headers, data=json.dumps(account_data), content_type="application/json", ) data = res.get_json(force=True) assert res.status_code == 201 # Check account data = self.check_account(account_data) account_id = data["id"] # Create user1 res = client.post( "/api/v1/pdnsadmin/users", headers=basic_auth_admin_headers, data=json.dumps(user1_data), content_type="application/json", ) data = res.get_json(force=True) assert res.status_code == 201 assert len(data) == 1 # Check user user1 = self.check_user(user1_data, data[0]) user1_id = user1["id"] # Assert test account has no users res = client.get( "/api/v1/pdnsadmin/accounts/users/{}".format(account_id), headers=basic_auth_admin_headers, content_type="application/json", ) data = res.get_json(force=True) assert res.status_code == 200 assert data == [] # Link user to account (as user, should fail) res = client.put( "/api/v1/pdnsadmin/accounts/users/{}/{}".format( account_id, user1_id), headers=basic_auth_user_headers, content_type="application/json", ) assert res.status_code == 401 # Link user to account (as admin) res = client.put( "/api/v1/pdnsadmin/accounts/users/{}/{}".format( account_id, user1_id), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 # Unlink user from account (as user, should fail) res = client.delete( "/api/v1/pdnsadmin/accounts/users/{}/{}".format( account_id, user1_id), headers=basic_auth_user_headers, content_type="application/json", ) assert res.status_code == 401 # Unlink user from account (as admin) res = client.delete( "/api/v1/pdnsadmin/accounts/users/{}/{}".format( account_id, user1_id), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 # Cleanup (delete user) res = client.delete( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(user1_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 # Cleanup (delete account) res = client.delete( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(account_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204
def test_accounts( self, client, initial_data, # noqa: F811 account_data, # noqa: F811 basic_auth_admin_headers): # noqa: F811 account_name = account_data["name"] self.client = client self.basic_auth_admin_headers = basic_auth_admin_headers # Create account res = client.post( "/api/v1/pdnsadmin/accounts", headers=basic_auth_admin_headers, data=json.dumps(account_data), content_type="application/json", ) data = res.get_json(force=True) assert res.status_code == 201 # Check account data = self.check_account(account_data) account_id = data["id"] updated = account_data.copy() # Update and check values for upd_key in ["description", "contact", "mail"]: upd_value = "upd-{}".format(account_data[upd_key]) # Update data = {"name": account_name, upd_key: upd_value} res = client.put( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 updated[upd_key] = upd_value # Check data = self.check_account(updated) # Update to defaults res = client.put( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(account_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 # Check account res = client.get( "/api/v1/pdnsadmin/accounts/{}".format(account_name), headers=basic_auth_admin_headers, content_type="application/json", ) data = res.get_json(force=True) assert res.status_code == 200 assert len(data) == 1 data = data[0] account_id = data["id"] for key, value in account_data.items(): assert data[key] == value # Cleanup (delete account) res = client.delete( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(account_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 # Get non-existing account (should fail) data = self.get_account(account_name, status_code=404) # Update non-existing account (should fail) res = client.put( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(account_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 404 # Delete non-existing account (should fail) res = client.delete( "/api/v1/pdnsadmin/accounts/{}".format(account_id), data=json.dumps(account_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 404
def test_users( self, client, initial_data, # noqa: F811 user1_data, # noqa: F811 basic_auth_admin_headers): # noqa: F811 user1name = user1_data["username"] self.client = client self.basic_auth_admin_headers = basic_auth_admin_headers # Create user (user1) res = client.post( "/api/v1/pdnsadmin/users", headers=basic_auth_admin_headers, data=json.dumps(user1_data), content_type="application/json", ) data = res.get_json(force=True) assert res.status_code == 201 assert len(data) == 1 # Check user user1 = self.check_user(user1_data, data[0]) user1_id = user1["id"] updated = user1_data.copy() # Update and check values for upd_key in ["firstname", "lastname", "email"]: upd_value = "upd-{}".format(user1_data[upd_key]) # Update data = {"username": user1name, upd_key: upd_value} res = client.put( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 updated[upd_key] = upd_value # Check data = self.check_user(updated) # Update to defaults res = client.put( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(user1_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 # Check user self.check_user(user1_data) # Cleanup (delete user) res = client.delete( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(user1_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 204 # Get non-existing user (should fail) data = self.get_user(user1name, status_code=404) # Update non-existing user (should fail) res = client.put( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(user1_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 404 # Delete non-existing user (should fail) res = client.delete( "/api/v1/pdnsadmin/users/{}".format(user1_id), data=json.dumps(user1_data), headers=basic_auth_admin_headers, content_type="application/json", ) assert res.status_code == 404