def test_post_new(self): response = self.testapp.post_json( '/api/participants', {'name': 'Rene', 'organization_id': 'Org_Eruditorum'}, ) participant_id = json.loads(response.body)['uid'] self.assertIsNotNone(Participant.get_by_id(participant_id))
def test_get_participant_by_id(self): p = Participant.create( name= 'f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7', organization_id='Organization_12345678', ) p.put() fetched_p = Participant.get_by_id(p.uid) # Test that hashes up to 128 characters will fit. self.assertEqual(fetched_p.name, p.name)
def test_delete_disassociates_participants(self): captain, contact, classroom1, report1, report2 = \ self.create_for_delete() # Create another classroom, so we check that participants remain on it # afterward. classroom2 = Classroom.create( name='Classroom Bar', code='steel snake', team_id=classroom1.team_id, contact_id=contact.uid, num_students=22, ) classroom2.put() ppt_on_one = Participant.create( student_id='on_one', team_id=classroom1.team_id, classroom_ids=[classroom1.uid], ) ppt_on_two = Participant.create( student_id='on_two', team_id=classroom1.team_id, classroom_ids=[classroom1.uid, classroom2.uid], ) Participant.put_multi([ppt_on_one, ppt_on_two]) url = '/api/classrooms/{}'.format(classroom1.uid) headers = self.login_headers(captain) # Delete the classroom. self.testapp.delete(url, headers=headers, status=204) # Both participants are still there, but neither is associated with # the deleted classroom. self.assertEqual( Participant.get_by_id(ppt_on_one.uid).classroom_ids, []) self.assertEqual( Participant.get_by_id(ppt_on_two.uid).classroom_ids, [classroom2.uid])
def test_patch_with_disassociated_participant(self): """If a participant exists with no classrooms, they are found.""" other, teammate, contact, captain, team, classroom, _ = self.create() student_id = 'disassociated' # Add an participant who is disassociated from all classrooms. ppt = Participant.create( team_id=team.uid, classroom_ids=[], student_id=student_id, ) ppt.put() def postBody(ppt, id_modifier=''): ppt = ppt.copy() ppt['student_id'] += id_modifier return {'method': 'POST', 'path': '/api/participants', 'body': ppt} response = self.testapp.patch_json( '/api/participants', [{ 'method': 'POST', 'path': '/api/participants', 'body': { 'team_id': team.uid, 'classroom_id': classroom.uid, 'student_id': student_id, }, }], headers=jwt_headers(contact), ) response_list = json.loads(response.body) # The provided student id matched the disassociated user and the # db used that uid. self.assertEqual(len(response_list), 1) self.assertEqual(response_list[0]['uid'], ppt.uid) self.assertEqual( Participant.get_by_id(ppt.uid).classroom_ids, [classroom.uid], ) # `num_students` has incremented after adding new participant to # classroom. updated_classroom = Classroom.get_by_id(classroom.uid) self.assertEqual( updated_classroom.num_students, classroom.num_students + 1, )
def test_update_remove_from_all(self): other, teammate, contact, captain, team, classroom, ppnt = self.create( ) # Updating to have no classrooms DOES NOT delete the participant and # updates classroom counts. We keep participants even if they're not on # any classrooms so that if they're re-added their uid (which must be # synced with Neptune) remains the same. self.testapp.put_json( '/api/participants/{}'.format(ppnt.uid), {'classroom_ids': []}, headers=jwt_headers(captain), ) fetched_ppnt = Participant.get_by_id(ppnt.uid) self.assertIsNotNone(fetched_ppnt) self.assertEqual(fetched_ppnt.classroom_ids, []) fetched_classroom = Classroom.get_by_id(classroom.uid) self.assertEqual(fetched_classroom.num_students, classroom.num_students - 1)
def test_update_remove_from_multiple_rosters(self): """When ppt is updated to have one fewer classroom id.""" other, teammate, contact, captain, team, classroom, _ = self.create() # Make a second classroom to associate with. classroom2 = Classroom.create( name="Second Classroom", team_id=team.uid, contact_id='User_contact', code="bar", ) # Start with a ppt on two classrooms. ppnt = Participant.create( team_id=team.uid, classroom_ids=[classroom.uid, classroom2.uid], student_id='toremove', ) ppnt.put() # Make sure count of students starts out correct. classroom2.num_students = 1 classroom2.put() # Remove them from just one of their classrooms. response = self.testapp.put_json( '/api/participants/{}'.format(ppnt.uid), {'classroom_ids': [classroom.uid]}, # classroom2 removed headers=jwt_headers(contact), ) # Check they're still on the other classroom. fetched = Participant.get_by_id(ppnt.uid) self.assertEqual(fetched.classroom_ids, [classroom.uid]) # Check that classroom size is updated. fetched_classroom2 = Classroom.get_by_id(classroom2.uid) self.assertEqual(fetched_classroom2.num_students, classroom2.num_students - 1)
def test_update(self): other, teammate, contact, captain, team, classroom, ppnt = self.create( ) # Can't update for other classrooms. for user in (other, teammate): self.testapp.put_json( '/api/participants/{}'.format(ppnt.uid), {'student_id': 'Dave'}, headers=jwt_headers(user), status=403, ) # Success for contacts and captains. for user in (contact, captain): new_first = 'Dave-new' response = self.testapp.put_json( '/api/participants/{}'.format(ppnt.uid), {'student_id': new_first}, headers=jwt_headers(user), ) fetched = Participant.get_by_id(json.loads(response.body)['uid']) self.assertEqual(fetched.student_id, new_first)
def test_patch_batch(self): """custom PATCH handling: add a batch of participants""" (other, teammate, contact, captain, team, classroom, ppnt) = self.create() class1ppt1 = { 'first_name': u'Je\u017cu', 'last_name': u'Kl\u0105tw', 'team_id': team.uid, 'classroom_id': classroom.uid, 'student_id': 'Student_one', } class1ppt2 = { 'first_name': u'Ppt', 'last_name': u'One-Two', 'team_id': team.uid, 'classroom_id': classroom.uid, 'student_id': 'Student_two', } class2ppt1 = { 'first_name': u'Ppt', 'last_name': u'Two-One', 'team_id': team.uid, 'classroom_id': 'Classroom_other', 'student_id': 'Student_other', } def postBody(ppt, id_modifier=''): ppt = ppt.copy() ppt['student_id'] += id_modifier return {'method': 'POST', 'path': '/api/participants', 'body': ppt} # Can't create for other classrooms. for user in (other, teammate): self.testapp.patch_json( '/api/participants', [postBody(class1ppt1)], headers=jwt_headers(user), status=403, ) # Can't create for mixed classrooms. self.testapp.patch_json( '/api/participants', [postBody(class1ppt1), postBody(class2ppt1)], headers=jwt_headers(captain), status=400, ) # Success for contacts and captains. for user in (contact, captain): response = self.testapp.patch_json( '/api/participants', [ postBody(class1ppt1, user.uid), postBody(class1ppt2, user.uid), ], headers=jwt_headers(user), ) response_list = json.loads(response.body) self.assertEqual(len(response_list), 2) fetched1 = Participant.get_by_id(response_list[0]['uid']) fetched2 = Participant.get_by_id(response_list[1]['uid']) self.assertIsNotNone(fetched1) self.assertIsNotNone(fetched2) # `num_students` has incremented after adding new participant to # classroom. updated_classroom = Classroom.get_by_id(classroom.uid) self.assertEqual( updated_classroom.num_students, # two created by contact, and two by captain classroom.num_students + 2 + 2)