def _UploadEpisodeWithPhoto(self):
    """Create episode with photo and upload.
    Returns: photo_id of created photo.
    """
    timestamp = time.time()

    episode_id = Episode.ConstructEpisodeId(timestamp, self._device_ids[0], 100)
    ep_dict = {'episode_id': episode_id,
               'timestamp': timestamp,
               'title': 'Episode Title'}

    photo_id = Photo.ConstructPhotoId(timestamp, self._device_ids[0], 100)
    ph_dict = {'aspect_ratio': 1.3333,
               'timestamp': timestamp,
               'tn_md5': util.ComputeMD5Hex('thumbnail image data'),
               'med_md5': util.ComputeMD5Hex('medium image data'),
               'full_md5': util.ComputeMD5Hex('full image data'),
               'orig_md5': util.ComputeMD5Hex('original image data'),
               'tn_size': 5*1024,
               'med_size': 10*1024,
               'full_size': 150*1024,
               'orig_size': 1200*1024,
               'caption': 'a photo',
               'photo_id': photo_id}

    self._tester.UploadEpisode(self._cookie, ep_dict, [ph_dict])

    return photo_id
    def testWebappUploadEpisode(self):
        """Create 10 new photos using web."""
        photos = [{'aspect_ratio': 1.3333,
                   'timestamp': time.time(),
                   'content_type': 'text/plain',
                   'tn_md5': util.ComputeMD5Hex('thumbnail image data'),
                   'med_md5': util.ComputeMD5Hex('medium image data'),
                   'full_md5': util.ComputeMD5Hex('full image data'),
                   'orig_md5': util.ComputeMD5Hex('original image data'),
                   'tn_size': 5 * 1024,
                   'med_size': 10 * 1024,
                   'full_size': 150 * 1024,
                   'orig_size': 1200 * 1024,
                   'caption': 'caption number %d' % i} \
                  for i in xrange(10)]

        # Simulate the web app by allocating ids.
        response_dict = self._SendRequest(
            'allocate_ids', self._cookie2,
            {'asset_types': list('p' * len(photos))})

        for i, p in enumerate(photos):
            p['photo_id'] = response_dict['asset_ids'][i]

        self._tester.UploadEpisode(self._cookie2, {}, photos)
 def _CreatePhotoDict(self, user_cookie, **update_ph_dict):
   """Create dict() for a test photo, overriding default values with
   whatever is passed in "update_ph_dict"."""
   user_id, device_id = self._tester.GetIdsFromCookie(user_cookie)
   timestamp = update_ph_dict.get('timestamp', time.time() - self._test_id)
   ph_dict = {'photo_id': Photo.ConstructPhotoId(timestamp, device_id, self._test_id),
              'aspect_ratio': .75 + self._test_id,
              'content_type': 'image/jpeg',
              'tn_md5': util.ComputeMD5Hex('thumbnail image data'),
              'med_md5': util.ComputeMD5Hex('medium image data'),
              'full_md5': util.ComputeMD5Hex('full image data'),
              'orig_md5': util.ComputeMD5Hex('original image data'),
              'location': {'latitude': 47.5675, 'longitude':-121.962, 'accuracy': 0.0},
              'placemark': {'iso_country_code': u'US',
                            'thoroughfare': u'SE 43rd St',
                            'locality': u'Fall City',
                            'country': u'United States',
                            'subthoroughfare': u'28408',
                            'state': u'Washington',
                            'sublocality': u'Issaquah Plateau'},
              'tn_size': 5 * 1024,
              'med_size': 40 * 1024,
              'full_size': 150 * 1024,
              'orig_size': 1200 * 1024,
              'timestamp': timestamp,
              'caption': 'Photo caption #%d' % self._test_id}
   self._test_id += 1
   ph_dict.update(**update_ph_dict)
   return ph_dict
    def testMobileUploadEpisode(self):
        """Create 10 new photos using mobile device."""
        photos = [{'aspect_ratio': 1.3333,
                   'tn_md5': util.ComputeMD5Hex('thumbnail image data'),
                   'med_md5': util.ComputeMD5Hex('medium image data'),
                   'full_md5': util.ComputeMD5Hex('full image data'),
                   'orig_md5': util.ComputeMD5Hex('original image data'),
                   'tn_size': 5 * 1024,
                   'med_size': 10 * 1024,
                   'full_size': 150 * 1024,
                   'orig_size': 1200 * 1024,
                   'caption': 'caption number %d' % i} \
                  for i in xrange(10)]

        # Simulate the mobile device by letting _UploadEpisode generate photo device ids.
        self._tester.UploadEpisode(self._cookie, {}, photos)
