Пример #1
0
    def test_create(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        data = {"name": u"Phone", "field_type": "text id"}  # type: Dict[str, Any]
        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_error(result, u'Argument "field_type" is not valid JSON.')

        data["name"] = ""
        data["field_type"] = 100
        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_error(result, u'Name cannot be blank.')

        data["name"] = "Phone"
        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_error(result, u'Invalid field type.')

        data["name"] = "Phone"
        data["hint"] = "*" * 81
        data["field_type"] = CustomProfileField.SHORT_TEXT
        result = self.client_post("/json/realm/profile_fields", info=data)
        msg = "hint is too long (limit: 80 characters)"
        self.assert_json_error(result, msg)

        data["name"] = "Phone"
        data["hint"] = "Contact number"
        data["field_type"] = CustomProfileField.SHORT_TEXT
        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_success(result)

        field = CustomProfileField.objects.get(name="Phone", realm=realm)
        self.assertEqual(field.id, field.order)

        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_error(result,
                               u'A field with that name already exists.')
Пример #2
0
    def test_update(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        result = self.client_patch(
            "/json/realm/profile_fields/100",
            info={'name': '',
                  'field_type': CustomProfileField.SHORT_TEXT}
        )
        self.assert_json_error(result, u'Name cannot be blank.')

        result = self.client_patch(
            "/json/realm/profile_fields/100",
            info={'name': 'Phone Number',
                  'field_type': CustomProfileField.SHORT_TEXT}
        )
        self.assert_json_error(result, u'Field id 100 not found.')

        field = try_add_realm_custom_profile_field(
            realm,
            u"Phone",
            CustomProfileField.SHORT_TEXT
        )

        self.assertEqual(CustomProfileField.objects.count(), 1)
        result = self.client_patch(
            "/json/realm/profile_fields/{}".format(field.id),
            info={'name': 'Phone Number',
                  'field_type': CustomProfileField.SHORT_TEXT})
        self.assert_json_success(result)
        self.assertEqual(CustomProfileField.objects.count(), 1)
        field = CustomProfileField.objects.first()
        self.assertEqual(field.name, 'Phone Number')
        self.assertEqual(field.field_type, CustomProfileField.SHORT_TEXT)
Пример #3
0
    def test_delete_field_value(self) -> None:
        iago = self.example_user("iago")
        self.login(iago.email)
        realm = get_realm("zulip")

        invalid_field_id = 1234
        result = self.client_delete("/json/users/me/profile_data", {
            'data': ujson.dumps([invalid_field_id])
        })
        self.assert_json_error(result,
                               u'Field id %d not found.' % (invalid_field_id))

        field = CustomProfileField.objects.get(name="Mentor", realm=realm)
        data = [{'id': field.id,
                 'value': [self.example_user("aaron").id]}]  # type: List[Dict[str, Union[int, str, List[int]]]]
        do_update_user_custom_profile_data(iago, data)

        iago_value = CustomProfileFieldValue.objects.get(user_profile=iago, field=field)
        converter = field.FIELD_CONVERTERS[field.field_type]
        self.assertEqual([self.example_user("aaron").id], converter(iago_value.value))

        result = self.client_delete("/json/users/me/profile_data", {
            'data': ujson.dumps([field.id])
        })
        self.assert_json_success(result)

        # Don't throw an exception here
        result = self.client_delete("/json/users/me/profile_data", {
            'data': ujson.dumps([field.id])
        })
        self.assert_json_success(result)
Пример #4
0
    def test_null_value_and_rendered_value(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm("zulip")

        quote = try_add_realm_custom_profile_field(
            realm=realm,
            name="Quote",
            hint="Saying or phrase which you known for.",
            field_type=CustomProfileField.SHORT_TEXT
        )

        iago = self.example_user("iago")
        iago_profile_quote = iago.profile_data[-1]
        value = iago_profile_quote["value"]
        rendered_value = iago_profile_quote["rendered_value"]
        self.assertIsNone(value)
        self.assertIsNone(rendered_value)

        update_dict = {
            "id": quote.id,
            "value": "***beware*** of jealousy..."
        }
        do_update_user_custom_profile_data(iago, [update_dict])

        iago_profile_quote = self.example_user("iago").profile_data[-1]
        value = iago_profile_quote["value"]
        rendered_value = iago_profile_quote["rendered_value"]
        self.assertIsNotNone(value)
        self.assertIsNotNone(rendered_value)
        self.assertEqual("<p><strong><em>beware</em></strong> of jealousy...</p>", rendered_value)
Пример #5
0
 def test_delete_no_author(self) -> None:
     email = self.example_email('iago')
     self.login(email)
     realm = get_realm('zulip')
     self.create_test_emoji_with_no_author('my_emoji', realm)
     result = self.client_delete('/json/realm/emoji/my_emoji')
     self.assert_json_success(result)
Пример #6
0
    def test_create(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        data = {"name": u"Phone", "field_type": "text id"}  # type: Dict[str, Any]
        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_error(result, u'Argument "field_type" is not valid JSON.')

        data["name"] = ""
        data["field_type"] = 100
        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_error(result, u'Name cannot be blank.')

        data["name"] = "Phone"
        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_error(result, u'Invalid field type.')

        data["name"] = "Phone"
        data["hint"] = "*" * 81
        data["field_type"] = CustomProfileField.SHORT_TEXT
        result = self.client_post("/json/realm/profile_fields", info=data)
        msg = "hint is too long (limit: 80 characters)"
        self.assert_json_error(result, msg)

        data["name"] = "Phone"
        data["hint"] = "Contact number"
        data["field_type"] = CustomProfileField.SHORT_TEXT
        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_success(result)

        field = CustomProfileField.objects.get(name="Phone", realm=realm)
        self.assertEqual(field.id, field.order)

        result = self.client_post("/json/realm/profile_fields", info=data)
        self.assert_json_error(result,
                               u'A field with that name already exists.')
Пример #7
0
    def test_update(self):
        # type: () -> None
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        result = self.client_patch("/json/realm/profile_fields/100",
                                   info={
                                       'name': '',
                                       'field_type':
                                       CustomProfileField.SHORT_TEXT
                                   })
        self.assert_json_error(result, u'Name cannot be blank.')

        result = self.client_patch("/json/realm/profile_fields/100",
                                   info={
                                       'name': 'Phone Number',
                                       'field_type':
                                       CustomProfileField.SHORT_TEXT
                                   })
        self.assert_json_error(result, u'Field id 100 not found.')

        field = try_add_realm_custom_profile_field(
            realm, u"Phone", CustomProfileField.SHORT_TEXT)

        self.assertEqual(CustomProfileField.objects.count(), 1)
        result = self.client_patch(
            "/json/realm/profile_fields/{}".format(field.id),
            info={
                'name': 'Phone Number',
                'field_type': CustomProfileField.SHORT_TEXT
            })
        self.assert_json_success(result)
        self.assertEqual(CustomProfileField.objects.count(), 1)
        field = CustomProfileField.objects.first()
        self.assertEqual(field.name, 'Phone Number')
        self.assertEqual(field.field_type, CustomProfileField.SHORT_TEXT)
Пример #8
0
    def test_null_value_and_rendered_value(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm("zulip")

        quote = try_add_realm_custom_profile_field(
            realm=realm,
            name="Quote",
            hint="Saying or phrase which you known for.",
            field_type=CustomProfileField.SHORT_TEXT)

        iago = self.example_user("iago")
        iago_profile_quote = iago.profile_data[-1]
        value = iago_profile_quote["value"]
        rendered_value = iago_profile_quote["rendered_value"]
        self.assertIsNone(value)
        self.assertIsNone(rendered_value)

        update_dict = {"id": quote.id, "value": "***beware*** of jealousy..."}
        do_update_user_custom_profile_data(iago, [update_dict])

        iago_profile_quote = self.example_user("iago").profile_data[-1]
        value = iago_profile_quote["value"]
        rendered_value = iago_profile_quote["rendered_value"]
        self.assertIsNotNone(value)
        self.assertIsNotNone(rendered_value)
        self.assertEqual(
            "<p><strong><em>beware</em></strong> of jealousy...</p>",
            rendered_value)
Пример #9
0
    def test_delete_field_value(self) -> None:
        iago = self.example_user("iago")
        self.login(iago.email)
        realm = get_realm("zulip")

        invalid_field_id = 1234
        result = self.client_delete("/json/users/me/profile_data", {
            'data': ujson.dumps([invalid_field_id])
        })
        self.assert_json_error(result,
                               u'Field id %d not found.' % (invalid_field_id))

        field = CustomProfileField.objects.get(name="Mentor", realm=realm)
        data = [{'id': field.id,
                 'value': [self.example_user("aaron").id]}]  # type: List[Dict[str, Union[int, str, List[int]]]]
        do_update_user_custom_profile_data(iago, data)

        iago_value = CustomProfileFieldValue.objects.get(user_profile=iago, field=field)
        converter = field.FIELD_CONVERTERS[field.field_type]
        self.assertEqual([self.example_user("aaron").id], converter(iago_value.value))

        result = self.client_delete("/json/users/me/profile_data", {
            'data': ujson.dumps([field.id])
        })
        self.assert_json_success(result)

        # Don't throw an exception here
        result = self.client_delete("/json/users/me/profile_data", {
            'data': ujson.dumps([field.id])
        })
        self.assert_json_success(result)
Пример #10
0
    def test_update(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        result = self.client_patch("/json/realm/profile_fields/100",
                                   info={
                                       'name': '',
                                       'field_type':
                                       CustomProfileField.SHORT_TEXT
                                   })
        self.assert_json_error(result, u'Name cannot be blank.')

        result = self.client_patch("/json/realm/profile_fields/100",
                                   info={
                                       'name': 'Phone Number',
                                       'field_type':
                                       CustomProfileField.SHORT_TEXT
                                   })
        self.assert_json_error(result, u'Field id 100 not found.')

        field = CustomProfileField.objects.get(name="Phone number",
                                               realm=realm)

        self.assertEqual(CustomProfileField.objects.count(), 3)
        result = self.client_patch(
            "/json/realm/profile_fields/{}".format(field.id),
            info={
                'name': 'New phone number',
                'field_type': CustomProfileField.SHORT_TEXT
            })
        self.assert_json_success(result)

        field = CustomProfileField.objects.get(id=field.id, realm=realm)
        self.assertEqual(CustomProfileField.objects.count(), 3)
        self.assertEqual(field.name, 'New phone number')
        self.assertEqual(field.field_type, CustomProfileField.SHORT_TEXT)
Пример #11
0
    def test_delete_admin_or_author(self) -> None:
        # If any user in a realm can upload the emoji then the user who
        # uploaded it as well as the admin should be able to delete it.
        emoji_author = self.example_user('othello')
        realm = get_realm('zulip')
        realm.add_emoji_by_admins_only = False
        realm.save()

        self.create_test_emoji('my_emoji_1', emoji_author)
        self.login(emoji_author.email)
        result = self.client_delete("/json/realm/emoji/my_emoji_1")
        self.assert_json_success(result)
        self.logout()

        self.create_test_emoji('my_emoji_2', emoji_author)
        self.login(self.example_email('iago'))
        result = self.client_delete("/json/realm/emoji/my_emoji_2")
        self.assert_json_success(result)
        self.logout()

        self.create_test_emoji('my_emoji_3', emoji_author)
        self.login(self.example_email('cordelia'))
        result = self.client_delete("/json/realm/emoji/my_emoji_3")
        self.assert_json_error(
            result, 'Must be an organization administrator or emoji author')
Пример #12
0
    def test_update_profile_data_successfully(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        fields = [
            ('Phone number', '*short* text data'),
            ('Biography', '~~short~~ **long** text data'),
            ('Favorite food', 'long short text data'),
            ('Favorite editor', 'vim'),
            ('Birthday', '1909-3-5'),
            ('GitHub profile', 'https://github.com/ABC'),
            ('Mentor', [self.example_user("cordelia").id]),
        ]

        data = []
        for i, field_value in enumerate(fields):
            name, value = field_value
            field = CustomProfileField.objects.get(name=name, realm=realm)
            data.append({
                'id': field.id,
                'value': value,
                'field': field,
            })

        # Update value of field
        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_success(result)

        iago = self.example_user('iago')
        expected_value = {f['id']: f['value'] for f in data}
        expected_rendered_value = {
        }  # type: Dict[Union[int, float, str, None], Union[str, None]]
        for f in data:
            if f['field'].is_renderable():
                expected_rendered_value[f['id']] = bugdown_convert(f['value'])
            else:
                expected_rendered_value[f['id']] = None

        for field_dict in iago.profile_data:
            self.assertEqual(field_dict['value'],
                             expected_value[field_dict['id']])
            self.assertEqual(field_dict['rendered_value'],
                             expected_rendered_value[field_dict['id']])
            for k in ['id', 'type', 'name', 'field_data']:
                self.assertIn(k, field_dict)

        # Update value of one field.
        field = CustomProfileField.objects.get(name='Biography', realm=realm)
        data = [{
            'id': field.id,
            'value': 'foobar',
        }]

        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_success(result)
        for f in iago.profile_data:
            if f['id'] == field.id:
                self.assertEqual(f['value'], 'foobar')
Пример #13
0
 def test_realm_patterns_negative(self):
     realm = get_realm('zulip.com')
     RealmFilter(realm=realm, pattern=r"#(?P<id>[0-9]{2,8})",
                 url_format_string=r"https://trac.zulip.net/ticket/%(id)s").save()
     boring_msg = Message(sender=get_user_profile_by_email("*****@*****.**"),
                          subject=u"no match here")
     converted_boring_subject = bugdown.subject_links(realm.domain.lower(), boring_msg.subject)
     self.assertEqual(converted_boring_subject,  [])
Пример #14
0
 def test_reorder_unauthorized(self) -> None:
     self.login(self.example_email("hamlet"))
     realm = get_realm('zulip')
     order = (CustomProfileField.objects.filter(
         realm=realm).order_by('-order').values_list('order', flat=True))
     result = self.client_patch("/json/realm/profile_fields",
                                info={'order': ujson.dumps(order)})
     self.assert_json_error(result, "Must be an organization administrator")
Пример #15
0
 def test_delete_admins_only(self) -> None:
     email = self.example_email('othello')
     self.login(email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = True
     realm.save()
     check_add_realm_emoji(realm, "my_emoji", "my_emoji.png")
     result = self.client_delete("/json/realm/emoji/my_emoji")
     self.assert_json_error(result, 'Must be a realm administrator')
Пример #16
0
 def test_list(self) -> None:
     email = self.example_email('iago')
     self.login(email)
     realm = get_realm('zulip')
     check_add_realm_emoji(realm, "my_emoji", "my_emoji")
     result = self.client_get("/json/realm/emoji")
     self.assert_json_success(result)
     self.assertEqual(200, result.status_code)
     self.assertEqual(len(result.json()["emoji"]), 2)
Пример #17
0
 def test_list(self) -> None:
     email = self.example_email('iago')
     self.login(email)
     realm = get_realm('zulip')
     check_add_realm_emoji(realm, "my_emoji", "my_emoji")
     result = self.client_get("/json/realm/emoji")
     self.assert_json_success(result)
     self.assertEqual(200, result.status_code)
     self.assertEqual(len(result.json()["emoji"]), 2)
Пример #18
0
    def test_realm_patterns(self):
        RealmFilter(realm=get_realm('zulip.com'), pattern=r"#(?P<id>[0-9]{2,8})",
                    url_format_string=r"https://trac.zulip.net/ticket/%(id)s").save()
        msg = Message(sender=get_user_profile_by_email("*****@*****.**"))

        content = "We should fix #224 and #115, but not issue#124 or #1124z or [trac #15](https://trac.zulip.net/ticket/16) today."
        converted = bugdown.convert(content, realm_domain='zulip.com', message=msg)

        self.assertEqual(converted, '<p>We should fix <a href="https://trac.zulip.net/ticket/224" target="_blank" title="https://trac.zulip.net/ticket/224">#224</a> and <a href="https://trac.zulip.net/ticket/115" target="_blank" title="https://trac.zulip.net/ticket/115">#115</a>, but not issue#124 or #1124z or <a href="https://trac.zulip.net/ticket/16" target="_blank" title="https://trac.zulip.net/ticket/16">trac #15</a> today.</p>')
Пример #19
0
 def test_delete_admins_only(self) -> None:
     email = self.example_email('othello')
     self.login(email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = True
     realm.save()
     check_add_realm_emoji(realm, "my_emoji", "my_emoji.png")
     result = self.client_delete("/json/realm/emoji/my_emoji")
     self.assert_json_error(result, 'Must be an organization administrator')
Пример #20
0
 def test_delete_admins_only(self) -> None:
     emoji_author = self.example_user('othello')
     self.login(emoji_author.email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = True
     realm.save()
     self.create_test_emoji_with_no_author("my_emoji", realm)
     result = self.client_delete("/json/realm/emoji/my_emoji")
     self.assert_json_error(result, 'Must be an organization administrator')
Пример #21
0
    def assert_error_update_invalid_value(self, field_name: str, new_value: object, error_msg: str) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = CustomProfileField.objects.get(name=field_name, realm=realm)

        # Update value of field
        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps([{"id": field.id, "value": new_value}])})
        self.assert_json_error(result, error_msg)
Пример #22
0
    def assert_error_update_invalid_value(self, field_name: str, new_value: object, error_msg: str) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = CustomProfileField.objects.get(name=field_name, realm=realm)

        # Update value of field
        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps([{"id": field.id, "value": new_value}])})
        self.assert_json_error(result, error_msg)
Пример #23
0
    def test_update_profile_data_successfully(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        fields = [
            ('Phone number', '*short* text data'),
            ('Biography', '~~short~~ **long** text data'),
            ('Favorite food', 'long short text data'),
            ('Favorite editor', 'vim'),
            ('Birthday', '1909-3-5'),
            ('GitHub profile', 'https://github.com/ABC'),
            ('Mentor', [self.example_user("cordelia").id]),
        ]

        data = []
        for i, field_value in enumerate(fields):
            name, value = field_value
            field = CustomProfileField.objects.get(name=name, realm=realm)
            data.append({
                'id': field.id,
                'value': value,
                'field': field,
            })

        # Update value of field
        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_success(result)

        iago = self.example_user('iago')
        expected_value = {f['id']: f['value'] for f in data}
        expected_rendered_value = {}  # type: Dict[Union[int, float, str, None], Union[str, None]]
        for f in data:
            if f['field'].is_renderable():
                expected_rendered_value[f['id']] = bugdown_convert(f['value'])
            else:
                expected_rendered_value[f['id']] = None

        for field_dict in iago.profile_data:
            self.assertEqual(field_dict['value'], expected_value[field_dict['id']])
            self.assertEqual(field_dict['rendered_value'], expected_rendered_value[field_dict['id']])
            for k in ['id', 'type', 'name', 'field_data']:
                self.assertIn(k, field_dict)

        # Update value of one field.
        field = CustomProfileField.objects.get(name='Biography', realm=realm)
        data = [{
            'id': field.id,
            'value': 'foobar',
        }]

        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_success(result)
        for f in iago.profile_data:
            if f['id'] == field.id:
                self.assertEqual(f['value'], 'foobar')
Пример #24
0
 def test_list(self) -> None:
     self.login(self.example_email("iago"))
     realm = get_realm('zulip')
     try_add_realm_custom_profile_field(realm, u"Phone",
                                        CustomProfileField.SHORT_TEXT)
     result = self.client_get("/json/realm/profile_fields")
     self.assert_json_success(result)
     self.assertEqual(200, result.status_code)
     content = result.json()
     self.assertEqual(len(content["custom_fields"]), 1)
Пример #25
0
 def test_list_no_author(self) -> None:
     email = self.example_email('iago')
     self.login(email)
     realm = get_realm('zulip')
     RealmEmoji.objects.create(realm=realm, name='my_emoji', file_name='my_emoji')
     result = self.client_get("/json/realm/emoji")
     self.assert_json_success(result)
     content = result.json()
     self.assertEqual(len(content["emoji"]), 2)
     self.assertIsNone(content["emoji"]['my_emoji']['author'])
Пример #26
0
 def test_upload_anyone(self) -> None:
     email = self.example_email('othello')
     self.login(email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = False
     realm.save()
     with get_test_image_file('img.png') as fp1:
         emoji_data = {'f1': fp1}
         result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
     self.assert_json_success(result)
Пример #27
0
 def test_list(self):
     # type: () -> None
     self.login("*****@*****.**")
     realm = get_realm('zulip.com')
     check_add_realm_emoji(realm, "my_emoji", "https://example.com/my_emoji")
     result = self.client_get("/json/realm/emoji")
     self.assert_json_success(result)
     self.assertEqual(200, result.status_code)
     content = ujson.loads(result.content)
     self.assertEqual(len(content["emoji"]), 1)
Пример #28
0
 def test_list(self) -> None:
     self.login(self.example_email("iago"))
     realm = get_realm('zulip')
     try_add_realm_custom_profile_field(realm, u"Phone",
                                        CustomProfileField.SHORT_TEXT)
     result = self.client_get("/json/realm/profile_fields")
     self.assert_json_success(result)
     self.assertEqual(200, result.status_code)
     content = result.json()
     self.assertEqual(len(content["custom_fields"]), 1)
Пример #29
0
 def test_list_no_author(self) -> None:
     email = self.example_email('iago')
     self.login(email)
     realm = get_realm('zulip')
     RealmEmoji.objects.create(realm=realm, name='my_emoji', file_name='my_emoji')
     result = self.client_get("/json/realm/emoji")
     self.assert_json_success(result)
     content = result.json()
     self.assertEqual(len(content["emoji"]), 2)
     self.assertIsNone(content["emoji"]['my_emoji']['author'])
Пример #30
0
 def test_upload_admins_only(self) -> None:
     email = self.example_email('othello')
     self.login(email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = True
     realm.save()
     with get_test_image_file('img.png') as fp1:
         emoji_data = {'f1': fp1}
         result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
     self.assert_json_error(result, 'Must be an organization administrator')
 def test_list(self) -> None:
     email = self.example_email('iago')
     self.login(email)
     realm = get_realm('zulip')
     do_add_realm_filter(realm, "#(?P<id>[123])",
                         "https://realm.com/my_realm_filter/%(id)s")
     result = self.client_get("/json/realm/filters")
     self.assert_json_success(result)
     self.assertEqual(200, result.status_code)
     self.assertEqual(len(result.json()["filters"]), 1)
Пример #32
0
 def test_upload_admins_only(self) -> None:
     email = self.example_email('othello')
     self.login(email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = True
     realm.save()
     with get_test_image_file('img.png') as fp1:
         emoji_data = {'f1': fp1}
         result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
     self.assert_json_error(result, 'Must be an organization administrator')
Пример #33
0
 def test_upload_anyone(self) -> None:
     email = self.example_email('othello')
     self.login(email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = False
     realm.save()
     with get_test_image_file('img.png') as fp1:
         emoji_data = {'f1': fp1}
         result = self.client_post('/json/realm/emoji/my_emoji', info=emoji_data)
     self.assert_json_success(result)
Пример #34
0
    def test_update_invalid_double(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = try_add_realm_custom_profile_field(realm, u"distance",
                                                   CustomProfileField.FLOAT)

        data = [{'id': field.id, 'value': 'text'}]
        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_error(result,
                               u"value[{}] is not a float".format(field.id))
Пример #35
0
 def test_list_order(self) -> None:
     self.login(self.example_email("iago"))
     realm = get_realm('zulip')
     order = (CustomProfileField.objects.filter(
         realm=realm).order_by('-order').values_list('order', flat=True))
     try_reorder_realm_custom_profile_fields(realm, order)
     result = self.client_get("/json/realm/profile_fields")
     content = result.json()
     self.assertListEqual(
         content["custom_fields"],
         sorted(content["custom_fields"], key=lambda x: -x["id"]))
Пример #36
0
    def test_update_invalid_short_text(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = try_add_realm_custom_profile_field(
            realm, u"description", CustomProfileField.SHORT_TEXT)

        data = [{'id': field.id, 'value': 't' * 201}]
        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_error(
            result, u"value[{}] is longer than 50.".format(field.id))
Пример #37
0
 def test_reorder_unauthorized(self) -> None:
     self.login(self.example_email("hamlet"))
     realm = get_realm('zulip')
     order = (
         CustomProfileField.objects.filter(realm=realm)
         .order_by('-order')
         .values_list('order', flat=True)
     )
     result = self.client_patch("/json/realm/profile_fields",
                                info={'order': ujson.dumps(order)})
     self.assert_json_error(result, "Must be an organization administrator")
Пример #38
0
 def test_list_admins_only(self) -> None:
     email = self.example_email('othello')
     self.login(email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = True
     realm.save()
     check_add_realm_emoji(realm, 'my_emoji', 'my_emoji')
     result = self.client_get("/json/realm/emoji")
     self.assert_json_success(result)
     content = result.json()
     self.assertEqual(len(content["emoji"]), 2)
     self.assertIsNone(content["emoji"]['my_emoji']['author'])
Пример #39
0
    def test_delete(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = CustomProfileField.objects.get(name="Phone number", realm=realm)
        result = self.client_delete("/json/realm/profile_fields/100")
        self.assert_json_error(result, 'Field id 100 not found.')

        self.assertTrue(self.custom_field_exists_in_realm(field.id))
        result = self.client_delete(
            "/json/realm/profile_fields/{}".format(field.id))
        self.assert_json_success(result)
        self.assertFalse(self.custom_field_exists_in_realm(field.id))
Пример #40
0
    def test_update_choice_field_successfully(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = CustomProfileField.objects.get(name='Favorite editor', realm=realm)
        data = [{
            'id': field.id,
            'value': 'emacs',
        }]

        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_success(result)
Пример #41
0
    def test_list_no_author(self) -> None:
        email = self.example_email('iago')
        self.login(email)
        realm = get_realm('zulip')
        realm_emoji = self.create_test_emoji_with_no_author('my_emoji', realm)

        result = self.client_get("/json/realm/emoji")
        self.assert_json_success(result)
        content = result.json()
        self.assertEqual(len(content["emoji"]), 2)
        test_emoji = content["emoji"][str(realm_emoji.id)]
        self.assertIsNone(test_emoji['author'])
Пример #42
0
 def test_reorder(self) -> None:
     self.login(self.example_email("iago"))
     realm = get_realm('zulip')
     order = (CustomProfileField.objects.filter(
         realm=realm).order_by('-order').values_list('order', flat=True))
     result = self.client_patch("/json/realm/profile_fields",
                                info={'order': ujson.dumps(order)})
     self.assert_json_success(result)
     fields = CustomProfileField.objects.filter(
         realm=realm).order_by('order')
     for field in fields:
         self.assertEqual(field.id, order[field.order])
Пример #43
0
    def test_update_choice_field_successfully(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = CustomProfileField.objects.get(name='Favorite editor', realm=realm)
        data = [{
            'id': field.id,
            'value': 'emacs',
        }]

        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_success(result)
Пример #44
0
 def test_list_admins_only(self) -> None:
     email = self.example_email('othello')
     self.login(email)
     realm = get_realm('zulip')
     realm.add_emoji_by_admins_only = True
     realm.save()
     check_add_realm_emoji(realm, 'my_emoji', 'my_emoji')
     result = self.client_get("/json/realm/emoji")
     self.assert_json_success(result)
     content = result.json()
     self.assertEqual(len(content["emoji"]), 2)
     self.assertIsNone(content["emoji"]['my_emoji']['author'])
Пример #45
0
 def test_list(self) -> None:
     email = self.example_email('iago')
     self.login(email)
     realm = get_realm('zulip')
     do_add_realm_filter(
         realm,
         "#(?P<id>[123])",
         "https://realm.com/my_realm_filter/%(id)s")
     result = self.client_get("/json/realm/filters")
     self.assert_json_success(result)
     self.assertEqual(200, result.status_code)
     self.assertEqual(len(result.json()["filters"]), 1)
Пример #46
0
    def test_update_invalid_value(self):
        # type: () -> None
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        age_field = try_add_realm_custom_profile_field(
            realm, u"age", CustomProfileField.INTEGER)

        data = [{'id': age_field.id, 'value': 'text'}]
        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_error(
            result, u"value[{}] is not an integer".format(age_field.id))
Пример #47
0
    def test_delete(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = CustomProfileField.objects.get(name="Phone number", realm=realm)
        result = self.client_delete("/json/realm/profile_fields/100")
        self.assert_json_error(result, 'Field id 100 not found.')

        self.assertTrue(self.custom_field_exists_in_realm(field.id))
        result = self.client_delete(
            "/json/realm/profile_fields/{}".format(field.id))
        self.assert_json_success(result)
        self.assertFalse(self.custom_field_exists_in_realm(field.id))
Пример #48
0
 def test_list_order(self) -> None:
     self.login(self.example_email("iago"))
     realm = get_realm('zulip')
     order = (
         CustomProfileField.objects.filter(realm=realm)
         .order_by('-order')
         .values_list('order', flat=True)
     )
     try_reorder_realm_custom_profile_fields(realm, order)
     result = self.client_get("/json/realm/profile_fields")
     content = result.json()
     self.assertListEqual(content["custom_fields"],
                          sorted(content["custom_fields"], key=lambda x: -x["id"]))
Пример #49
0
    def test_delete(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = try_add_realm_custom_profile_field(
            realm, "Phone", CustomProfileField.SHORT_TEXT)
        result = self.client_delete("/json/realm/profile_fields/100")
        self.assert_json_error(result, 'Field id 100 not found.')

        self.assertEqual(CustomProfileField.objects.count(), 1)
        result = self.client_delete("/json/realm/profile_fields/{}".format(
            field.id))
        self.assert_json_success(result)
        self.assertEqual(CustomProfileField.objects.count(), 0)
    def test_update_profile_data_successfully(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        fields = [
            ('Phone number', 'short text data'),
            ('Biography', 'long text data'),
            ('Favorite food', 'short text data'),
            ('Favorite editor', 'vim'),
            ('Birthday', '1909-3-5'),
            ('GitHub profile', 'https://github.com/ABC'),
        ]

        data = []
        for i, field_value in enumerate(fields):
            name, value = field_value
            field = CustomProfileField.objects.get(name=name, realm=realm)
            data.append({
                'id': field.id,
                'value': value,
            })

        # Update value of field
        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_success(result)

        iago = self.example_user('iago')
        expected_value = {f['id']: f['value'] for f in data}

        for field_dict in iago.profile_data:
            self.assertEqual(field_dict['value'],
                             expected_value[field_dict['id']])
            for k in ['id', 'type', 'name', 'field_data']:
                self.assertIn(k, field_dict)

            self.assertEqual(len(iago.profile_data), self.original_count)

        # Update value of one field.
        field = CustomProfileField.objects.get(name='Biography', realm=realm)
        data = [{
            'id': field.id,
            'value': 'foobar',
        }]

        result = self.client_patch("/json/users/me/profile_data",
                                   {'data': ujson.dumps(data)})
        self.assert_json_success(result)
        for f in iago.profile_data:
            if f['id'] == field.id:
                self.assertEqual(f['value'], 'foobar')
Пример #51
0
 def test_reorder(self) -> None:
     self.login(self.example_email("iago"))
     realm = get_realm('zulip')
     order = (
         CustomProfileField.objects.filter(realm=realm)
         .order_by('-order')
         .values_list('order', flat=True)
     )
     result = self.client_patch("/json/realm/profile_fields",
                                info={'order': ujson.dumps(order)})
     self.assert_json_success(result)
     fields = CustomProfileField.objects.filter(realm=realm).order_by('order')
     for field in fields:
         self.assertEqual(field.id, order[field.order])
Пример #52
0
    def test_delete(self) -> None:
        email = self.example_email('iago')
        self.login(email)
        realm = get_realm('zulip')
        filter_id = do_add_realm_filter(
            realm,
            "#(?P<id>[123])",
            "https://realm.com/my_realm_filter/%(id)s")
        filters_count = RealmFilter.objects.count()
        result = self.client_delete("/json/realm/filters/{0}".format(filter_id + 1))
        self.assert_json_error(result, 'Filter not found')

        result = self.client_delete("/json/realm/filters/{0}".format(filter_id))
        self.assert_json_success(result)
        self.assertEqual(RealmFilter.objects.count(), filters_count - 1)
Пример #53
0
    def test_delete(self) -> None:
        email = self.example_email('iago')
        self.login(email)
        realm = get_realm('zulip')
        check_add_realm_emoji(realm, "my_emoji", "my_emoji.png")
        result = self.client_delete("/json/realm/emoji/my_emoji")
        self.assert_json_success(result)

        result = self.client_get("/json/realm/emoji")
        emojis = result.json()["emoji"]
        self.assert_json_success(result)
        # We only mark an emoji as deactivated instead of
        # removing it from the database.
        self.assertEqual(len(emojis), 2)
        self.assertEqual(emojis["my_emoji"]["deactivated"], True)
Пример #54
0
    def test_delete(self) -> None:
        email = self.example_email('iago')
        self.login(email)
        realm = get_realm('zulip')
        filter_id = do_add_realm_filter(
            realm,
            "#(?P<id>[123])",
            "https://realm.com/my_realm_filter/%(id)s")
        filters_count = RealmFilter.objects.count()
        result = self.client_delete("/json/realm/filters/{0}".format(filter_id + 1))
        self.assert_json_error(result, 'Filter not found')

        result = self.client_delete("/json/realm/filters/{0}".format(filter_id))
        self.assert_json_success(result)
        self.assertEqual(RealmFilter.objects.count(), filters_count - 1)
Пример #55
0
    def test_list_admins_only(self) -> None:
        # Test that realm emoji list is public and realm emojis
        # having no author are also there in the list.
        email = self.example_email('othello')
        self.login(email)
        realm = get_realm('zulip')
        realm.add_emoji_by_admins_only = True
        realm.save()
        realm_emoji = self.create_test_emoji_with_no_author('my_emoji', realm)

        result = self.client_get("/json/realm/emoji")
        self.assert_json_success(result)
        content = result.json()
        self.assertEqual(len(content["emoji"]), 2)
        test_emoji = content["emoji"][str(realm_emoji.id)]
        self.assertIsNone(test_emoji['author'])
Пример #56
0
    def test_update_is_aware_of_uniqueness(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field_1 = try_add_realm_custom_profile_field(realm, u"Phone",
                                                     CustomProfileField.SHORT_TEXT)

        field_2 = try_add_realm_custom_profile_field(realm, u"Phone 1",
                                                     CustomProfileField.SHORT_TEXT)

        self.assertTrue(self.custom_field_exists_in_realm(field_1.id))
        self.assertTrue(self.custom_field_exists_in_realm(field_2.id))
        result = self.client_patch(
            "/json/realm/profile_fields/{}".format(field_2.id),
            info={'name': 'Phone', 'field_type': CustomProfileField.SHORT_TEXT})
        self.assert_json_error(
            result, u'A field with that name already exists.')
Пример #57
0
    def test_delete(self) -> None:
        self.login(self.example_email("iago"))
        realm = get_realm('zulip')
        field = try_add_realm_custom_profile_field(
            realm,
            "Phone",
            CustomProfileField.SHORT_TEXT
        )
        result = self.client_delete("/json/realm/profile_fields/100")
        self.assert_json_error(result, 'Field id 100 not found.')

        self.assertEqual(CustomProfileField.objects.count(), 1)
        result = self.client_delete(
            "/json/realm/profile_fields/{}".format(field.id))
        self.assert_json_success(result)
        self.assertEqual(CustomProfileField.objects.count(), 0)