def test_magicfiles_resource(self): # Set up some more files. testfile = Sharedfile.get("id = %s", 1) response = self.upload_file(file_path=self.test_file1_path, sha1=self.test_file1_sha1, content_type=self.test_file1_content_type, user_id=1, sid=self.sid, xsrf=self.xsrf) testfile_2 = Sharedfile.get("id = %s", 2) testfile_2.name = "test file 2" testfile_2.save() testfile_in_user2_shake = testfile_2.save_to_shake(self.user_b) testfile_in_another_group = testfile_2.save_to_shake(self.user_a, self.group_shake_2) sid_b = self.sign_in('user2', 'asdfasdf') xsrf_b = self.get_xsrf() response = self.upload_file(file_path=self.test_file1_path, sha1=self.test_file1_sha1, content_type=self.test_file1_content_type, user_id=2, sid=sid_b, xsrf=xsrf_b, shake_id=self.group_shake.id) testfile_3 = Sharedfile.get("id = %s", 5) testfile_3.name = "test file 5" testfile_3.save() with test_option('likes_to_magic', 5): # Set up their favorites. for sf, likes in zip((testfile, testfile_2, testfile_in_user2_shake, testfile_3), (0, 7, 3, 5)): for i in range(likes): f = Favorite() f.user_id = i f.sharedfile_id = sf.id f.save() calculate_likes(sf.id) # What's best? request = signed_request(self.access_token, self.get_url('/api/magicfiles'), 'GET') self.http_client.fetch(request, self.stop) response = self.wait() j_response = json_decode(response.body) magicfiles = j_response['magicfiles'] pivot_ids = [sf['pivot_id'] for sf in magicfiles] share_keys = [sf['sharekey'] for sf in magicfiles] self.assertEqual(share_keys, ['5', '2']) self.assertEqual(pivot_ids, ['2', '1']) # Pagination check. request = signed_request(self.access_token, self.get_url('/api/magicfiles/before/2'), 'GET') self.http_client.fetch(request, self.stop) response = self.wait() j_response = json_decode(response.body) self.assertEqual('1', j_response['magicfiles'][0]['pivot_id']) request = signed_request(self.access_token, self.get_url('/api/magicfiles/after/1'), 'GET') self.http_client.fetch(request, self.stop) response = self.wait() j_response = json_decode(response.body) self.assertEqual('2', j_response['magicfiles'][0]['pivot_id'])
def test_query_favorites(self): # Set up a favorite. f = Favorite() f.user_id = self.user_b.id f.sharedfile_id = 1 f.save() request = signed_request(self.access_token, self.get_url('/api/favorites')) self.http_client.fetch(request, self.stop) response = self.wait() j_response = json_decode(response.body) self.assertTrue('favorites' in j_response) favs = j_response['favorites'] fav_ids = (fav['permalink_page'].rsplit('/', 1)[-1] for fav in favs) self.assertEqual(list(fav_ids), ['1'])