Example #1
0
    def test_upload_size_quote(self) -> None:
        """
        User quote for uploading should not be exceeded
        """
        self.login(self.example_email("hamlet"))

        d1 = StringIO("zulip!")
        d1.name = "dummy_1.txt"
        result = self.client_post("/json/user_uploads", {'file': d1})
        d1_path_id = re.sub('/user_uploads/', '', result.json()['uri'])
        d1_attachment = Attachment.objects.get(path_id = d1_path_id)
        self.assert_json_success(result)

        """
        Below we set size quota to the limit without 1 upload(1GB - 11 bytes).
        """
        d1_attachment.size = UserProfile.DEFAULT_UPLOADS_QUOTA - 11
        d1_attachment.save()

        d2 = StringIO("zulip!")
        d2.name = "dummy_2.txt"
        result = self.client_post("/json/user_uploads", {'file': d2})
        self.assert_json_success(result)

        d3 = StringIO("zulip!")
        d3.name = "dummy_3.txt"
        result = self.client_post("/json/user_uploads", {'file': d3})
        self.assert_json_error(result, "Upload would exceed your maximum quota.")
Example #2
0
    def test_delete_old_unclaimed_attachments(self) -> None:
        # Upload some files and make them older than a weeek
        self.login(self.example_email("hamlet"))
        d1 = StringIO("zulip!")
        d1.name = "dummy_1.txt"
        result = self.client_post("/json/user_uploads", {'file': d1})
        d1_path_id = re.sub('/user_uploads/', '', result.json()['uri'])

        d2 = StringIO("zulip!")
        d2.name = "dummy_2.txt"
        result = self.client_post("/json/user_uploads", {'file': d2})
        d2_path_id = re.sub('/user_uploads/', '', result.json()['uri'])

        two_week_ago = timezone_now() - datetime.timedelta(weeks=2)
        d1_attachment = Attachment.objects.get(path_id = d1_path_id)
        d1_attachment.create_time = two_week_ago
        d1_attachment.save()
        self.assertEqual(str(d1_attachment), u'<Attachment: dummy_1.txt>')
        d2_attachment = Attachment.objects.get(path_id = d2_path_id)
        d2_attachment.create_time = two_week_ago
        d2_attachment.save()

        # Send message referring only dummy_1
        self.subscribe(self.example_user("hamlet"), "Denmark")
        body = "Some files here ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")"
        self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test")

        # dummy_2 should not exist in database or the uploads folder
        do_delete_old_unclaimed_attachments(2)
        self.assertTrue(not Attachment.objects.filter(path_id = d2_path_id).exists())
        self.assertTrue(not delete_message_image(d2_path_id))
Example #3
0
def parse_attachment(message_part):
    '''Parse an email message part into an attachment object.'''
    content_disposition = message_part.get("Content-Disposition", None)
    if content_disposition:
        dispositions = content_disposition.strip().split(";")
        if bool(content_disposition and dispositions[0].lower() == "attachment"):
            file_data = message_part.get_payload(decode=True)
            attachment = StringIO(file_data)
            attachment.content_type = message_part.get_content_type()
            attachment.size = len(file_data)
            attachment.name = message_part.get_filename()
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None

            for param in dispositions[1:]:
                name,value = param.split("=")
                name = name.lower()

                if name == "filename":
                    attachment.name = value
                elif name == "create-date":
                    attachment.create_date = value
                    attachment.ctime = parse_time(value)
                elif name == "modification-date":
                    attachment.mod_date = value
                    attachment.mtime = parse_time(value)
                elif name == "read-date":
                    attachment.read_date = value
                    attachment.atime = parse_time(value)
            return attachment
    return None
Example #4
0
    def test_check_attachment_reference_update(self) -> None:
        f1 = StringIO("file1")
        f1.name = "file1.txt"
        f2 = StringIO("file2")
        f2.name = "file2.txt"
        f3 = StringIO("file3")
        f3.name = "file3.txt"

        self.login(self.example_email("hamlet"))
        result = self.client_post("/json/user_uploads", {'file': f1})
        f1_path_id = re.sub('/user_uploads/', '', result.json()['uri'])

        result = self.client_post("/json/user_uploads", {'file': f2})
        f2_path_id = re.sub('/user_uploads/', '', result.json()['uri'])

        self.subscribe(self.example_user("hamlet"), "test")
        body = ("[f1.txt](http://localhost:9991/user_uploads/" + f1_path_id + ")"
                "[f2.txt](http://localhost:9991/user_uploads/" + f2_path_id + ")")
        msg_id = self.send_stream_message(self.example_email("hamlet"), "test", body, "test")

        result = self.client_post("/json/user_uploads", {'file': f3})
        f3_path_id = re.sub('/user_uploads/', '', result.json()['uri'])

        new_body = ("[f3.txt](http://localhost:9991/user_uploads/" + f3_path_id + ")"
                    "[f2.txt](http://localhost:9991/user_uploads/" + f2_path_id + ")")
        result = self.client_patch("/json/messages/" + str(msg_id), {
            'message_id': msg_id,
            'content': new_body
        })
        self.assert_json_success(result)

        message = Message.objects.get(id=msg_id)
        f1_attachment = Attachment.objects.get(path_id=f1_path_id)
        f2_attachment = Attachment.objects.get(path_id=f2_path_id)
        f3_attachment = Attachment.objects.get(path_id=f3_path_id)

        self.assertTrue(message not in f1_attachment.messages.all())
        self.assertTrue(message in f2_attachment.messages.all())
        self.assertTrue(message in f3_attachment.messages.all())

        # Delete all the attachments from the message
        new_body = "(deleted)"
        result = self.client_patch("/json/messages/" + str(msg_id), {
            'message_id': msg_id,
            'content': new_body
        })
        self.assert_json_success(result)

        message = Message.objects.get(id=msg_id)
        f1_attachment = Attachment.objects.get(path_id=f1_path_id)
        f2_attachment = Attachment.objects.get(path_id=f2_path_id)
        f3_attachment = Attachment.objects.get(path_id=f3_path_id)
        self.assertTrue(message not in f1_attachment.messages.all())
        self.assertTrue(message not in f2_attachment.messages.all())
        self.assertTrue(message not in f3_attachment.messages.all())
Example #5
0
    def test_multiple_upload_failure(self) -> None:
        """
        Attempting to upload two files should fail.
        """
        self.login(self.example_email("hamlet"))
        fp = StringIO("bah!")
        fp.name = "a.txt"
        fp2 = StringIO("pshaw!")
        fp2.name = "b.txt"

        result = self.client_post("/json/user_uploads", {'f1': fp, 'f2': fp2})
        self.assert_json_error(result, "You may only upload one file at a time")
Example #6
0
    def test_extract_param_encoding(self):
        fake_f = StringIO("""
            <html>
                <head>
                    <meta charset="utf-8">
                    <meta name="haystack-test" content="test 1234">
                    <title>Test Title ☃&#x2603;</title>
                </head>
                    <body>foobar</body>
            </html>
        """)
        fake_f.name = "test.html"
        
        # Insert a doc with extra metadata
        ascii_value = u'aeiou'
        nonascii_value = u'áéëíóúüñÁÉËÍÓÚÜ'
        
        extracted = self.solr.extract(fake_f, extractOnly=False, **{
                                        'literal.id': '29A',
                                        'literal.title': nonascii_value,
                                        'literal.subject': ascii_value,
                                        'commit': 'true',
                                      })

        res = self.solr.search('id:29A')
        # Check if document are added
        self.assertEqual(len(res), 1)
        # Check for ascii and non-ascii values
        doc = res.docs[0] 
        self.assertEqual(doc['id'], '29A')
        self.assertEqual(doc['subject'], ascii_value)
        self.assertEqual(doc['title'], [nonascii_value])
