def get_doors_you_manage(request): """ Give template the list of door names that the staff user can manage, or 'None' """ lua = LockUserAdmin(LockUser, AdminSite()) doors_to_show_qs = lua.get_doors_to_show(request) return_val = 'None' # avoiding a test for 'None'... if doors_to_show_qs: return_val = ', '.join([door.name for door in doors_to_show_qs]) return return_val
def test_deactivate(self): """ Deactivating selected lock users (admin action) """ response = self.client.get("/lockadmin/rfid_lock_management/lockuser/") request = response.context['request'] # deactivate all LockUsers lua = LockUserAdmin(LockUser, AdminSite()) queryset = LockUser.objects.all() lua.deactivate(request, queryset) # Issue #x self.assertTrue(LockUser.objects.get(pk=1).is_active()) self.assertTrue(LockUser.objects.get(pk=2).is_active()) # In the fixture, only one LockUser (id 3) is not permitted the same # doors that the staff user is not permitted, so only LockUser 3 would # be deactivated if all are selected. self.assertFalse(LockUser.objects.get(pk=3).is_active())
def test_lockuser_change_form(self): """ Is this the change_form for the right lock user? """ # will be looking at change form for this lockuser object_id = 2 lockuser = LockUser.objects.get(pk=object_id) t_info("Opening browser to get to lockuser's change_form.......", 3) # Issue #y self.browser.get(self.live_server_url + '/lockadmin/rfid_lock_management/lockuser/%d' % object_id) self.browser.maximize_window() # Non-form-field things t_info("check that in div with id='main_form', the first/only " "h2 tag text is: '<h2> Lock user/keycard assignment</h2>", 4) main_form_div = self.browser.find_element_by_id('main_form') first_h2 = main_form_div.find_element_by_tag_name('h2') self.assertEqual(first_h2.text, "Lock user / keycard assignment") # Check the form fields t_info("Check that various form fields that are filled in reflect " "the attributes for the LockUser whose pk is in the url.", 4) t_info("Is the staff user able to see - but not interact with - the " "doors that this lock user has access to, but the staff user " "does not?", 5) # create LockUserAdmin object so we can get other doors lua = LockUserAdmin(LockUser, AdminSite()) client = Client() client.login(username='******', password='******') response = client.get( "/lockadmin/rfid_lock_management/lockuser/%d/" % object_id) request = response.context['request'] actual_other_doors = lua.get_other_doors(request, object_id) door_names = [door.name for door in list(actual_other_doors)] # get the other doors as page displays them actual_other_doors_str = ", ".join(door_names) field = self.browser.find_element_by_id('other_doors') self.assertEqual(field.text, actual_other_doors_str) # test that we see the same thing the admin function would return # Issue #x # (Here specifically: alternative - hardcode actual_doors_str, i.e. # actual_other_doors_str = "Space 1 Space 4" ) t_info("Comparing the value of the relevant text input field " "with the lockuser's actual email.", 4) field = self.browser.find_element_by_name('email') self.assertEqual(field.get_attribute('value'), lockuser.email) field = self.browser.find_element_by_name('first_name') self.assertEqual(field.get_attribute('value'), lockuser.first_name) field = self.browser.find_element_by_name('last_name') self.assertEqual(field.get_attribute('value'), lockuser.last_name) field = self.browser.find_element_by_name('address') self.assertEqual(field.get_attribute('value'), lockuser.address) field = self.browser.find_element_by_name('phone_number') self.assertEqual(field.get_attribute('value'), lockuser.phone_number) field = self.browser.find_element_by_name('phone_number') self.assertEqual(field.get_attribute('value'), lockuser.phone_number) doors_pk_tuples = lockuser.doors.values_list('pk') # make list of str, not list of tuples doors_pk_list = [t[0] for t in doors_pk_tuples] door_checkboxes = self.browser.find_elements_by_css_selector( "input[name='doors']") for door_checkbox in door_checkboxes: if door_checkbox.is_selected(): self.assertIn( int(door_checkbox.get_attribute('value')), doors_pk_list) deact_checkbox = self.browser.find_element_by_css_selector( "input[id='id_deactivate_current_keycard']") self.assertEqual(deact_checkbox.is_selected(), lockuser.deactivate_current_keycard) current_rfid_field = self.browser.find_element_by_css_selector( '.form-row.field-prettify_get_current_rfid') current_rfid_field = current_rfid_field.find_element_by_tag_name('p') self.assertEqual(current_rfid_field.text, lockuser.prettify_get_current_rfid()) last_access_time_field = self.browser.find_element_by_css_selector( '.form-row.field-last_access_time_and_door_and_link_to_more') last_access_field = last_access_time_field.find_element_by_tag_name( 'p') self.assertIn(lockuser.prettify_get_last_access_time(), last_access_time_field.text)