def test_create_staff(self): staff = self.NEW_STAFF_DATA with self.app.app_context(): # verify query results staff_id = staff_q.create(staff) self.assertEqual(staff_id, 2) self.assertEqual(len(staff_q.get_all()), 2) # verify new staff's details staff_details = staff_q.get_details(staff_id) self.assertTrue(staff) self.assertEqual(staff_details["first_name"], "Ted") self.assertEqual(staff_details["certification"], "RBT") self.assertEqual(staff_details["mon"], 1) self.assertEqual(staff_details["monday"], "08:00-18:00") self.assertEqual(staff_details["supervisor"], 1) # test that client team was setup staff_clients = staff_q.get_clients(staff_id) self.assertEqual(staff_clients[0]["initials"], "AS") self.assertEqual(staff_clients[1]["initials"], "LS") # test staff creation fail """ This function assumes validation of data has already taken place """
def edit_staff(staff_id): # ensure staff exits staff = staff_q.is_found(staff_id) if not staff: flash("Staff member not found.") return redirect(url_for("staff.staff_index")) errors = [] # handle staff-client team deletions if request.method == "DELETE": status = 1 error_message = "Failed to delete client from staff's team." # delete team id if exists team_id = util.safe_int(request.json.get("staff_cli_team_id")) if team_id and staff_q.del_team(team_id): status = 0 # flash message on error if status != 0: flash(error_message) return json.dumps({"status": status}) # handle updates to staff information if request.method == "POST": # update staff data staff_data = util.parse_staff_data(request.form) errors.extend(staff_val.validate_staff(staff_data, True)) if not errors: errors.extend(staff_q.update(staff_data, staff_id)) # get arbitary number of client initials client_count = util.safe_int(request.form.get("client_count")) client_initials = [] for i in range(client_count): client_initials.append(request.form.get("client_initials" + str(i))) # create new staff-client teams errors.extend(staff_q.create_teams(staff_id, client_initials)) if not errors: flash("All Staff Information Updated.") # render page staff = staff_q.get_details(staff_id) clients = staff_q.get_clients(staff_id) supervisors = superv_q.get_all_names() return render_template( "staff/edit_staff.html", clients=clients, supervisors=supervisors, staff=staff, errors=errors, )
def staff_details(staff_id): # get staff details staff_details = staff_q.get_details(staff_id) if not staff_details: flash("Staff Member not found") return redirect(url_for("staff.staff_index")) supervisor = superv_q.get_details(staff_details["supervisor"]) clients = staff_q.get_clients(staff_id) return render_template("staff/staff_details.html", staff_details=staff_details, supervisor=supervisor, clients=clients)
def test_create_new_teams(self): # test exceptions with self.assertRaises(TypeError): staff_q.create_teams("1", ["AB", "DC"]) with self.assertRaises(TypeError): staff_q.create_teams(1, None) with self.assertRaises(ValueError): staff_q.create_teams(0, ["AB", "DC"]) with self.app.app_context(): # create new staff member staff = self.NEW_STAFF_DATA staff["clients"] = [] staff_id = staff_q.create(self.NEW_STAFF_DATA) staff = staff_q.get_details(staff_id) self.assertEqual(staff_id, 2) self.assertEqual(staff["first_name"], "Ted") self.assertFalse(staff_q.get_clients(staff_id)) # add a client to staff's team self.assertTrue(staff_q.create_team(staff_id, 1)) # add or ignore multiple "clients" errors = staff_q.create_teams( staff_id, [None, "", "FakeClient", "FakeClient2", "LS", "AS"]) expected_errors = [ "Client with Initials FakeClient does not exist.", "Client with Initials FakeClient2 does not exist.", ] self.assertEqual(errors, expected_errors) self.assertTrue(staff_q.get_team_id(staff_id, 2)) self.assertEqual(len(staff_q.get_clients(staff_id)), 2) # test with an invalid staff_id staff_id = 5 errors = staff_q.create_teams( staff_id, ["", "FakeClient", "FakeClient2", "LS", "AS"]) expected_errors = [ "Client with Initials FakeClient does not exist.", "Client with Initials FakeClient2 does not exist.", "One or more client teams failed to create.", ] self.assertEqual(errors, expected_errors)
def test_get_staff_details(self): with self.app.app_context(): # preform query staff_details = staff_q.get_details(self.TEST_STAFF_ID) # verify query results self.assertTrue(staff_details) self.assertEqual(staff_details["supervisor"], self.TEST_SUPERVISOR_ID) self.assertEqual(staff_details["first_name"], "Mark") self.assertEqual(staff_details["monday"], "08:30-17:30") # test empty query result self.assertIsNone(staff_q.get_details(100)) # test errors with self.assertRaises(TypeError): staff_q.get_details("1") with self.assertRaises(ValueError): staff_q.get_details(0)
def test_delete_staff(self): with self.client: # ensure login is required response = self.client.post( "staff/delete", data={"staff_id": "1"}, follow_redirects=True, ) self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/auth/login") # login as staff response = self.login("test_user", "test_password") self.assertEqual(response.status_code, 200) # ensure admin is required response = self.client.post( "staff/delete", data={"staff_id": "1"}, follow_redirects=True, ) self.assertEqual(response.status_code, 403) # login as admin response = self.login("admin", "Admin12345") self.assertEqual(response.status_code, 200) ### Test failed delete ### # incorrect request response = self.client.get("/staff/delete") self.assertEqual(response.status_code, 405) # missing required variable response = self.client.post( "/staff/delete", data={}, follow_redirects=True, ) self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/staff/") self.assertIn(b"Invalid Staff Data.", response.data) # non existant staff member response = self.client.post( "/staff/delete", data={"staff_id": 100}, follow_redirects=True, ) self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/staff/") self.assertIn(b"Staff does not exist.", response.data) # incorrect argument response = self.client.post( "/staff/delete", data={"staff_id": "Notanumber"}, follow_redirects=True, ) self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/staff/") self.assertIn(b"Invalid Staff Data.", response.data) # test staff is in database self.assertTrue(staff_q.is_found(1)) self.assertEqual(len(staff_q.get_clients(1)), 1) # test successful delete response = self.client.post( "staff/delete", data={"staff_id": "1"}, follow_redirects=True, ) # test page renders correctly self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/staff/") self.assertIn(b"Staff Member Deleted.", response.data) # test data is no longer in database self.assertFalse(staff_q.is_found(1)) self.assertEqual(staff_q.get_clients(1), []) self.assertIsNone(staff_q.get_details(1)) db = database.connect() result = db.execute("SELECT * FROM staff_hours WHERE staff_id = ?", (1, )).fetchone() self.assertIsNone(result)
def test_edit_staff(self): with self.client: # ensure login is required response = self.client.get("/staff/edit/1", follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/auth/login") # login response = self.login("test_user", "test_password") self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/") self.assertIn(b"What is Achieve?", response.data) """ Test GET requests """ response = self.client.get("/staff/edit/1") self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/staff/edit/1") self.assertIn(b"Edit Staff", response.data) self.assertIn(b"Mark", response.data) self.assertIn(b"Hammel", response.data) self.assertIn(b"LS", response.data) self.assertIn(b"Becky", response.data) self.assertIn(b"08:30", response.data) self.assertIn(b"17:30", response.data) response = self.client.get("/staff/edit/100", follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertEqual(request.path, "/staff/") self.assertIn(b"Staff member not found.", response.data) self.assertIn(b"All Staff", response.data) response = self.client.get("/staff/edit/string") self.assertEqual(response.status_code, 404) response = self.client.get("/staff/edit/!#@") self.assertEqual(response.status_code, 404) response = self.client.get("/staff/edit/") self.assertEqual(response.status_code, 404) """ Test DELETE requests """ self.assertTrue(staff_q.get_team_id(1, 1)) json_data = {"staff_cli_team_id": 1} response = self.client.delete("/staff/edit/1", json=json_data) self.assertEqual(response.status_code, 200) self.assertEqual(b'{"status": 0}', response.data) self.assertIsNone(staff_q.get_team_id(1, 1)) database.init() json_data = {"staff_cli_team_id": ""} response = self.client.delete("/staff/edit/1", json=json_data) self.assertEqual(response.status_code, 200) self.assertEqual(b'{"status": 1}', response.data) self.assertTrue(staff_q.get_team_id(1, 1)) json_data = {} response = self.client.delete("/staff/edit/1", json=json_data) self.assertEqual(response.status_code, 200) self.assertEqual(b'{"status": 1}', response.data) self.assertTrue(staff_q.get_team_id(1, 1)) ### Test POST Requests ### """ Test successful POST """ # verify original data current_staff = staff_q.get_details(1) self.assertEqual(current_staff["first_name"], "Mark") self.assertEqual(current_staff["last_name"], "Hammel") self.assertEqual(current_staff["fri"], 1) # update data response = self.client.post("/staff/edit/1", data=self.NEW_STAFF_DATA) self.assertEqual(response.status_code, 200) self.assertIn(b"All Staff Information Updated.", response.data) # verify database has been updated current_staff = staff_q.get_details(1) self.assertEqual(current_staff["first_name"], "Ted") self.assertEqual(current_staff["last_name"], "George") self.assertEqual(current_staff["fri"], 0) self.assertEqual(current_staff["friday"], "08:00-18:00") clients = staff_q.get_clients(1) self.assertEqual(clients[0]["initials"], "LS") self.assertEqual(clients[1]["initials"], "AS") supervisor = superv_q.get_details(current_staff["supervisor"]) self.assertEqual(supervisor["first_name"], "Katie") # verify data is rendered on page self.assertIn(b"Ted", response.data) self.assertIn(b"George", response.data) self.assertIn(b"BT", response.data) self.assertIn(b"Katie Frank", response.data) self.assertIn(b"Red", response.data) self.assertIn(b"LS", response.data) self.assertIn(b"AS", response.data) self.assertIn(b"08:00", response.data) self.assertIn(b"18:00", response.data) # reset db database.init() """ Test failed POST """ # verify original data current_staff = staff_q.get_details(1) self.assertEqual(current_staff["first_name"], "Mark") self.assertEqual(current_staff["last_name"], "Hammel") self.assertEqual(current_staff["fri"], 1) self.assertEqual(current_staff["friday"], "08:30-17:30") self.assertEqual(current_staff["supervisor"], 1) # POST invalid data response = self.client.post("/staff/edit/1", data=self.INVALID_STAFF_DATA) self.assertEqual(response.status_code, 200) # verify database contains no change current_staff = staff_q.get_details(1) self.assertEqual(current_staff["first_name"], "Mark") self.assertEqual(current_staff["last_name"], "Hammel") self.assertEqual(current_staff["fri"], 1) self.assertEqual(current_staff["friday"], "08:30-17:30") self.assertEqual(current_staff["supervisor"], 1) clients = staff_q.get_clients(1) self.assertEqual(clients[0]["initials"], "LS") self.assertEqual(len(clients), 1) # verify page renders correctly self.assertNotIn(b"All Staff Information Updated.", response.data) self.assertIn(b"First and last name are required", response.data) self.assertIn(b"Full name must be less then 64 characters", response.data) self.assertIn(b"Invalid RBT certification data.", response.data) self.assertIn(b"Invalid Tier data.", response.data) self.assertIn(b"Invalid category data.", response.data) self.assertIn(b"Supervisor with id: 100 not found.", response.data) self.assertIn(b"Invalid attendance data.", response.data) self.assertIn(b"Invalid time format: (08:00-) for: mon", response.data) self.assertIn(b"Start time cannot be later then end time for: tue", response.data) self.assertIn(b"Invalid time format: (-18:00) for: wed", response.data) self.assertIn(b"Invalid time format: (08:00-24:00) for: fri", response.data) self.assertIn( b"Staff must be absent on thu since their hours are empty", response.data)
def test_add_staff(self): with self.client: # check that login is required response = self.client.get("/staff/add") self.assertEqual(response.status_code, 302) # login response = self.login("test_user", "test_password") self.assertEqual(response.status_code, 200) self.assertIn(b"What is Achieve?", response.data) # check that admin is required response = self.client.get("/staff/add") self.assertEqual(response.status_code, 403) # logut response = self.client.get("/auth/logout") self.assertEqual(response.status_code, 302) # login as admin response = self.login("admin", "Admin12345") self.assertEqual(response.status_code, 200) self.assertIn(b"What is Achieve?", response.data) # check template renders response = self.client.get("/staff/add") self.assertEqual(response.status_code, 200) self.assertIn(b"<h1>Add Staff</h1>", response.data) self.assertIn(b"<legend>Basic Info</legend>", response.data) self.assertIn(b"<legend>Create Staff Schedule</legend>", response.data) # check successful staff creation response = self.client.post("/staff/add", data=self.NEW_STAFF_DATA) self.assertEqual(response.status_code, 200) self.assertIn(b"Staff Member Added", response.data) # verify staff member was added to the database staff = staff_q.get_details(2) self.assertEqual(staff["first_name"], "Ted") self.assertEqual(staff["color"], 3) self.assertEqual(staff["monday"], "08:00-18:00") clients = staff_q.get_clients(2) self.assertEqual(len(clients), 2) self.assertEqual(clients[0]["initials"], "LS") self.assertEqual(clients[1]["initials"], "AS") # test that errors are correctly renderes response = self.client.post("/staff/add", data=self.INVALID_STAFF_DATA) self.assertEqual(response.status_code, 200) self.assertIn(b"Staff Creation Failed", response.data) self.assertIn(b"First and last name are required", response.data) self.assertIn(b"Full name must be less then 64 characters", response.data) self.assertIn(b"Invalid RBT certification data.", response.data) self.assertIn(b"Invalid Tier data.", response.data) self.assertIn(b"Invalid category data.", response.data) self.assertIn(b"Supervisor with id: 100 not found.", response.data) self.assertIn(b"Client with initials 'ZZ' was not found", response.data) self.assertIn(b"Client with initials 'XZ' was not found", response.data) self.assertIn(b"Invalid attendance data.", response.data) self.assertIn(b"Invalid time format: (08:00-) for: mon", response.data) self.assertIn(b"Start time cannot be later then end time for: tue", response.data) self.assertIn(b"Invalid time format: (-18:00) for: wed", response.data) self.assertIn(b"Invalid time format: (08:00-24:00) for: fri", response.data) self.assertIn( b"Staff must be absent on thu since their hours are empty", response.data) # test that no new staff was created self.assertIsNone(staff_q.get_details(3))
def test_update_staff(self): with self.app.app_context(): # NOTE: assumes validation occured previously # confirm current staff info (sanity check) staff = staff_q.get_details(self.TEST_STAFF_ID) self.assertEqual(staff["first_name"], "Mark") # change staff data staff = self.NEW_STAFF_DATA del staff["clients"] staff["supervisor"] = 2 staff["cert"] = "BT" staff["attendance"]["mon"] = 0 # update database info self.assertEqual(staff_q.update(staff, self.TEST_STAFF_ID), []) staff = staff_q.get_details(self.TEST_STAFF_ID) self.assertEqual(staff["first_name"], "Ted") self.assertEqual(staff["last_name"], "George") self.assertEqual(staff["certification"], "BT") self.assertEqual(staff["tier"], 1) self.assertEqual(staff["color"], 3) self.assertEqual(staff["supervisor"], 2) self.assertEqual(staff["mon"], 0) self.assertEqual(staff["tue"], 1) self.assertEqual(staff["wed"], 1) self.assertEqual(staff["thu"], 1) self.assertEqual(staff["fri"], 0) self.assertEqual(staff["monday"], "08:00-18:00") self.assertEqual(staff["tuesday"], "08:00-18:00") self.assertEqual(staff["wednesday"], "08:00-18:00") self.assertEqual(staff["thursday"], "08:00-18:00") self.assertEqual(staff["friday"], "08:00-18:00") # reset changes database.init() # test sql error staff = self.NEW_STAFF_DATA staff["first_name"] = None result = staff_q.update(staff, self.TEST_STAFF_ID) staff = staff_q.get_details(self.TEST_STAFF_ID) # confirm lack of database changes self.assertEqual(result, ["Staff Information update failed."]) self.assertEqual(staff["first_name"], "Mark") self.assertEqual(staff["last_name"], "Hammel") self.assertEqual(staff["certification"], "RBT") self.assertEqual(staff["tier"], 2) self.assertEqual(staff["color"], 2) self.assertEqual(staff["supervisor"], 1) self.assertEqual(staff["mon"], 1) self.assertEqual(staff["tue"], 0) self.assertEqual(staff["wed"], 1) self.assertEqual(staff["thu"], 1) self.assertEqual(staff["fri"], 1) self.assertEqual(staff["monday"], "08:30-17:30") self.assertEqual(staff["tuesday"], "08:30-17:30") self.assertEqual(staff["wednesday"], "08:30-17:30") self.assertEqual(staff["thursday"], "08:30-17:30") self.assertEqual(staff["friday"], "08:30-17:30")