Exemple #1
0
    def setUp(self):
        super(TagApiTest, self).setUp()
        self.service = tag_api.TagApi()
        self.login_admin_endpoints_user()

        self.test_tag = tag_model.Tag.create(user_email=loanertest.USER_EMAIL,
                                             name='tag-one',
                                             hidden=False,
                                             protect=False,
                                             color='amber')
        self.test_tag_response = tag_messages.Tag(
            name=self.test_tag.name,
            hidden=self.test_tag.hidden,
            protect=self.test_tag.protect,
            color=self.test_tag.color,
            description=self.test_tag.description,
            urlsafe_key=self.test_tag.key.urlsafe())

        self.hidden_tag = tag_model.Tag.create(
            user_email=loanertest.USER_EMAIL,
            name='tag-two',
            hidden=True,
            protect=False,
            color='red',
            description='test-description')
        self.hidden_tag_response = tag_messages.Tag(
            name=self.hidden_tag.name,
            hidden=self.hidden_tag.hidden,
            protect=self.hidden_tag.protect,
            color=self.hidden_tag.color,
            description=self.hidden_tag.description,
            urlsafe_key=self.hidden_tag.key.urlsafe())
Exemple #2
0
  def list(self, request):
    """Lists tags in datastore."""
    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.')

    cursor = None
    if request.cursor:
      cursor = api_utils.get_datastore_cursor(urlsafe_cursor=request.cursor)

    (tag_results, next_cursor,
     has_additional_results), total_pages = tag_model.Tag.list(
         page_size=request.page_size,
         page_index=request.page_index,
         include_hidden_tags=request.include_hidden_tags,
         cursor=cursor)
    tags_messages = []
    for tag in tag_results:
      message = tag_messages.Tag(
          name=tag.name, hidden=tag.hidden, color=tag.color,
          protect=tag.protect, description=tag.description,
          urlsafe_key=tag.key.urlsafe())
      tags_messages.append(message)

    return tag_messages.ListTagResponse(
        tags=tags_messages,
        cursor=next_cursor.urlsafe() if next_cursor else None,
        has_additional_results=has_additional_results,
        total_pages=total_pages)
Exemple #3
0
 def test_create_defaults(self):
     request = tag_messages.CreateTagRequest(
         tag=tag_messages.Tag(name='restricted_location', color='blue'))
     with mock.patch.object(self.service, 'check_xsrf_token',
                            autospec=True) as mock_xsrf_token:
         response = self.service.create(request)
         self.assertEqual(mock_xsrf_token.call_count, 1)
         self.assertIsInstance(response, message_types.VoidMessage)
Exemple #4
0
def build_device_message_from_model(device, guest_permitted):
    """Builds a device_messages.Device ProtoRPC message.

  Args:
    device: device_model.Device, a device entity to convert into a message.
    guest_permitted: bool, whether or not guest is permitted for this
        organization.

  Returns:
    A populated device_messages.Device ProtoRPC message.
  """
    message = device_messages.Device(
        serial_number=device.serial_number,
        asset_tag=device.asset_tag,
        identifier=device.identifier,
        enrolled=device.enrolled,
        device_model=device.device_model,
        due_date=device.due_date,
        last_known_healthy=device.last_known_healthy,
        assigned_user=device.assigned_user,
        assignment_date=device.assignment_date,
        current_ou=device.current_ou,
        ou_changed_date=device.ou_changed_date,
        locked=device.locked,
        lost=device.lost,
        mark_pending_return_date=device.mark_pending_return_date,
        chrome_device_id=device.chrome_device_id,
        last_heartbeat=device.last_heartbeat,
        damaged=device.damaged,
        damaged_reason=device.damaged_reason,
        guest_enabled=device.guest_enabled,
        guest_permitted=guest_permitted,
        overdue=device.overdue,
    )
    if device.last_reminder:
        message.last_reminder = build_reminder_message_from_model(
            device.last_reminder)
    if device.next_reminder:
        message.next_reminder = build_reminder_message_from_model(
            device.next_reminder)
    if device.is_assigned:
        message.max_extend_date = device.return_dates.max
    if device.shelf:
        message.shelf = build_shelf_message_from_model(device.shelf.get())
    for tag_data in device.tags:
        tag_data_message = tag_messages.TagData()
        urlsafe_key = tag_model.Tag.get(tag_data.tag.name).key.urlsafe()
        tag_data_message.tag = tag_messages.Tag(
            name=tag_data.tag.name,
            hidden=tag_data.tag.hidden,
            color=tag_data.tag.color,
            protect=tag_data.tag.protect,
            description=tag_data.tag.description,
            urlsafe_key=urlsafe_key)
        tag_data_message.more_info = tag_data.more_info
        message.tags.append(tag_data_message)

    return message