Example #5
0
    def testUploadMismatch(self):
        """Upload photo image data with a different MD5 than was originally
    provided to upload_episode. Because the photo image data does not
    yet exist, the metadata should be overwritten with the new values.
    Then try to upload a different MD5 again, expecting an error this
    time.
    """
        for attr_name, suffix, image_data in [
            ('tn_md5', '.t', 'new thumbnail image data'),
            ('med_md5', '.m', 'new medium image data'),
            ('full_md5', '.f', 'new full image data'),
            ('orig_md5', '.o', 'new original image data')
        ]:
            # Expect success on first upload.
            response = self._PutPhoto(
                self._cookie,
                self._episode_id,
                self._photo_ids[0],
                suffix,
                image_data,
                content_md5=util.ComputeMD5Base64(image_data),
                etag=util.ComputeMD5Hex(image_data))
            self.assertEqual(response.code, 200)

            # Validate that the photo's MD5 was updated.
            ph_dict = {
                'photo_id': self._photo_ids[0],
                attr_name: util.ComputeMD5Hex(image_data)
            }
            self._validator.ValidateUpdateDBObject(Photo, **ph_dict)

            # Expect failure on second upload with different MD5.
            new_image_data = 'really ' + image_data
            response = self._PutPhoto(
                self._cookie,
                self._episode_id,
                self._photo_ids[0],
                suffix,
                new_image_data,
                content_md5=util.ComputeMD5Base64(new_image_data),
                etag=util.ComputeMD5Hex(new_image_data))
            self.assertEqual(response.code, 400)
Example #6
0
    def testReUpload(self):
        """Upload a new photo and attempt to re-upload using If-None-Match
    header to simulate a phone reinstall where the client uses the
    /photos/<photo_id> interface to get a redirect to a PUT URL. In
    the case of the photo existing, the Etag should match and result
    in a 304 response, saving the client the upload bandwidth.
    """
        full_image_data = 'full image data'

        for photo_id in self._photo_ids:
            response = self._PutPhoto(
                self._cookie,
                self._episode_id,
                photo_id,
                '.f',
                full_image_data,
                content_md5=util.ComputeMD5Base64(full_image_data),
                etag=util.ComputeMD5Hex(full_image_data))
            self.assertEqual(response.code, 200)

        for photo_id in self._photo_ids:
            response = self._PutPhoto(
                self._cookie,
                self._episode_id,
                photo_id,
                '.f',
                full_image_data,
                content_md5=util.ComputeMD5Base64(full_image_data),
                etag='"%s"' % util.ComputeMD5Hex(full_image_data))
            self.assertEqual(response.code, 304)

            response = self._PutPhoto(
                self._cookie,
                self._episode_id,
                photo_id,
                '.f',
                full_image_data,
                content_md5=util.ComputeMD5Base64(full_image_data),
                etag='*')
            self.assertEqual(response.code, 304)
  def testUpdateEpisode(self):
    """Creates a new episode and photo and updates both."""
    timestamp = time.time()

    episode_id = Episode.ConstructEpisodeId(timestamp, self._device_ids[0], 100)
    ep_dict = {'episode_id': episode_id,
               'timestamp': timestamp,
               'title': 'Episode Title'}

    photo_id = Photo.ConstructPhotoId(timestamp, self._device_ids[0], 100)
    ph_dict = {'aspect_ratio': 1.3333,
               'timestamp': time.time(),
               'tn_md5': util.ComputeMD5Hex('thumbnail image data'),
               'med_md5': util.ComputeMD5Hex('medium image data'),
               'full_md5': util.ComputeMD5Hex('full image data'),
               'orig_md5': util.ComputeMD5Hex('original image data'),
               'tn_size': 5 * 1024,
               'med_size': 10 * 1024,
               'full_size': 150 * 1024,
               'orig_size': 1200 * 1024,
               'photo_id': photo_id}

    self._tester.UploadEpisode(self._cookie, ep_dict, [ph_dict])
    self._tester.UpdateEpisode(self._cookie, episode_id, description='A newly added description')