Example #7
0
    def test_multiple_claim_attachments_different_owners(self) -> None:
        """This test tries to claim the same attachment more than once, first
        with a private stream and then with different recipients."""
        self.login(self.example_email("hamlet"))
        d1 = StringIO("zulip!")
        d1.name = "dummy_1.txt"
        result = self.client_post("/json/user_uploads", {'file': d1})
        d1_path_id = re.sub('/user_uploads/', '', result.json()['uri'])

        self.make_stream("private_stream", invite_only=True)
        self.subscribe(self.example_user("hamlet"), "private_stream")

        # First, send the mesasge to the new private stream.
        body = "First message ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")"
        self.send_stream_message(self.example_email("hamlet"), "private_stream", body, "test")
        self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_realm_public)
        self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 1)

        # Then, try having a user who didn't receive the message try to publish it, and fail
        body = "Illegal message ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")"
        self.send_stream_message(self.example_email("cordelia"), "Denmark", body, "test")
        self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 1)
        self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_realm_public)

        # Then, have the owner PM it to another user, giving that other user access.
        body = "Second message ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")"
        self.send_personal_message(self.example_email("hamlet"), self.example_email("othello"), body)
        self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 2)
        self.assertFalse(Attachment.objects.get(path_id=d1_path_id).is_realm_public)

        # Then, have that new recipient user publish it.
        body = "Third message ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")"
        self.send_stream_message(self.example_email("othello"), "Denmark", body, "test")
        self.assertEqual(Attachment.objects.get(path_id=d1_path_id).messages.count(), 3)
        self.assertTrue(Attachment.objects.get(path_id=d1_path_id).is_realm_public)
Example #8
0
def test_arc_v2_writer():
    "Try writing records to an ARC V2 file. This is what API will feel like to a user of the library"
    now = "20120302193210"
    file_headers = dict(ip_address = "127.0.0.1",
                        date = now,
                        org = "Internet Archive")

    opfile = StringIO()
    opfile.name = "sample.arc" # Necessary since only file objects in Python have names.

    f = arc.ARCFile(fileobj = opfile, file_headers = file_headers)
    for payload in "Payload1 Payload2".split():
        header = arc.ARCHeader(url = "http://archive.org",
                               ip_address = "127.0.0.1",
                               date = "20120301093000",
                               content_type = "text/html",
                               length = "500",
                               result_code = "200",
                               checksum = "a123456",
                               location = "http://www.archive.org",
                               offset = "300",
                               filename = "sample.arc.gz")
        r = arc.ARCRecord(headers = header, payload = payload)
        f.write(r)
    assert opfile.getvalue() == "filedesc://sample.arc 127.0.0.1 20120302193210 text/plain 200 - - 0 sample.arc 113\n2 0 Internet Archive\nURL IP-address Archive-date Content-type Result-code Checksum Location Offset Filename Archive-length\n\nhttp://archive.org 127.0.0.1 20120301093000 text/html 200 a123456 http://www.archive.org 300 sample.arc.gz 500\nPayload1\n\nhttp://archive.org 127.0.0.1 20120301093000 text/html 200 a123456 http://www.archive.org 300 sample.arc.gz 500\nPayload2\n\n"
    f.close()