Exemple #5
0
 def get(self, request):
     """Gets a tag by its urlsafe key."""
     self.check_xsrf_token(self.request_state)
     tag = api_utils.get_ndb_key(request.urlsafe_key).get()
     return tag_messages.Tag(name=tag.name,
                             hidden=tag.hidden,
                             color=tag.color,
                             protect=tag.protect,
                             description=tag.description)
    def setUp(self):
        super(TagMessagesPy23MigrationTest, self).setUp()

        self.tag = tag_messages.Tag(name='FAKE-NAME',
                                    hidden=False,
                                    color='FAKE-COLOR',
                                    protect=True,
                                    description='FAKE-DESCRIPTION',
                                    urlsafe_key='FAKE-URL-KEY')
Exemple #7
0
 def test_update_protected(self):
     """Tests updating a nonexistent tag."""
     request = tag_messages.UpdateTagRequest(tag=tag_messages.Tag(
         urlsafe_key=self.protected_tag.key.urlsafe(),
         name=self.protected_tag.name,
         hidden=self.protected_tag.hidden,
         protect=self.protected_tag.protect,
         color=self.protected_tag.color,
         description='A new description for a protected tag.'))
     with self.assertRaises(tag_api.endpoints.BadRequestException):
         self.service.update(request)
Exemple #8
0
 def test_create(self):
     request = tag_messages.CreateTagRequest(
         tag=tag_messages.Tag(name='restricted_location',
                              hidden=False,
                              color='red',
                              description='leadership circle'))
     with mock.patch.object(self.service, 'check_xsrf_token',
                            autospec=True) as mock_xsrf_token:
         response = self.service.create(request)
         self.assertEqual(mock_xsrf_token.call_count, 1)
         self.assertIsInstance(response, message_types.VoidMessage)
Exemple #9
0
 def test_update_nonexistent(self):
     """Tests updating a nonexistent tag."""
     request = tag_messages.UpdateTagRequest(
         tag=tag_messages.Tag(urlsafe_key='nonexistent_urlsafe_key',
                              name='nonexistent tag',
                              hidden=False,
                              protect=False,
                              color='blue',
                              description=None))
     with mock.patch.object(self.service, 'check_xsrf_token',
                            autospec=True):
         with self.assertRaises(tag_api.endpoints.BadRequestException):
             self.service.update(request)
Exemple #10
0
 def test_get_tag(self):
     request = tag_messages.TagRequest(
         urlsafe_key=self.test_tag.key.urlsafe())
     expected_response = tag_messages.Tag(
         name=self.test_tag.name,
         hidden=self.test_tag.hidden,
         color=self.test_tag.color,
         protect=self.test_tag.protect,
         description=self.test_tag.description)
     with mock.patch.object(self.service, 'check_xsrf_token',
                            autospec=True) as mock_xsrf_token:
         response = self.service.get(request)
         self.assertEqual(mock_xsrf_token.call_count, 1)
         self.assertEqual(response, expected_response)
Exemple #11
0
 def test_update_rename(self):
     """Tests updating a tag with a rename."""
     new_name = 'A new tag name.'
     request = tag_messages.UpdateTagRequest(
         tag=tag_messages.Tag(urlsafe_key=self.test_tag.key.urlsafe(),
                              name=new_name,
                              hidden=self.test_tag.hidden,
                              protect=self.test_tag.protect,
                              color=self.test_tag.color,
                              description=self.test_tag.description))
     response = self.service.update(request)
     self.assertIsInstance(response, message_types.VoidMessage)
     tag = tag_model.Tag.get(self.test_tag.key.urlsafe())
     self.assertEqual(tag.name, new_name)
     self.assertEqual(tag.hidden, self.test_tag.hidden)
     self.assertEqual(tag.protect, self.test_tag.protect)
     self.assertEqual(tag.color, self.test_tag.color)
     self.assertEqual(tag.description, self.test_tag.description)
Exemple #12
0
  def list(self, request):
    """Lists tags in datastore."""
    self.check_xsrf_token(self.request_state)
    cursor = None
    if request.cursor:
      cursor = api_utils.get_datastore_cursor(urlsafe_cursor=request.cursor)

    tag_results, next_cursor, has_additional_results = tag_model.Tag.list(
        page_size=request.page_size, cursor=cursor)
    tags_messages = []
    for tag in tag_results:
      message = tag_messages.Tag(
          name=tag.name, hidden=tag.hidden, color=tag.color,
          protect=tag.protect, description=tag.description,
          urlsafe_key=tag.key.urlsafe())
      tags_messages.append(message)

    return tag_messages.ListTagResponse(
        tags=tags_messages,
        cursor=next_cursor.urlsafe() if next_cursor else None,
        has_additional_results=has_additional_results)
