def setUp(self): super(FileViewTests, self).setUp() self.user = User(name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user.set_password('asdfasdf') self.user.save() self.user2 = User(name='user', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user2.set_password('asdfasdf') self.user2.save() self.sid2 = self.sign_in('user', 'asdfasdf') self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.test_file1_path = os.path.abspath("test/files/1.png") self.test_file1_sha1 = Sourcefile.get_sha1_file_key( self.test_file1_path) self.test_file1_content_type = "image/png" self.test_file2_path = os.path.abspath("test/files/love.gif") self.test_file2_sha1 = Sourcefile.get_sha1_file_key( self.test_file2_path) self.test_file2_content_type = "image/gif"
def test_sharedfile_from_new_file(self): test_files = os.path.join(os.path.dirname(os.path.dirname(__file__)), "files") file_key = Sourcefile.get_sha1_file_key(test_files + "/1.png") shutil.copyfile("%s/1.png" % (test_files), "/tmp/%s" % (file_key)) shared_file = Sharedfile.create_from_file("/tmp/%s" % (file_key),"1.png", file_key, "image/png", self.user.id) self.assertEqual(shared_file.id, 2) self.assertEqual(shared_file.source_id, 2)
def setUp(self): super(NotificationTests, self).setUp() self.sender = User(name="admin", email="*****@*****.**", email_confirmed=1, is_paid=1) self.sender.set_password('asdfasdf') self.sender.save() self.receiver = User(name="user2", email="*****@*****.**", email_confirmed=1, is_paid=1) self.receiver.set_password('asdfasdf') self.receiver.save() self.xsrf = self.get_xsrf() self.sender_sid = self.sign_in('admin', 'asdfasdf') self.receiver_sid = self.sign_in('user2', 'asdfasdf') self.test_file1_path = os.path.abspath("test/files/1.png") self.test_file1_sha1 = Sourcefile.get_sha1_file_key( self.test_file1_path) self.test_file1_content_type = "image/png" self.upload_file(self.test_file1_path, self.test_file1_sha1, self.test_file1_content_type, 1, self.receiver_sid, self.xsrf)
def test_posting_fails_when_provider_is_not_localhost(self): provider = "http://notes.torrez.org" """ Copies a file to the file-system, then POSTs the location and details to the upload method for processing """ file_path = os.path.abspath("test/files/1.png") sha1 = Sourcefile.get_sha1_file_key(file_path) content_type = "image/png" file_name = os.path.basename(file_path) file_size = os.path.getsize(file_path) body = "media_name=%s&media_content_type=%s&media_sha1=%s&media_size=%s&media_path=%s" % ( file_name, content_type, sha1, file_size, file_path) request = HTTPRequest( url=self.get_url('/upload'), method='POST', headers={ 'X-Auth-Service-Provider': provider, 'X-Verify-Credentials-Authorization': 'OAuth oauth_timestamp="1290404453", oauth_version="1.0", oauth_consumer_key="IQKbtAYlXLripLGPWd0HUA", oauth_token="37458155-JCG7c8oejM6N4TK4HJbXVC5VGq1gtaSUPt90wxFI", oauth_signature="9QxkJqBAJfZ83sbz6SCJKSaPn9U%3D", oauth_nonce="C7AB0CBC-9193-44EE-AFC1-6FE3BA51F048", oauth_signature_method="HMAC-SHA1"' }, body=body, ) self.http_client.fetch(request, self.stop) response = self.wait() self.assertEqual(response.code, 403)
def test_total_file_count_for_this_month(self): """ tests that the current amount of new files uploaded for the current month is correct """ images = ['red.gif', 'blue.gif', 'green.gif', 'love.gif'] for image in images: file_name = image file_content_type = 'image/gif' file_path = os.path.abspath("test/files/%s" % (file_name)) file_sha1 = Sourcefile.get_sha1_file_key(file_path) sf = Sharedfile.create_from_file(file_path=file_path, file_name=file_name, sha1_value=file_sha1, content_type=file_content_type, user_id=self.user.id) month_days = calendar.monthrange(datetime.utcnow().year, datetime.utcnow().month) start_time = datetime.utcnow().strftime("%Y-%m-01") end_time = datetime.utcnow().strftime("%Y-%m-" + str(month_days[1])) self.assertEqual( self.user.uploaded_kilobytes(start_time=start_time, end_time=end_time), 72)
def setUp(self): super(ReadonlyTests, self).setUp() options.readonly = False self.admin = test.factories.user() self.sid = self.sign_in("admin", "password") self.xsrf = self.get_xsrf() self.test_file1_path = os.path.abspath("test/files/1.png") self.test_file1_sha1 = Sourcefile.get_sha1_file_key(self.test_file1_path) self.test_file1_content_type = "image/png"
def on_thumbnail_response(self, response): if response.code != 200: self.render( "tools/save-video-error.html", message= "We could not load the thumbnail for this file and therefore could not save this video. Please contact support." ) return # save the response url = self.get_argument('url') current_user = self.get_current_user_object() sha1_key = Sourcefile.get_sha1_file_key(file_path=None, file_data=url) thumbnail_path = "%s/%s" % (options.uploaded_files, sha1_key) fh = open(thumbnail_path, 'wb') fh.write(response.body) fh.close() source_file = Sourcefile.create_from_json_oembed( link=url, oembed_doc=self.oembed_doc, thumbnail_file_path=thumbnail_path) #cleanup if not options.debug: try: os.remove(thumbnail_path) except: pass title = '' if self.oembed_doc.has_key('title'): title = self.oembed_doc['title'] shared_file = Sharedfile(user_id=current_user.id, name=url, content_type='text/html', source_id=source_file.id, title=title, source_url=url) shared_file.save() share_key = base36encode(shared_file.id) shared_file.share_key = share_key shared_file.save() user_shake = Shake.get('user_id = %s and type=%s', current_user.id, 'user') shared_file.add_to_shake(self.destination_shake) if self.oembed_doc.has_key('description'): shared_file.description = self.oembed_doc['description'] self.write({'path': "/p/%s" % (share_key)}) self.finish()
def setUp(self): """ user_a -> admin user_b -> user2 user_a uploads shared file. We authenticate to the API with user_b. user_b subscribes to user_a's shake.] """ super(APIResourceRequests, self).setUp() self.user_a = User( name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1, about="admin", website='https://mltshp.com') self.user_a.set_password('asdfasdf') self.user_a.save() self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.test_file1_path = os.path.abspath("test/files/1.png") self.test_file1_sha1 = Sourcefile.get_sha1_file_key(self.test_file1_path) self.test_file1_content_type = "image/png" response = self.upload_file(file_path=self.test_file1_path, sha1=self.test_file1_sha1, content_type=self.test_file1_content_type, user_id=self.user_a.id, sid=self.sid, xsrf=self.xsrf) self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.group_shake = self.user_b.create_group_shake(title='Group Shake', name='groupshake', description='This is a group shake.') self.group_shake_2 = self.user_a.create_group_shake(title='Another Group', name='anothergroup') # Add user_b to user_a's group shake, so we get it in user_b's /shakes endpoint. shake_manager = ShakeManager(user_id=self.user_b.id, shake_id=self.group_shake_2.id) shake_manager.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save() self.authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id) self.access_token = Accesstoken.generate(self.authorization.id) extra_authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id) self.ratelimited_access_token = Accesstoken.generate(extra_authorization.id) now_hour = datetime.utcnow().strftime('%Y-%m-%d %H:00:00') ratelimit = Apihit(accesstoken_id=self.ratelimited_access_token.id, hits=options.api_hits_per_hour - 2, hour_start=now_hour) ratelimit.save() #subscription self.user_b.subscribe(self.user_a.shake())
def upload_test_file(self, shake_id=None): arguments = {} if shake_id: arguments['shake_id'] = int(shake_id) arguments['file_name'] = 'love.gif' arguments['file_content_type'] = 'image/gif' arguments['file_path'] = os.path.abspath("test/files/love.gif") arguments['file_sha1'] = Sourcefile.get_sha1_file_key( arguments['file_path']) arguments['file_size'] = os.path.getsize(arguments['file_path']) return self.post_url('/upload?skip_s3=1', arguments)
def test_file_size_is_saved_in_newly_uploaded_file(self): """ Tests that a brand new file upload saves the file size """ file_name = '1.png' file_content_type = 'image/png' file_path = os.path.abspath("test/files/1.png") file_sha1 = Sourcefile.get_sha1_file_key(file_path) file_size = os.path.getsize(file_path) sf = Sharedfile.create_from_file(file_path=file_path, file_name=file_name, sha1_value=file_sha1, content_type=file_content_type, user_id=self.user.id) self.assertEqual(sf.size, 69)
def on_response(self, response): url_parts = urlparse(response.request.url) file_name = os.path.basename(url_parts.path) title = self.get_argument("title", None) source_url = self.get_argument('source_url', None) description = self.get_argument('description', None) shake_id = self.get_argument('shake_id', None) if title == file_name: title = None if self.content_type not in self.approved_content_types: if response.body[0:50].find('JFIF') > -1: self.content_type = 'image/jpeg' else: return self.render("tools/picker-error.html") if len(file_name) == 0: return self.render("tools/picker-error.html") sha1_file_key = Sourcefile.get_sha1_file_key(file_data=response.body) user = self.get_current_user() try: fh = open("%s/%s" % (options.uploaded_files, sha1_file_key), 'wb') fh.write(response.body) fh.close() except Exception as e: raise tornado.web.HTTPError(500) sf = Sharedfile.create_from_file( file_path="%s/%s" % (options.uploaded_files, sha1_file_key), file_name=file_name, sha1_value=sha1_file_key, content_type=self.content_type, user_id=user['id'], title=title, shake_id=shake_id) sf.source_url = source_url sf.description = description sf.save() if not options.debug: # file cleanup try: os.remove("%s/%s" % (options.uploaded_files, sha1_file_key)) except: pass self.render("tools/picker-success.html", sf=sf)
def test_sharedfile_saved_to_group_shake(self): test_files = os.path.join(os.path.dirname(os.path.dirname(__file__)), "files") file_key = Sourcefile.get_sha1_file_key(test_files + "/1.png") shutil.copyfile("%s/1.png" % (test_files), "/tmp/%s" % (file_key)) #create a new shake group_shake = Shake(user_id=self.user.id, type='group', title='asdf', name='asdf') group_shake.save() a_shared_file = Sharedfile.create_from_file("/tmp/%s" % (file_key),"1.png", file_key, "image/png", self.user.id, group_shake.id) self.assertTrue(group_shake.can_update(self.user.id)) a_shared_file.add_to_shake(self.user.shake()) ssfs = Shakesharedfile.all() for ssf in ssfs: self.assertEqual(ssf.sharedfile_id, a_shared_file.id)
def test_cannot_upload_if_over_file_upload_limit_unpaid(self): """ Tests whether a user can upload a file if they are over the limit for this month. """ file_name = 'red.gif' file_content_type = 'image/gif' file_path = os.path.abspath("test/files/%s" % (file_name)) file_sha1 = Sourcefile.get_sha1_file_key(file_path) sf = Sharedfile.create_from_file(file_path=file_path, file_name=file_name, sha1_value=file_sha1, content_type=file_content_type, user_id=self.user.id) sf.size = 410000000 sf.save() self.assertFalse(self.user.can_upload_this_month())
def setUp(self): super(FileDeleteTests, self).setUp() self.user = User(name='admin', email='*****@*****.**', email_confirmed=1) self.user.set_password('asdfasdf') self.user.save() self.sid = self.sign_in("admin", "asdfasdf") self.xsrf = self.get_xsrf() self.test_file1_path = os.path.abspath("test/files/1.png") self.test_file1_sha1 = Sourcefile.get_sha1_file_key( self.test_file1_path) self.test_file1_content_type = "image/png" self.upload_file(self.test_file1_path, self.test_file1_sha1, self.test_file1_content_type, 1, self.sid, self.xsrf)
def test_can_upload_if_over_file_upload_limit_paid(self): """ Tests whether a user can upload a file if they are over the limit and have paid for the double scoop plan. """ self.user.stripe_plan_id = "mltshp-double" self.user.save() file_name = 'red.gif' file_content_type = 'image/gif' file_path = os.path.abspath("test/files/%s" % (file_name)) file_sha1 = Sourcefile.get_sha1_file_key(file_path) sf = Sharedfile.create_from_file(file_path=file_path, file_name=file_name, sha1_value=file_sha1, content_type=file_content_type, user_id=self.user.id) sf.size = 310000000 sf.save() self.assertTrue(self.user.can_upload_this_month())
def test_post_from_twitter_with_message(self): provider = self.get_url('/heartbeat') """ Copies a file to the file-system, then POSTs the location and details to the upload method for processing """ file_path = os.path.abspath("test/files/1.png") sha1 = Sourcefile.get_sha1_file_key(file_path) content_type = "image/png" file_name = os.path.basename(file_path) file_size = os.path.getsize(file_path) message = "hey look\r\n at me!\r\n" body = "message=%s&media_name=%s&media_content_type=%s&media_sha1=%s&media_size=%s&media_path=%s" % ( message, file_name, content_type, sha1, file_size, file_path) request = HTTPRequest( url=self.get_url('/upload'), method='POST', headers={ 'X-Auth-Service-Provider': provider, 'X-Verify-Credentials-Authorization': 'OAuth oauth_timestamp="1290404453", oauth_version="1.0", oauth_consumer_key="IQKbtAYlXLripLGPWd0HUA", oauth_token="37458155-JCG7c8oejM6N4TK4HJbXVC5VGq1gtaSUPt90wxFI", oauth_signature="9QxkJqBAJfZ83sbz6SCJKSaPn9U%3D", oauth_nonce="C7AB0CBC-9193-44EE-AFC1-6FE3BA51F048", oauth_signature_method="HMAC-SHA1"' }, body=body, ) self.http_client.fetch(request, self.stop) response = self.wait() self.assertEqual(response.code, 200) self.assertEqual(response.body, "<mediaurl>http://s.mltshp.com/r/1.png</mediaurl>") sf = Sharedfile.get("id = %s", 1) self.assertEqual(sf.id, 1) self.assertEqual(sf.get_title(), message.replace('\n', '').replace('\r', '')) self.assertEqual(sf.user_id, self.user.id)
def on_thumbnail_response(self, response): if response.code != 200: self.render( "tools/save-video-error.html", message= "We could not load the thumbnail for this file and therefore could not save this video. Please contact support." ) return #if the thumbnail url needs to be extracted (Flickr) let's see if # we got back HTML that points to the thumbnail if self.oembed_doc['provider_name'] == "Flickr" and response.headers[ 'Content-Type'] == 'text/html; charset=utf-8': #if we're here, that means we need to extract the thumbnail and make a call to the actual jpg s = re.search( '<link rel="image_src" href="http://farm(\d).static.flickr.com/(\d+)/(\d+)_([a-zA-Z0-9]+)_m.jpg">', response.body) try: if s and s.group(0) and s.group(1) and s.group(2) and s.group( 3) and s.group(4): self.oembed_doc[ 'thumbnail_url'] = "http://farm%s.static.flickr.com/%s/%s_%s_b.jpg" % ( s.group(1), s.group(2), s.group(3), s.group(4)) request = HTTPRequest(self.oembed_doc['thumbnail_url'], 'GET') http = tornado.httpclient.AsyncHTTPClient() http.fetch(request, self.on_thumbnail_response) except: self.render( "tools/save-video-error.html", message= "We could not load the thumbnail for this file and therefore could not save this video. Please contact support." ) return elif self.oembed_doc['provider_name'] == "Vine" and response.headers[ 'Content-Type'] == 'text/html; charset=utf-8': # if we're here, that means we need to extract the thumbnail and make a call to the actual jpg # use BeautfilSoup to parse for the title and meta tag. We'll do this bit of danger in a # try block and shrug if something bad happens try: soup = BeautifulSoup( response.body, convertEntities=BeautifulSoup.HTML_ENTITIES) self.oembed_doc['title'] = soup.title.text thumbnail = soup.find('meta', {"property": "og:image"}) if thumbnail: self.oembed_doc['thumbnail_url'] = thumbnail.attrMap[ 'content'] request = HTTPRequest(self.oembed_doc['thumbnail_url'], 'GET') http = tornado.httpclient.AsyncHTTPClient() http.fetch(request, self.on_thumbnail_response) return except: pass # either we failed to find a thumbnail url, or an exception was raised # while attempting to fetch. self.render( "tools/save-video-error.html", message= "We could not load the thumbnail for this file and therefore could not save this video. Please contact support." ) else: # save the response url = self.get_argument('url') current_user = self.get_current_user_object() sha1_key = Sourcefile.get_sha1_file_key(file_path=None, file_data=url) thumbnail_path = "%s/%s" % (options.uploaded_files, sha1_key) fh = open(thumbnail_path, 'wb') fh.write(response.body) fh.close() source_file = Sourcefile.create_from_json_oembed( link=url, oembed_doc=self.oembed_doc, thumbnail_file_path=thumbnail_path) #cleanup if not options.debug: try: os.remove(thumbnail_path) except: pass title = '' if self.oembed_doc.has_key('title'): title = self.oembed_doc['title'] shared_file = Sharedfile(user_id=current_user.id, name=url, content_type='text/html', source_id=source_file.id, title=title, source_url=url) shared_file.save() share_key = base36encode(shared_file.id) shared_file.share_key = share_key shared_file.save() user_shake = Shake.get('user_id = %s and type=%s', current_user.id, 'user') shared_file.add_to_shake(self.destination_shake) if self.oembed_doc.has_key('description'): shared_file.description = self.oembed_doc['description'] self.write({'path': "/p/%s" % (share_key)}) self.finish()
def test_sha1_file_encoding(self): sha1_key = Sourcefile.get_sha1_file_key( os.path.join(os.path.dirname(os.path.dirname(__file__)), "files/love.gif")) self.assertEqual("ac7180f6b038d5ae4f2297989e39a900995bb8fc", sha1_key)