Example #8
0
        def _OnCompletedGet(content):
            md5_hex = util.ComputeMD5Hex(content)
            self.set_header(
                "Expires",
                int(time.time()) + FileObjectStoreHandler._CACHE_MAX_AGE)
            self.set_header('Content-Type', self.content_type)
            self.set_header('Content-Length', len(content))
            self.set_header('Content-MD5', md5_hex)
            self.set_header('Etag', '"%s"' % md5_hex)

            cache_control = self.get_argument('response-cache-control', None)
            if cache_control is not None:
                self.set_header("Cache-Control", cache_control)

            if include_body:
                self.write(content)
            self.finish()
    def testMD5ClientLog(self):
        """Verify MD5 validation for client logs."""
        log_body = 'test log file'
        content_md5 = util.ComputeMD5Hex(log_body)

        request_dict = {
            'headers': {
                'op_id': 'o1',
                'op_timestamp': time.time()
            },
            'timestamp': time.time(),
            'client_log_id': 'log1',
            'content_md5': content_md5
        }
        self._GetNewLogUrlAndVerify(request_dict,
                                    log_body,
                                    content_type=CLIENT_LOG_CONTENT_TYPE,
                                    content_md5=content_md5)
    def testUploadMD5Mismatch(self):
        """Call upload_episode once. Then call it again, but with different
    MD5 image values. Because the photo image data does not yet exist,
    the metadata should be overwritten with the new values. Then actually
    upload the image data and try to overwrite the MD5 values again,
    expecting an error this time.
    """
        upload_data = [('tn_md5', '.t', 'new thumbnail image data'),
                       ('med_md5', '.m', 'new medium image data'),
                       ('full_md5', '.f', 'new full image data'),
                       ('orig_md5', '.o', 'new original image data')]

        ph_dict = {
            'aspect_ratio': 0.75,
            'timestamp': time.time(),
            'tn_size': 5 * 1024,
            'med_size': 10 * 1024,
            'full_size': 150 * 1024,
            'orig_size': 1200 * 1024
        }

        # Do first upload.
        for data in upload_data:
            attr_name, suffix, image_data = data
            ph_dict[attr_name] = util.ComputeMD5Hex(image_data)

        ep_dict = {'timestamp': time.time()}
        ep_id, ph_ids = self._UploadEpisode(self._cookie, [ph_dict], ep_dict)
        ph_dict['photo_id'] = ph_ids[0]

        # Update the photo MD5 values and upload again.
        for data in upload_data:
            attr_name, suffix, image_data = data
            ph_dict[attr_name] = util.ComputeMD5Hex('really ' + image_data)

        ep_dict['episode_id'] = ep_id
        ep_id, ph_ids = self._UploadEpisode(self._cookie, [ph_dict], ep_dict)
        assert ph_dict['photo_id'] == ph_ids[0], (ph_dict, ph_ids)

        for data in upload_data:
            # Upload the image data for this size of photo.
            attr_name, suffix, image_data = data
            etag = util.ComputeMD5Hex('really ' + image_data)
            self._tester.PutPhotoImage(
                self._cookie,
                ep_id,
                ph_ids[0],
                suffix,
                'really ' + image_data,
                content_md5=util.ComputeMD5Base64('really ' + image_data),
                etag=etag)

            # Validate that the photo's MD5 was updated.
            update_ph_dict = {'photo_id': ph_ids[0], attr_name: etag}
            self._validator.ValidateUpdateDBObject(Photo, **update_ph_dict)

            # Verify the MD5 value can no longer be updated.
            copy_ph_dict = deepcopy(ph_dict)
            copy_ph_dict[attr_name] = util.ComputeMD5Hex('bad ' + image_data)
            self.assertRaisesHttpError(403, self._UploadEpisode, self._cookie,
                                       [copy_ph_dict], ep_dict)

            # Test changing one MD5 and not others.
            if suffix == '.m':
                copy_ph_dict = deepcopy(ph_dict)
                copy_ph_dict['full_md5'] = util.ComputeMD5Hex('only this ' +
                                                              image_data)
                self._UploadEpisode(self._cookie, [copy_ph_dict], ep_dict)