Exemple #13
0
 def test_update(self):
     new_color = 'blue'
     new_description = 'An updated description.'
     request = tag_messages.UpdateTagRequest(
         tag=tag_messages.Tag(urlsafe_key=self.test_tag.key.urlsafe(),
                              name=self.test_tag.name,
                              hidden=self.test_tag.hidden,
                              protect=self.test_tag.protect,
                              color=new_color,
                              description=new_description))
     with mock.patch.object(self.service, 'check_xsrf_token',
                            autospec=True) as mock_xsrf_token:
         response = self.service.update(request)
         self.assertEqual(mock_xsrf_token.call_count, 1)
     self.assertIsInstance(response, message_types.VoidMessage)
     # Ensure that the new tag was updated.
     tag = tag_model.Tag.get(self.test_tag.key.urlsafe())
     self.assertEqual(tag.name, self.test_tag.name)
     self.assertEqual(tag.hidden, self.test_tag.hidden)
     self.assertEqual(tag.protect, self.test_tag.protect)
     self.assertEqual(tag.color, new_color)
     self.assertEqual(tag.description, new_description)
Exemple #14
0
 def test_create_bad_request(self):
     """Test create raises BadRequestException with required fields missing."""
     request = tag_messages.CreateTagRequest(tag=tag_messages.Tag(
         name='no_color_tag'))
     with self.assertRaises(endpoints.BadRequestException):
         self.service.create(request)
Exemple #15
0
    def test_build_device_message_from_model(self):
        """Test the construction of a device message from a device entity."""
        test_device = device_model.Device(
            serial_number='test_serial_value',
            asset_tag='test_asset_tag_value',
            enrolled=True,
            device_model='test model value',
            due_date=datetime.datetime(year=2018, month=1, day=1),
            last_known_healthy=datetime.datetime(year=2018, month=1, day=2),
            shelf=self.test_shelf_model.key,
            assigned_user='******',
            assignment_date=datetime.datetime(year=2018, month=1, day=3),
            current_ou=constants.ORG_UNIT_DICT['GUEST'],
            ou_changed_date=datetime.datetime(year=2018, month=1, day=4),
            locked=True,
            lost=False,
            mark_pending_return_date=datetime.datetime(year=2018,
                                                       month=1,
                                                       day=5),
            chrome_device_id='device id value',
            last_heartbeat=datetime.datetime(year=2018, month=1, day=6),
            damaged=None,
            damaged_reason='Not damaged',
            last_reminder=device_model.Reminder(level=1),
            next_reminder=device_model.Reminder(level=2),
        ).put().get()
        test_device.associate_tag('test', self.test_tag.name)
        expected_message = device_messages.Device(
            serial_number='test_serial_value',
            asset_tag='test_asset_tag_value',
            identifier='test_asset_tag_value',
            enrolled=True,
            device_model='test model value',
            due_date=datetime.datetime(year=2018, month=1, day=1),
            last_known_healthy=datetime.datetime(year=2018, month=1, day=2),
            shelf=self.expected_shelf_message,
            assigned_user='******',
            assignment_date=datetime.datetime(year=2018, month=1, day=3),
            current_ou=constants.ORG_UNIT_DICT['GUEST'],
            ou_changed_date=datetime.datetime(year=2018, month=1, day=4),
            locked=True,
            lost=False,
            mark_pending_return_date=datetime.datetime(year=2018,
                                                       month=1,
                                                       day=5),
            chrome_device_id='device id value',
            last_heartbeat=datetime.datetime(year=2018, month=1, day=6),
            damaged=None,
            damaged_reason='Not damaged',
            last_reminder=device_messages.Reminder(level=1),
            next_reminder=device_messages.Reminder(level=2),
            guest_permitted=True,
            guest_enabled=True,
            max_extend_date=test_device.return_dates.max,
            overdue=True,
        )
        expected_tag = tag_messages.Tag(
            name=self.test_tag.name,
            hidden=self.test_tag.hidden,
            protect=self.test_tag.protect,
            color=self.test_tag.color,
            urlsafe_key=self.test_tag.key.urlsafe())
        expected_tag_data = tag_messages.TagData(tag=expected_tag)
        expected_message.tags.append(expected_tag_data)

        actual_message = api_utils.build_device_message_from_model(
            test_device, True)
        self.assertEqual(actual_message, expected_message)
Exemple #16
0
 def test_update_bad_request(self):
     """Tests update raises BadRequestException with required fields missing."""
     request = tag_messages.UpdateTagRequest(tag=tag_messages.Tag(
         urlsafe_key=self.test_tag.key.urlsafe(), name='tag name'))
     with self.assertRaises(endpoints.BadRequestException):
         self.service.update(request)
Exemple #17
0
 def test_create_prexisting_tag(self):
     request = tag_messages.CreateTagRequest(tag=tag_messages.Tag(
         name=self.test_tag.name, hidden=False, color='blue'))
     with self.assertRaises(endpoints.BadRequestException):
         self.service.create(request)