def test_audit_using_shelf_location(self, mock_logging, mock_xsrf_token): request = shelf_messages.ShelfAuditRequest( shelf_request=shelf_messages.ShelfRequest(location='NYC'), device_identifiers=self.device_identifiers) response = self.service.audit(request) assert mock_xsrf_token.call_count == 1 mock_logging.assert_called() for identifier in self.device_identifiers: datastore_device = device_model.Device.get( serial_number=identifier) self.assertEqual(datastore_device.shelf.get().location, 'NYC') self.assertFalse(self.shelf.audit_requested) self.assertEqual(self.shelf.last_audit_by, loanertest.SUPER_ADMIN_EMAIL) self.assertIsInstance(response, message_types.VoidMessage)
def test_list_shelves_with_search_constraints(self): expressions = shared_messages.SearchExpression(expression='location') expected_response = shelf_messages.ListShelfResponse(shelves=[ shelf_messages.Shelf(location=self.shelf.location, shelf_request=shelf_messages.ShelfRequest( location=self.shelf.location, urlsafe_key=self.shelf.key.urlsafe())) ], total_results=1, total_pages=1) request = shelf_messages.Shelf( query=shared_messages.SearchRequest(query_string='location:NYC', expressions=[expressions], returned_fields=['location'])) response = self.service.list_shelves(request) self.assertEqual(response, expected_response)
def test_audit_remove_devices(self, mock_get_shelf, mock_model_device_search): shelf = self.device2_key.get() shelf.shelf = self.shelf.key shelf.put() mock_model_device_search.return_value = (search.SearchResults( results=[ search.ScoredDocument(doc_id=self.device2_key.urlsafe()), search.ScoredDocument(doc_id=self.device3_key.urlsafe()), search.ScoredDocument(doc_id=self.device4_key.urlsafe()) ], number_found=3)) mock_get_shelf.return_value = self.shelf request = shelf_messages.ShelfAuditRequest( shelf_request=shelf_messages.ShelfRequest( location=self.shelf.location), device_identifiers=[self.device3_key.get().serial_number]) self.service.audit(request) self.assertEqual(self.device3_key.get().shelf, self.shelf.key) self.assertEqual(self.device2_key.get().shelf, None) self.assertEqual(self.device4_key.get().shelf, None)
def list_shelves(self, request): """Lists enabled or all shelves based on any shelf attribute.""" self.check_xsrf_token(self.request_state) if request.page_size <= 0: raise endpoints.BadRequestException( 'The value for page_size must be greater than 0.') query, sort_options, returned_fields = ( search_utils.set_search_query_options(request.query)) if not query: query = search_utils.to_query(request, shelf_model.Shelf) offset = search_utils.calculate_page_offset( page_size=request.page_size, page_number=request.page_number) search_results = shelf_model.Shelf.search( query_string=query, query_limit=request.page_size, offset=offset, sort_options=sort_options, returned_fields=returned_fields) total_pages = search_utils.calculate_total_pages( page_size=request.page_size, total_results=search_results.number_found) shelves_messages = [] for document in search_results.results: message = search_utils.document_to_message(document, shelf_messages.Shelf()) message.shelf_request = shelf_messages.ShelfRequest() message.shelf_request.urlsafe_key = document.doc_id message.shelf_request.location = message.location shelves_messages.append(message) return shelf_messages.ListShelfResponse( shelves=shelves_messages, total_results=search_results.number_found, total_pages=total_pages)
def setUp(self): super(ApiUtilsTest, self).setUp() self.test_tag = tag_model.Tag(name='test', hidden=False, protect=False, color='red').put().get() self.test_shelf_model = shelf_model.Shelf( enabled=True, friendly_name='test_friendly_name', location='test_location', lat_long=ndb.GeoPt(10.10, 20.20), altitude=1.1, capacity=10, audit_interval_override=12, audit_notification_enabled=True, audit_requested=True, responsible_for_audit='test_group', last_audit_time=datetime.datetime(year=2018, month=1, day=1), last_audit_by='test_auditer').put().get() self.expected_shelf_message = shelf_messages.Shelf( shelf_request=shelf_messages.ShelfRequest( location='test_location', urlsafe_key=self.test_shelf_model.key.urlsafe()), enabled=True, friendly_name='test_friendly_name', location='test_location', identifier='test_friendly_name', latitude=10.10, longitude=20.20, altitude=1.1, capacity=10, audit_notification_enabled=True, audit_requested=True, audit_enabled=True, responsible_for_audit='test_group', last_audit_time=datetime.datetime(year=2018, month=1, day=1), last_audit_by='test_auditer')
def test_get_shelf_using_location(self): """Test getting a shelf using the location.""" request = shelf_messages.ShelfRequest(location=self.shelf.location) shelf = shelf_api.get_shelf(request) self.assertEqual(shelf, self.shelf)
def test_get_shelf_urlsafe_key(self): """Test getting a shelf using the urlsafe key.""" request = shelf_messages.ShelfRequest( urlsafe_key=self.shelf.key.urlsafe()) shelf = shelf_api.get_shelf(request) self.assertEqual(shelf, self.shelf)
def test_disable_by_location(self): request = shelf_messages.ShelfRequest(location='NYC') self.assertTrue(self.shelf.enabled) response = self.service.disable(request) self.assertFalse(self.shelf.enabled) self.assertIsInstance(response, message_types.VoidMessage)
def test_get_by_location(self, mock_xsrf_token): request = shelf_messages.ShelfRequest(location='NYC') response = self.service.get(request) assert mock_xsrf_token.call_count == 1 self.assertEqual(self.shelf.location, response.location) self.assertEqual(self.shelf.friendly_name, response.friendly_name)
def testShelfRequest(self): shelf_req = shelf_messages.ShelfRequest( location='FAKE-LOCATION', urlsafe_key='FAKE-URL-SAFE-KEY') self.assertEqual(shelf_req.location, 'FAKE-LOCATION') self.assertEqual(shelf_req.urlsafe_key, 'FAKE-URL-SAFE-KEY')