Example #9
0
    def test_with_thumbor_disabled(self) -> None:
        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.jpeg"

        result = self.client_post("/json/user_uploads", {'file': fp})
        self.assert_json_success(result)
        json = ujson.loads(result.content)
        self.assertIn("uri", json)
        uri = json["uri"]
        base = '/user_uploads/'
        self.assertEqual(base, uri[:len(base)])

        quoted_uri = urllib.parse.quote(uri[1:], safe='')

        with self.settings(THUMBOR_URL=''):
            result = self.client_get("/thumbnail?url=%s&size=original" % (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        self.assertEqual(uri, result.url)

        uri = 'https://www.google.com/images/srpr/logo4w.png'
        quoted_uri = urllib.parse.quote(uri, safe='')
        with self.settings(THUMBOR_URL=''):
            result = self.client_get("/thumbnail?url=%s&size=original" % (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        self.assertEqual(uri, result.url)

        uri = 'http://www.google.com/images/srpr/logo4w.png'
        quoted_uri = urllib.parse.quote(uri, safe='')
        with self.settings(THUMBOR_URL=''):
            result = self.client_get("/thumbnail?url=%s&size=original" % (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        base = 'https://external-content.zulipcdn.net/7b6552b60c635e41e8f6daeb36d88afc4eabde79/687474703a2f2f7777772e676f6f676c652e636f6d2f696d616765732f737270722f6c6f676f34772e706e67'
        self.assertEqual(base, result.url)
Example #10
0
    def test_simple(self, config_mock, savefig_mock):
        config_mock.return_value = self.config
        amino_csv = StringIO("""\
seed,region,q-cutoff,query.aa.pos,refseq.aa.pos,\
A,C,D,E,F,G,H,I,K,L,M,N,P,Q,R,S,T,V,W,Y,*,X,partial,del,ins,clip,v3_overlap
R1-seed,R1,15,100,1,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
R1-seed,R1,15,101,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0
R1-seed,R1,15,102,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0
""")
        expected_scores = """\
project,region,seed,q.cut,min.coverage,which.key.pos,off.score,on.score
R1,R1,R1-seed,15,5,1,-1,1
R1-and-R2,R1,R1-seed,15,5,1,-1,1
"""
        scores_csv = StringIO()
        amino_csv.name = 'E1234.amino.csv'
        expected_calls = [call('E1234.R1.R1.png'),
                          call('E1234.R1.R1.details.png'),
                          call('E1234.R1-and-R2.R1.png'),
                          call('E1234.R1-and-R2.R1.details.png')]

        coverage_plot(amino_csv,
                      coverage_scores_csv=scores_csv,
                      coverage_maps_prefix='E1234')

        self.assertEqual(expected_calls, savefig_mock.mock_calls)
        self.assertEqual(expected_scores, scores_csv.getvalue())
Example #11
0
    def test_file_upload_authed(self) -> None:
        """
        A call to /json/user_uploads should return a uri and actually create an
        entry in the database. This entry will be marked unclaimed till a message
        refers it.
        """
        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.txt"

        result = self.client_post("/json/user_uploads", {'file': fp})
        self.assert_json_success(result)
        self.assertIn("uri", result.json())
        uri = result.json()["uri"]
        base = '/user_uploads/'
        self.assertEqual(base, uri[:len(base)])

        # In the future, local file requests will follow the same style as S3
        # requests; they will be first authenthicated and redirected
        self.assert_url_serves_contents_of_file(uri, b"zulip!")

        # check if DB has attachment marked as unclaimed
        entry = Attachment.objects.get(file_name='zulip.txt')
        self.assertEqual(entry.is_claimed(), False)

        self.subscribe(self.example_user("hamlet"), "Denmark")
        body = "First message ...[zulip.txt](http://localhost:9991" + uri + ")"
        self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test")
        self.assertIn('title="zulip.txt"', self.get_last_message().rendered_content)
Example #12
0
    def test_file_upload_authed(self) -> None:
        """
        A call to /json/user_uploads should return a uri and actually create an object.
        """
        conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY)
        conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET)

        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.txt"

        result = self.client_post("/json/user_uploads", {'file': fp})
        self.assert_json_success(result)
        self.assertIn("uri", result.json())
        base = '/user_uploads/'
        uri = result.json()['uri']
        self.assertEqual(base, uri[:len(base)])

        response = self.client_get(uri)
        redirect_url = response['Location']

        self.assertEqual(b"zulip!", urllib.request.urlopen(redirect_url).read().strip())

        self.subscribe(self.example_user("hamlet"), "Denmark")
        body = "First message ...[zulip.txt](http://localhost:9991" + uri + ")"
        self.send_stream_message(self.example_email("hamlet"), "Denmark", body, "test")
        self.assertIn('title="zulip.txt"', self.get_last_message().rendered_content)
Example #13
0
    def test_extract(self):
        fake_f = StringIO("""
            <html>
                <head>
                    <meta charset="utf-8">
                    <meta name="haystack-test" content="test 1234">
                    <title>Test Title ☃&#x2603;</title>
                </head>
                    <body>foobar</body>
            </html>
        """)
        fake_f.name = "test.html"
        extracted = yield self.solr.extract(fake_f)

        # Verify documented response structure:
        self.assertIn('contents', extracted)
        self.assertIn('metadata', extracted)

        self.assertIn('foobar', extracted['contents'])

        m = extracted['metadata']

        self.assertEqual([fake_f.name], m['stream_name'])

        self.assertIn('haystack-test', m, "HTML metadata should have been extracted!")
        self.assertEqual(['test 1234'], m['haystack-test'])

        # Note the underhanded use of a double snowman to verify both that Tika
        # correctly decoded entities and that our UTF-8 characters survived the
        # round-trip:
        self.assertEqual(['Test Title ☃☃'], m['title'])
Example #14
0
 def setUp(self):
     map_content = StringIO("3 2\n. # &\n+ . *")
     map_content.name = "map0.txt"
     self.world = World(map_content, False)
     self.player1 = Player(1, self.world)
     self.world.add_player(self.player1)
     self.world.game_start()
Example #15
0
    def test_delete_message_image_local(self) -> None:
        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.txt"
        result = self.client_post("/json/user_uploads", {'file': fp})

        path_id = re.sub('/user_uploads/', '', result.json()['uri'])
        self.assertTrue(delete_message_image(path_id))
Example #16
0
def create_run_command():
    """ Create the remote_run command to be executed """
    puts("Creating run_server.")
    with lcd(env.localfolder):
        with cd(env.REPOSITORY_FOLDER):
            run_temp_file = StringIO(run_command_content())
            run_temp_file.name = "run_server"  # to show it as fabric file representation
            put(run_temp_file, posixpath.join(env.REPOSITORY_FOLDER, 'run_server'))
            run('chmod +x run_server')
Example #17
0
 def setUp(self):
     map_content = StringIO("3 2\n& & &\n& & &")
     map_content.name = "map0.txt"
     self.world = World(map_content, False)
     self.player1 = Player(1, self.world)
     self.player2 = Player(2, self.world)
     self.player3 = Player(3, self.world)
     self.player4 = Player(4, self.world)
     self.test_team = Team()
     self.test_team2 = Team()
 def get_file(self):
     self._update_object()
     response = requests.get(self._list_object.SITE + self.location)
     response = requests.get(response.content.decode())
     buff = StringIO()
     buff.content_type = response.headers['content-type']
     buff.name = self.filename
     buff.write(response.content.decode())
     buff.seek(0)
     #buff.close()
     return buff
Example #19
0
    def test_file_name(self) -> None:
        """
        Unicode filenames should be processed correctly.
        """
        self.login(self.example_email("hamlet"))
        for expected in ["Здравейте.txt", "test"]:
            fp = StringIO("bah!")
            fp.name = urllib.parse.quote(expected)

            result = self.client_post("/json/user_uploads", {'f1': fp})
            assert sanitize_name(expected) in result.json()['uri']
Example #20
0
    def test_file_download_unauthed(self) -> None:
        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.txt"
        result = self.client_post("/json/user_uploads", {'file': fp})
        uri = result.json()["uri"]

        self.logout()
        response = self.client_get(uri)
        self.assert_json_error(response, "Not logged in: API authentication or user session required",
                               status_code=401)
Example #21
0
    def test_s3_source_type(self) -> None:
        def get_file_path_urlpart(uri: str, size: str='') -> str:
            base = '/user_uploads/'
            url_in_result = 'smart/filters:no_upscale()/%s/source_type/s3'
            if size:
                url_in_result = '/%s/%s' % (size, url_in_result)
            upload_file_path = uri[len(base):]
            hex_uri = base64.urlsafe_b64encode(upload_file_path.encode()).decode('utf-8')
            return url_in_result % (hex_uri)

        conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY)
        conn.create_bucket(settings.S3_AUTH_UPLOADS_BUCKET)

        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.jpeg"

        result = self.client_post("/json/user_uploads", {'file': fp})
        self.assert_json_success(result)
        json = ujson.loads(result.content)
        self.assertIn("uri", json)
        uri = json["uri"]
        base = '/user_uploads/'
        self.assertEqual(base, uri[:len(base)])

        quoted_uri = urllib.parse.quote(uri[1:], safe='')

        # Test original image size.
        result = self.client_get("/thumbnail?url=%s&size=original" % (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        expected_part_url = get_file_path_urlpart(uri)
        self.assertIn(expected_part_url, result.url)

        # Test thumbnail size.
        result = self.client_get("/thumbnail?url=%s&size=thumbnail" % (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        expected_part_url = get_file_path_urlpart(uri, '0x100')
        self.assertIn(expected_part_url, result.url)

        # Tests the /api/v1/thumbnail api endpoint with standard API auth
        self.logout()
        result = self.api_get(
            self.example_email("hamlet"),
            '/thumbnail?url=%s&size=original' %
            (quoted_uri,))
        self.assertEqual(result.status_code, 302, result)
        expected_part_url = get_file_path_urlpart(uri)
        self.assertIn(expected_part_url, result.url)

        # Test with another user trying to access image using thumbor.
        self.login(self.example_email("iago"))
        result = self.client_get("/thumbnail?url=%s&size=original" % (quoted_uri))
        self.assertEqual(result.status_code, 403, result)
        self.assert_in_response("You are not authorized to view this file.", result)
Example #22
0
 def lbcb(name, doc):
     # This only applies to 'start' Documents.
     if name != 'start':
         return
     plan_name = doc.get('plan_name', '')
     body = long_dispatch[plan_name]
     desc = desc_dispatch[plan_name]
     atch = StringIO(body.render(start=doc))
     # monkey-patch a 'name' attribute onto StringIO
     atch.name = 'long_description.txt'
     desc = desc.render(start=doc)
     logbook_func(text=desc, attachments=[atch], ensure=True)
    def test_illuminafastq(self):
        fastq_file = StringIO(
            u"@M03543:47:C8LJ2ANXX:1:2209:1084:2044 1:N:0:NNNNNNNN+NNNNNNNN")
        fastq_filepath = (
            "Miseq/160511_M03543_0047_000000000-APE6Y/Data/Intensities/"
            "BaseCalls/Undetermined_S0_L001_R1_001.fastq.gz")
        fastq_file.name = fastq_filepath
        fq = IlluminaFastq(fastq_file)

        self.assertEqual(fq.machine_type, "Illumina-MiSeq")
        self.assertEqual(fq.date, "2016-05-11")
        self.assertEqual(fq.lane, "1")
        self.assertEqual(fq.filepath, fastq_filepath)
Example #24
0
    def test_realm_quota(self) -> None:
        """
        Realm quota for uploading should not be exceeded.
        """
        self.login(self.example_email("hamlet"))

        d1 = StringIO("zulip!")
        d1.name = "dummy_1.txt"
        result = self.client_post("/json/user_uploads", {'file': d1})
        d1_path_id = re.sub('/user_uploads/', '', result.json()['uri'])
        d1_attachment = Attachment.objects.get(path_id = d1_path_id)
        self.assert_json_success(result)

        realm = get_realm("zulip")
        realm.upload_quota_gb = 1
        realm.save(update_fields=['upload_quota_gb'])

        # The size of StringIO("zulip!") is 6 bytes. Setting the size of
        # d1_attachment to realm.upload_quota_bytes() - 11 should allow
        # us to upload only one more attachment.
        quota = realm.upload_quota_bytes()
        assert(quota is not None)
        d1_attachment.size = quota - 11
        d1_attachment.save(update_fields=['size'])

        d2 = StringIO("zulip!")
        d2.name = "dummy_2.txt"
        result = self.client_post("/json/user_uploads", {'file': d2})
        self.assert_json_success(result)

        d3 = StringIO("zulip!")
        d3.name = "dummy_3.txt"
        result = self.client_post("/json/user_uploads", {'file': d3})
        self.assert_json_error(result, "Upload would exceed your organization's upload quota.")

        realm.upload_quota_gb = None
        realm.save(update_fields=['upload_quota_gb'])
        result = self.client_post("/json/user_uploads", {'file': d3})
        self.assert_json_success(result)
Example #25
0
    def test_removed_file_download(self) -> None:
        '''
        Trying to download deleted files should return 404 error
        '''
        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.txt"
        result = self.client_post("/json/user_uploads", {'file': fp})

        destroy_uploads()

        response = self.client_get(result.json()["uri"])
        self.assertEqual(response.status_code, 404)
Example #26
0
    def test_file_too_big_failure(self) -> None:
        """
        Attempting to upload big files should fail.
        """
        self.login(self.example_email("hamlet"))
        fp = StringIO("bah!")
        fp.name = "a.txt"

        # Use MAX_FILE_UPLOAD_SIZE of 0, because the next increment
        # would be 1MB.
        with self.settings(MAX_FILE_UPLOAD_SIZE=0):
            result = self.client_post("/json/user_uploads", {'f1': fp})
        self.assert_json_error(result, 'Uploaded file is larger than the allowed limit of 0 MB')
Example #27
0
    def test_opt_cmd(self):
        config = StringIO(u"""[hubble]
            name=Test
            opt-cmd={0} ${{opt.option}} ${{name}}
            [dfw]
            cmd={1}
            SOME=Thing""".format(self.opt_cmd, self.cmd))
        config.name = "test.conf"

        ret, out, err = run_main(config, ["hubble", "-o", "my-option",
                                          "dfw", "--debug"])
        self.assertEqual(ret, 0)
        self.assertEqual(out['SOME'], "Thing")
        self.assertEqual(out['opt-injected-key'], "value")
Example #28
0
    def test_import_gmf_scenario_csv(self):
        test_data = StringIO(unicode('''\
SA	0.025	5.0	{0.2}	POINT(0.0 0.0)
SA	0.025	5.0	{1.4}	POINT(1.0 0.0)
SA	0.025	5.0	{0.6}	POINT(0.0 1.0)
PGA	\N	\N	{0.2,0.3}	POINT(0.0 0.0)
PGA	\N	\N	{1.4,1.5}	POINT(1.0 0.0)
PGA	\N	\N	{0.6,0.7}	POINT(0.0 1.0)
PGV	\N	\N	{0.2}	POINT(0.0 0.0)
PGV	\N	\N	{1.4}	POINT(1.0 0.0)
'''))
        test_data.name = 'test_data'
        out = import_gmf_scenario.import_gmf_scenario(test_data)
        n = models.GmfData.objects.filter(gmf__output=out).count()
        assert_equal(n, 8)  # 8 rows entered
Example #29
0
def upload_file(client):
    # type: (Client) -> None
    fp = StringIO("zulip")
    fp.name = "zulip.txt"

    # {code_example|start}
    # Upload a file
    # (Make sure that 'fp' is a file object)
    result = client.call_endpoint(
        'user_uploads',
        method='POST',
        files=[fp]
    )
    # {code_example|end}

    validate_against_openapi_schema(result, '/user_uploads', 'post', '200')
Example #30
0
    def test_with_different_sizes(self) -> None:
        def get_file_path_urlpart(uri: str, size: str='') -> str:
            base = '/user_uploads/'
            url_in_result = 'smart/filters:no_upscale()/%s/source_type/local_file'
            if size:
                url_in_result = '/%s/%s' % (size, url_in_result)
            upload_file_path = uri[len(base):]
            hex_uri = base64.urlsafe_b64encode(upload_file_path.encode()).decode('utf-8')
            return url_in_result % (hex_uri)

        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.jpeg"

        result = self.client_post("/json/user_uploads", {'file': fp})
        self.assert_json_success(result)
        json = ujson.loads(result.content)
        self.assertIn("uri", json)
        uri = json["uri"]
        base = '/user_uploads/'
        self.assertEqual(base, uri[:len(base)])

        # Test with size supplied as a query parameter.
        # size=thumbnail should return a 0x100 sized image.
        # size=original should return the original resolution image.
        quoted_uri = urllib.parse.quote(uri[1:], safe='')
        result = self.client_get("/thumbnail?url=%s&size=thumbnail" % (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        expected_part_url = get_file_path_urlpart(uri, '0x100')
        self.assertIn(expected_part_url, result.url)

        result = self.client_get("/thumbnail?url=%s&size=original" % (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        expected_part_url = get_file_path_urlpart(uri)
        self.assertIn(expected_part_url, result.url)

        # Test with size supplied as a query parameter where size is anything
        # else than original or thumbnail. Result should be an error message.
        result = self.client_get("/thumbnail?url=%s&size=480x360" % (quoted_uri))
        self.assertEqual(result.status_code, 403, result)
        self.assert_in_response("Invalid size.", result)

        # Test with no size param supplied. In this case as well we show an
        # error message.
        result = self.client_get("/thumbnail?url=%s" % (quoted_uri))
        self.assertEqual(result.status_code, 400, "Missing 'size' argument")
def test_update_gha_python_versions_zopefoundation():
    tests_yml = StringIO(
        textwrap.dedent("""\
        jobs:
          build:
            strategy:
              matrix:
                config:
                # [Python version, tox env]
                - ["3.8",   "lint"]
                - ["2.7",   "py27"]
                - ["3.5",   "py35"]
                - ["3.6",   "py36"]
                - ["3.7",   "py37"]
                - ["3.8",   "py38"]
                - ["3.9",   "py39"]
                - ["pypy2", "pypy"]
                - ["pypy3", "pypy3"]
                - ["3.8",   "coverage"]
            steps:
            - ...
    """))
    tests_yml.name = '.github/workflows/tests.yml'
    result = update_gha_python_versions(tests_yml, v(["2.7", "3.8", "3.9"]))
    assert "".join(result) == textwrap.dedent("""\
        jobs:
          build:
            strategy:
              matrix:
                config:
                # [Python version, tox env]
                - ["3.8",   "lint"]
                - ["2.7",   "py27"]
                - ["3.8",   "py38"]
                - ["3.9",   "py39"]
                - ["pypy2", "pypy"]
                - ["pypy3", "pypy3"]
                - ["3.8",   "coverage"]
            steps:
            - ...
    """)
Example #32
0
 def run(self):
     failed = []
     for library in self._options.libraries:
         try:
             xml_doc = StringIO()
             # LibraryDocumentation().save() calls close() for the underlying
             # file but closing StringIO object discards its data.
             # This is why close() is overridden below.
             xml_doc.original_close = xml_doc.close
             try:
                 try:
                     if library.endswith('.xml'):
                         with open(library) as xml_file:
                             xml_doc.write(xml_file.read())
                     else:
                         xml_doc.close = lambda: None
                         LibraryDocumentation(library).save(xml_doc, 'xml')
                 except DataError as e:
                     message = "Library not found" if 'ImportError' in e.message else e.message
                     failed.append(library)
                     sys.stderr.write(
                         "Skipping '%s' due to an error: %s.\n" %
                         (library, message))
                     continue
                 xml_doc.name = library
                 self._uploader.upload_file(xml_doc, self._options.lib_name,
                                            self._options.lib_version)
                 sys.stdout.write("Updated documentation for '%s'.\n" %
                                  library)
             finally:
                 xml_doc.original_close()
         except DataError as e:
             failed.append(library)
             sys.stderr.write('%s: Remote error: %s\n' %
                              (os.path.basename(__file__), e.message))
     if failed:
         sys.stderr.write('\nERROR: Uploading %d file(s) failed:\n' %
                          len(failed))
         for name in failed:
             sys.stderr.write('* %s\n' % name)
         exit(1)
Example #33
0
    def test_extract(self):
        fake_f = StringIO("""
            <html>
                <head>
                    <meta charset="utf-8">
                    <meta name="haystack-test" content="test 1234">
                    <title>Test Title ☃&#x2603;</title>
                </head>
                    <body>foobar</body>
            </html>
        """)
        fake_f.name = "test.html"
        extracted = self.solr.extract(fake_f)
        # extract should default to 'update/extract' handler
        args, kwargs = self.solr._send_request.call_args
        self.assertTrue(args[1].startswith('update/extract'))

        # extract should support custom handlers
        with self.assertRaises(SolrError):
            self.solr.extract(fake_f, handler='fakehandler')
        args, kwargs = self.solr._send_request.call_args
        self.assertTrue(args[1].startswith('fakehandler'))

        # Verify documented response structure:
        self.assertIn('contents', extracted)
        self.assertIn('metadata', extracted)

        self.assertIn('foobar', extracted['contents'])

        m = extracted['metadata']

        self.assertEqual([fake_f.name], m['stream_name'])

        self.assertIn('haystack-test', m,
                      "HTML metadata should have been extracted!")
        self.assertEqual(['test 1234'], m['haystack-test'])

        # Note the underhanded use of a double snowman to verify both that Tika
        # correctly decoded entities and that our UTF-8 characters survived the
        # round-trip:
        self.assertEqual(['Test Title ☃☃'], m['title'])
Example #34
0
    def test_read_error(self):
        # given that there is no generic parser, emulate one like so
        def parser(text):
            raise ECMASyntaxError('Illegal input')

        def not_a_parser(text):
            raise Exception('Illegal input')

        stream = StringIO('var foo = "bar";')
        stream.name = 'somefile.js'
        with self.assertRaises(Exception) as e:
            io.read(parser, stream)

        self.assertEqual("Illegal input in 'somefile.js'", e.exception.args[0])

        stream = StringIO('var foo = "bar";')
        with self.assertRaises(Exception) as e:
            io.read(not_a_parser, stream)

        self.assertNotEqual("Illegal input in 'somefile.js'",
                            e.exception.args[0])
Example #35
0
def test_update_travis_yml_python_versions_3_10():
    travis_yml = StringIO(textwrap.dedent("""\
        language: python
        python:
          - 3.4
          - pypy3
        install: pip install -e .
        script: pytest tests
    """))
    travis_yml.name = '.travis.yml'
    result = update_travis_yml_python_versions(travis_yml, v(["3.9", "3.10"]))
    # Note: we cannot say '- 3.10', that's a float and evaluates to 3.1!
    assert "".join(result) == textwrap.dedent("""\
        language: python
        python:
          - 3.9
          - "3.10"
          - pypy3
        install: pip install -e .
        script: pytest tests
    """)
Example #36
0
def test_update_travis_yml_some_python_versions_as_strings():
    travis_yml = StringIO(textwrap.dedent("""\
        language: python
        python:
          - 3.9
          - "3.10"
          - pypy3
        install: pip install -e .
        script: pytest tests
    """))
    travis_yml.name = '.travis.yml'
    result = update_travis_yml_python_versions(travis_yml, v(["3.9", "3.10"]))
    assert "".join(result) == textwrap.dedent("""\
        language: python
        python:
          - 3.9
          - "3.10"
          - pypy3
        install: pip install -e .
        script: pytest tests
    """)
Example #37
0
def test_update_travis_yml_python_versions_drops_sudo():
    travis_yml = StringIO(textwrap.dedent("""\
        language: python
        sudo: false
        dist: xenial
        python:
          - 2.7
        install: pip install -e .
        script: pytest tests
    """))
    travis_yml.name = '.travis.yml'
    result = update_travis_yml_python_versions(travis_yml, v(["2.7", "3.7"]))
    assert "".join(result) == textwrap.dedent("""\
        language: python
        dist: xenial
        python:
          - 2.7
          - 3.7
        install: pip install -e .
        script: pytest tests
    """)
def test_update_travis_yml_python_versions_keeps_dev():
    travis_yml = StringIO(
        textwrap.dedent("""\
        language: python
        python:
          - 3.7
          - 3.8
          - 3.9-dev
        install: pip install -e .
        script: pytest tests
    """))
    travis_yml.name = '.travis.yml'
    result = update_travis_yml_python_versions(travis_yml, ["3.8"])
    assert "".join(result) == textwrap.dedent("""\
        language: python
        python:
          - 3.8
          - 3.9-dev
        install: pip install -e .
        script: pytest tests
    """)
Example #39
0
def convert(source_path, dest_path, sra_settings=None, validate_first=True):
    isa_json = isatab2json.convert(source_path, validate_first=validate_first)
    isa_json_fp = StringIO(json.dumps(isa_json))
    isa_json_fp.name = "BII-S-3.json"
    json2sra.convert(isa_json_fp,
                     dest_path,
                     sra_settings=sra_settings,
                     validate_first=False)
    logging.info("Conversion complete...")
    buffer = BytesIO()
    if os.path.isdir(dest_path):
        with ZipFile(buffer, 'w') as zip_file:
            # use relative dir_name to avoid absolute path on file names
            zipdir(dest_path, zip_file)
            print(zip_file.namelist())

            # clean up the target directory after the ZIP file has been closed
            # rmtree(sra_dir)

        buffer.seek(0)
        return buffer
Example #40
0
    def test_multiple_claim_attachments(self) -> None:
        """
        This test tries to claim the same attachment twice. The messages field in
        the Attachment model should have both the messages in its entry.
        """
        self.login(self.example_email("hamlet"))
        d1 = StringIO("zulip!")
        d1.name = "dummy_1.txt"
        result = self.client_post("/json/user_uploads", {'file': d1})
        d1_path_id = re.sub('/user_uploads/', '', result.json()['uri'])

        self.subscribe(self.example_user("hamlet"), "Denmark")
        body = "First message ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")"
        self.send_stream_message(self.example_email("hamlet"), "Denmark", body,
                                 "test")
        body = "Second message ...[zulip.txt](http://localhost:9991/user_uploads/" + d1_path_id + ")"
        self.send_stream_message(self.example_email("hamlet"), "Denmark", body,
                                 "test")

        self.assertEqual(
            Attachment.objects.get(path_id=d1_path_id).messages.count(), 2)
def _document_load_by_url(loader, url, loadingOptions):
    # type: (_Loader, str, LoadingOptions) -> Any
    if url in loadingOptions.idx:
        return _document_load(loader, loadingOptions.idx[url], url,
                              loadingOptions)

    text = loadingOptions.fetcher.fetch_text(url)
    if isinstance(text, bytes):
        textIO = StringIO(text.decode("utf-8"))
    else:
        textIO = StringIO(text)
    textIO.name = str(url)
    yaml = yaml_no_ts()
    result = yaml.load(textIO)
    add_lc_filename(result, url)

    loadingOptions.idx[url] = result

    loadingOptions = LoadingOptions(copyfrom=loadingOptions, fileuri=url)

    return _document_load(loader, result, url, loadingOptions)
Example #42
0
def test_update_travis_yml_python_versions():
    travis_yml = StringIO(
        textwrap.dedent("""\
        language: python
        python:
          - 2.7
          - pypy
        install: pip install -e .
        script: pytest tests
    """))
    travis_yml.name = '.travis.yml'
    result = update_travis_yml_python_versions(travis_yml, ["2.7", "3.4"])
    assert "".join(result) == textwrap.dedent("""\
        language: python
        python:
          - 2.7
          - 3.4
          - pypy
        install: pip install -e .
        script: pytest tests
    """)
Example #43
0
    def fetch(
        self,
        url: str,
        inject_ids: bool = True,
        content_types: Optional[List[str]] = None,
    ) -> IdxResultType:
        if url in self.idx:
            return self.idx[url]
        try:
            text = self.fetch_text(url, content_types=content_types)
            if isinstance(text, bytes):
                textIO = StringIO(text.decode("utf-8"))

            else:
                textIO = StringIO(text)
            textIO.name = str(url)
            yaml = YAML()
            yaml.preserve_quotes = True  # type: ignore
            attachments = yaml.load_all(textIO)
            result = cast(Union[CommentedSeq, CommentedMap], next(attachments))

            if self.allow_attachments is not None and self.allow_attachments(
                    result):
                i = 1
                for a in attachments:
                    self.idx[f"{url}#attachment-{i}"] = a
                    i += 1
            add_lc_filename(result, url)
        except MarkedYAMLError as e:
            raise to_validation_exception(e) from e
        if isinstance(result, CommentedMap) and inject_ids and bool(
                self.identifiers):
            for identifier in self.identifiers:
                if identifier not in result:
                    result[identifier] = url
                self.idx[self.expand_url(result[identifier],
                                         url,
                                         scoped_id=True)] = result
        self.idx[url] = result
        return result
Example #44
0
def test_update_travis_yml_python_versions_drops_pypy3():
    # yes this test case is massively unrealistic
    travis_yml = StringIO(textwrap.dedent("""\
        language: python
        python:
          - 2.7
          - 3.4
          - pypy
          - pypy3
        install: pip install -e .
        script: pytest tests
    """))
    travis_yml.name = '.travis.yml'
    result = update_travis_yml_python_versions(travis_yml, v(["2.7"]))
    assert "".join(result) == textwrap.dedent("""\
        language: python
        python:
          - 2.7
          - pypy
        install: pip install -e .
        script: pytest tests
     """)
Example #45
0
def convert(source_path, dest_path, sra_settings=None, validate_first=True):
    log.info("Converting ISA-Tab to JSON for %s", source_path)
    isa_json = isatab2json.convert(source_path, validate_first=validate_first)
    log.debug("Writing JSON to memory file")
    isa_json_fp = StringIO(json.dumps(isa_json))
    isa_json_fp.name = "BII-S-3.json"
    log.info("Converting JSON to SRA, writing to %s", dest_path)
    log.info("Using SRA settings %s", sra_settings)
    json2sra.convert(isa_json_fp,
                     dest_path,
                     sra_settings=sra_settings,
                     validate_first=False)
    log.info("Conversion from ISA-Tab to SRA complete")
    buffer = BytesIO()
    if os.path.isdir(dest_path):
        log.info("Zipping SRA files")
        with ZipFile(buffer, 'w') as zip_file:
            zipdir(dest_path, zip_file)
            log.debug("Zipped %s", zip_file.namelist())
        buffer.seek(0)
        log.info("Returning zipped files as memory file")
        return buffer
Example #46
0
 def check_xsend_links(name: Text, name_str_for_test: Text,
                       content_disposition: Text='') -> None:
     with self.settings(SENDFILE_BACKEND='sendfile.backends.nginx'):
         _get_sendfile.clear()  # To clearout cached version of backend from djangosendfile
         self.login(self.example_email("hamlet"))
         fp = StringIO("zulip!")
         fp.name = name
         result = self.client_post("/json/user_uploads", {'file': fp})
         uri = result.json()['uri']
         fp_path_id = re.sub('/user_uploads/', '', uri)
         fp_path = os.path.split(fp_path_id)[0]
         response = self.client_get(uri)
         _get_sendfile.clear()
         test_upload_dir = os.path.split(settings.LOCAL_UPLOADS_DIR)[1]
         self.assertEqual(response['X-Accel-Redirect'],
                          '/serve_uploads/../../'  + test_upload_dir +
                          '/files/' + fp_path + '/' + name_str_for_test)
         if content_disposition != '':
             self.assertIn('attachment;', response['Content-disposition'])
             self.assertIn(content_disposition, response['Content-disposition'])
         else:
             self.assertEqual(response.get('Content-disposition'), None)
Example #47
0
    def test_thumbnail_redirect(self) -> None:
        self.login("hamlet")
        fp = StringIO("zulip!")
        fp.name = "zulip.jpeg"

        result = self.client_post("/json/user_uploads", {"file": fp})
        self.assert_json_success(result)
        json = orjson.loads(result.content)
        self.assertIn("uri", json)
        uri = json["uri"]
        base = "/user_uploads/"
        self.assertEqual(base, uri[: len(base)])

        result = self.client_get("/thumbnail", {"url": uri[1:], "size": "full"})
        self.assertEqual(result.status_code, 302, result)
        self.assertEqual(uri, result["Location"])

        self.login("iago")
        result = self.client_get("/thumbnail", {"url": uri[1:], "size": "full"})
        self.assertEqual(result.status_code, 403, result)
        self.assert_in_response("You are not authorized to view this file.", result)

        uri = "https://www.google.com/images/srpr/logo4w.png"
        result = self.client_get("/thumbnail", {"url": uri, "size": "full"})
        self.assertEqual(result.status_code, 302, result)
        base = "https://external-content.zulipcdn.net/external_content/56c362a24201593891955ff526b3b412c0f9fcd2/68747470733a2f2f7777772e676f6f676c652e636f6d2f696d616765732f737270722f6c6f676f34772e706e67"
        self.assertEqual(base, result["Location"])

        uri = "http://www.google.com/images/srpr/logo4w.png"
        result = self.client_get("/thumbnail", {"url": uri, "size": "full"})
        self.assertEqual(result.status_code, 302, result)
        base = "https://external-content.zulipcdn.net/external_content/7b6552b60c635e41e8f6daeb36d88afc4eabde79/687474703a2f2f7777772e676f6f676c652e636f6d2f696d616765732f737270722f6c6f676f34772e706e67"
        self.assertEqual(base, result["Location"])

        uri = "//www.google.com/images/srpr/logo4w.png"
        result = self.client_get("/thumbnail", {"url": uri, "size": "full"})
        self.assertEqual(result.status_code, 302, result)
        base = "https://external-content.zulipcdn.net/external_content/676530cf4b101d56f56cc4a37c6ef4d4fd9b0c03/2f2f7777772e676f6f676c652e636f6d2f696d616765732f737270722f6c6f676f34772e706e67"
        self.assertEqual(base, result["Location"])
Example #48
0
    def test_with_different_THUMBOR_URL(self) -> None:
        self.login('hamlet')
        fp = StringIO("zulip!")
        fp.name = "zulip.jpeg"

        result = self.client_post("/json/user_uploads", {'file': fp})
        self.assert_json_success(result)
        json = ujson.loads(result.content)
        self.assertIn("uri", json)
        uri = json["uri"]
        base = '/user_uploads/'
        self.assertEqual(base, uri[:len(base)])

        quoted_uri = urllib.parse.quote(uri[1:], safe='')
        hex_uri = base64.urlsafe_b64encode(uri.encode()).decode('utf-8')
        with self.settings(THUMBOR_URL='http://test-thumborhost.com'):
            result = self.client_get(f"/thumbnail?url={quoted_uri}&size=full")
        self.assertEqual(result.status_code, 302, result)
        base = 'http://test-thumborhost.com/'
        self.assertEqual(base, result.url[:len(base)])
        expected_part_url = '/smart/filters:no_upscale()/' + hex_uri + '/source_type/local_file'
        self.assertIn(expected_part_url, result.url)
Example #49
0
    def test_write_error_handled_callable_closed(self):
        # streams
        root = mktemp()
        output_stream = StringIO()
        output_stream.name = join(root, 'packed.js')
        closed = []

        def close():
            closed.append(True)

        output_stream.close = close

        def f_output_stream():
            return output_stream

        def f_error():
            raise IOError('some error happened')

        definitions = {
            'Node': (
                Attr(attr='text'),
                Text(value=';'),
            )
        }

        # the program node; attributes are assigned to mimic a real one
        program = Node()
        program.text = 'hello'
        program.sourcepath = join(root, 'program.js')
        program._token_map = {'hello': [(0, 1, 1)]}

        unparser = BaseUnparser(definitions)
        with self.assertRaises(IOError):
            io.write(unparser, [program], f_output_stream, f_error)

        self.assertEqual(1, len(closed))
        self.assertEqual('hello;', output_stream.getvalue())
        self.assertNotIn('program.js', output_stream.getvalue())
Example #50
0
    def test_file_download_authorization_invite_only(self) -> None:
        subscribed_users = [self.example_email("hamlet"), self.example_email("iago")]
        unsubscribed_users = [self.example_email("othello"), self.example_email("prospero")]
        realm = get_realm("zulip")
        for email in subscribed_users:
            self.subscribe(get_user(email, realm), "test-subscribe")

        # Make the stream private
        stream = Stream.objects.get(name='test-subscribe')
        stream.invite_only = True
        stream.save()

        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.txt"
        result = self.client_post("/json/user_uploads", {'file': fp})
        uri = result.json()['uri']
        fp_path_id = re.sub('/user_uploads/', '', uri)
        body = "First message ...[zulip.txt](http://localhost:9991/user_uploads/" + fp_path_id + ")"
        self.send_stream_message(self.example_email("hamlet"), "test-subscribe", body, "test")
        self.logout()

        # Subscribed user should be able to view file
        for user in subscribed_users:
            self.login(user)
            response = self.client_get(uri)
            self.assertEqual(response.status_code, 200)
            data = b"".join(response.streaming_content)
            self.assertEqual(b"zulip!", data)
            self.logout()

        # Unsubscribed user should not be able to view file
        for user in unsubscribed_users:
            self.login(user)
            response = self.client_get(uri)
            self.assertEqual(response.status_code, 403)
            self.assert_in_response("You are not authorized to view this file.", response)
            self.logout()
Example #51
0
def test_update_travis_yml_python_versions_drops_dist_trusty(monkeypatch):
    monkeypatch.setitem(
        XENIAL_SUPPORTED_PYPY_VERSIONS, 'pypy', 'pypy2.7-6.0.0')
    travis_yml = StringIO(textwrap.dedent("""\
        language: python
        dist: trusty
        python:
          - 2.7
          - pypy
        install: pip install -e .
        script: pytest tests
    """))
    travis_yml.name = '.travis.yml'
    result = update_travis_yml_python_versions(travis_yml, v(["2.7", "3.7"]))
    assert "".join(result) == textwrap.dedent("""\
        language: python
        python:
          - 2.7
          - 3.7
          - pypy2.7-6.0.0
        install: pip install -e .
        script: pytest tests
    """)
Example #52
0
def test_update_travis_yml_python_versions_matrix_preserve_quotes():
    travis_yml = StringIO(textwrap.dedent("""\
        language: python
        matrix:
          include:
            - python: "2.7"
            - python: "3.3"
            - language: c
        install: pip install tox
        script: tox -e py
    """))
    travis_yml.name = '.travis.yml'
    result = update_travis_yml_python_versions(travis_yml, v(["3.8", "3.9"]))
    assert "".join(result) == textwrap.dedent("""\
        language: python
        matrix:
          include:
            - python: "3.8"
            - python: "3.9"
            - language: c
        install: pip install tox
        script: tox -e py
    """)
Example #53
0
    def test_add_recipients_from_csv(self, end):
        user = self.login()

        mlist = MailingList.objects.create(user=user, name='ml01')
        url = reverse('emails__add_recipients_from_csv', args=(mlist.id,))
        self.assertGET200(url)

        # TODO: it seems django validator does not manages address with unicode chars:
        #       is it a problem
        # recipients = ['*****@*****.**', 'jet.blä[email protected]']
        recipient1 = '*****@*****.**'
        recipient2 = '*****@*****.**'

        csvfile = StringIO(end.join([' ' + recipient1, recipient2 + ' ']) + ' ')
        csvfile.name = 'recipients.csv'  # Django uses this

        self.assertNoFormError(self.client.post(url, data={'recipients': csvfile}))
        self.assertSetEqual(
            {recipient1, recipient2},
            {r.address for r in mlist.emailrecipient_set.all()}
        )

        csvfile.close()
    def test_testgroup_set_group(self):
        # mostly to test the testing
        user = User.objects.get(username = '******')

        user2 = User.objects.create_user('user2', '*****@*****.**', 'demo')
        user2.mat_number = 22222
        user2.groups.add(self.testgroup)
        user2.save()

        f = StringIO("11111\n33333")
        f.name="foo.txt"
        response = self.client.post(
            reverse('admin:import_matriculation_list', args=[self.testgroup.id]),
            { 'mat_number_file': f,
              'remove_others' : True,
              'create_users': True},
            follow=True)
        self.assertContains(response, "2 users added to group test, 1 removed, 0 already in group. 1 new users created.")

        user3 = User.objects.get(mat_number = 33333)

        self.assertEqual(set( u.mat_number for u in User.objects.filter(groups = self.testgroup)),
                             set( u.mat_number for u in [user, user3]))
Example #55
0
    def test_get_environments(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('env')
        parser.add_argument('--user', default='', required=False)
        parser.add_argument('--option')
        args = parser.parse_args(['blah'])

        file = StringIO(u"[hubble]\n"
                        "name=My name is ${FIRST} ${last}\n"
                        "[prod-meta]\n"
                        "meta=['name', 'place']\n"
                        "[name]\n"
                        "FIRST=Derrick\n"
                        "last=Wippler\n")
        file.name = "test-config.ini"
        config = parse_configs([file], default_section='hubble')
        env = get_environments(args, 'name', config)

        self.assertIn('name', env[0])
        self.assertEqual(env[0]['name'].value, 'My name is Derrick Wippler')

        self.assertIn('FIRST', env[0])
        self.assertIn('last', env[0])
def test_update_appveyor_yml_leave_unknown_python_versions_alone():
    appveyor_yml = StringIO(
        textwrap.dedent("""\
        environment:
          matrix:
            - PYTHON: c:\\python27
            - PYTHON: c:\\python36
            - PYTHON: c:\\custompython
    """))
    appveyor_yml.name = 'appveyor.yml'
    result = update_appveyor_yml_python_versions(appveyor_yml,
                                                 v([
                                                     '3.6',
                                                     '3.7',
                                                 ]))
    assert result is not None
    assert ''.join(result) == textwrap.dedent("""\
        environment:
          matrix:
            - PYTHON: c:\\python36
            - PYTHON: c:\\python37
            - PYTHON: c:\\custompython
    """)
def test_update_appveyor_yml_python_versions():
    appveyor_yml = StringIO(
        textwrap.dedent(r"""
        environment:
           matrix:
            - PYTHON: "c:\\python27"
            - PYTHON: "c:\\python36"
    """).lstrip('\n'))
    appveyor_yml.name = 'appveyor.yml'
    result = update_appveyor_yml_python_versions(appveyor_yml,
                                                 v([
                                                     '2.7',
                                                     '3.7',
                                                     '3.10',
                                                 ]))
    assert result is not None
    assert ''.join(result) == textwrap.dedent(r"""
        environment:
           matrix:
            - PYTHON: "c:\\python27"
            - PYTHON: "c:\\python37"
            - PYTHON: "c:\\python310"
    """.lstrip('\n'))
Example #58
0
def test_config_inventory_fail_script_noexec(netcfgbu_envars, tmpdir):
    """
    Test the case where an [[inventory]] section defines a script, the script
    file exists, but the script file is not executable.
    """
    fake_script = tmpdir.join("dummy-script.sh")
    fake_script.ensure()

    config_data = {"inventory": [{"name": "foo", "script": str(fake_script)}]}

    strio = StringIO()
    strio.name = "fake-file"
    toml.dump(config_data, strio)
    strio.seek(0)

    with pytest.raises(RuntimeError) as excinfo:
        load(fileio=strio)

    exc_errmsgs = excinfo.value.args[0].splitlines()
    found = first(
        [line for line in exc_errmsgs if "inventory.0.script" in line])
    assert found
    assert "is not executable" in found
Example #59
0
    def test_with_thumbor_disabled(self) -> None:
        self.login(self.example_email("hamlet"))
        fp = StringIO("zulip!")
        fp.name = "zulip.jpeg"

        result = self.client_post("/json/user_uploads", {'file': fp})
        self.assert_json_success(result)
        json = ujson.loads(result.content)
        self.assertIn("uri", json)
        uri = json["uri"]
        base = '/user_uploads/'
        self.assertEqual(base, uri[:len(base)])

        quoted_uri = urllib.parse.quote(uri[1:], safe='')

        with self.settings(THUMBOR_URL=''):
            result = self.client_get("/thumbnail?url=%s&size=full" %
                                     (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        self.assertEqual(uri, result.url)

        uri = 'https://www.google.com/images/srpr/logo4w.png'
        quoted_uri = urllib.parse.quote(uri, safe='')
        with self.settings(THUMBOR_URL=''):
            result = self.client_get("/thumbnail?url=%s&size=full" %
                                     (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        self.assertEqual(uri, result.url)

        uri = 'http://www.google.com/images/srpr/logo4w.png'
        quoted_uri = urllib.parse.quote(uri, safe='')
        with self.settings(THUMBOR_URL=''):
            result = self.client_get("/thumbnail?url=%s&size=full" %
                                     (quoted_uri))
        self.assertEqual(result.status_code, 302, result)
        base = 'https://external-content.zulipcdn.net/external_content/7b6552b60c635e41e8f6daeb36d88afc4eabde79/687474703a2f2f7777772e676f6f676c652e636f6d2f696d616765732f737270722f6c6f676f34772e706e67'
        self.assertEqual(base, result.url)
Example #60
0
 def __init__(self, stream):
     with create_session() as session:
         airflow_variables = {
             var.key: var.val
             for var in session.query(Variable)
         }
     self._root = os.path.split(stream.name)[0]
     ctx = {
         "env": os.environ,
         "airflow_variables": airflow_variables,
         "pytz": {name: getattr(pytz, name)
                  for name in pytz.__all__},
         "datetime": {
             name: getattr(datetime, name)
             for name in
             ["date", "datetime", "time", "timedelta", "tzinfo"]
         },
         "re": {name: getattr(re, name)
                for name in re.__all__},
     }
     # Enable Jinja2 in the yaml files
     yaml_stream = StringIO(Template(stream.read()).render(ctx))
     yaml_stream.name = stream.name
     super().__init__(yaml_stream)