示例#1
0
    def test_is_feature_enabled(self):
        request = RequestFactory().get('/')
        request.user = AnonymousUser()

        # Return True if no switch or flag are provided
        self.assertTrue(utils.is_feature_enabled({'switch': None, 'flag': None}, request))

        name = 'test-waffle'
        item = {'switch': 'test-waffle'}
        # Return False if switch inactive
        with override_switch(name, active=False):
            self.assertFalse(utils.is_feature_enabled(item, request))

        # Return True if switch active
        with override_switch(name, active=True):
            self.assertTrue(utils.is_feature_enabled(item, request))

        item = {'flag': 'test-waffle'}
        # Return False if flag inactive
        with override_flag(name, active=False):
            self.assertFalse(utils.is_feature_enabled(item, request))

        # Return True if flag active
        with override_flag(name, active=True):
            self.assertTrue(utils.is_feature_enabled(item, request))
    def test_is_feature_enabled(self):
        request = RequestFactory().get('/')
        request.user = AnonymousUser()

        # Return True if no switch or flag are provided
        self.assertTrue(
            utils.is_feature_enabled({
                'switch': None,
                'flag': None
            }, request))

        name = 'test-waffle'
        item = {'switch': 'test-waffle'}
        # Return False if switch inactive
        with override_switch(name, active=False):
            self.assertFalse(utils.is_feature_enabled(item, request))

        # Return True if switch active
        with override_switch(name, active=True):
            self.assertTrue(utils.is_feature_enabled(item, request))

        item = {'flag': 'test-waffle'}
        # Return False if flag inactive
        with override_flag(name, active=False):
            self.assertFalse(utils.is_feature_enabled(item, request))

        # Return True if flag active
        with override_flag(name, active=True):
            self.assertTrue(utils.is_feature_enabled(item, request))
示例#3
0
    def test_flag_existed_and_was_null(self):
        Flag.objects.create(name="foo", everyone=None)

        with override_flag("foo", active=True):
            assert waffle.flag_is_active(req(), "foo")

        with override_flag("foo", active=False):
            assert not waffle.flag_is_active(req(), "foo")

        assert Flag.objects.get(name="foo").everyone is None
示例#4
0
    def test_flag_existed_and_was_null(self):
        waffle.get_waffle_flag_model().objects.create(name='foo', everyone=None)

        with override_flag('foo', active=True):
            assert waffle.flag_is_active(req(), 'foo')

        with override_flag('foo', active=False):
            assert not waffle.flag_is_active(req(), 'foo')

        assert waffle.get_waffle_flag_model().objects.get(name='foo').everyone is None
    def test_flag_did_not_exist(self):
        assert not Flag.objects.filter(name='foo').exists()

        with override_flag('foo', active=True):
            assert waffle.flag_is_active(req(), 'foo')

        with override_flag('foo', active=False):
            assert not waffle.flag_is_active(req(), 'foo')

        assert not Flag.objects.filter(name='foo').exists()
    def test_flag_existed_and_was_active(self):
        Flag.objects.create(name='foo', everyone=True)

        with override_flag('foo', active=True):
            assert waffle.flag_is_active(req(), 'foo')

        with override_flag('foo', active=False):
            assert not waffle.flag_is_active(req(), 'foo')

        assert Flag.objects.get(name='foo').everyone
示例#7
0
    def test_flag_existed_and_was_null(self):
        waffle.get_waffle_flag_model().objects.create(name='foo', everyone=None)

        with override_flag('foo', active=True):
            assert waffle.flag_is_active(req(), 'foo')

        with override_flag('foo', active=False):
            assert not waffle.flag_is_active(req(), 'foo')

        assert waffle.get_waffle_flag_model().objects.get(name='foo').everyone is None
示例#8
0
    def test_cache_is_flushed_by_testutils_even_in_transaction(self):
        waffle.get_waffle_flag_model().objects.create(name='foo', everyone=True)

        with transaction.atomic():
            with override_flag('foo', active=True):
                assert waffle.flag_is_active(req(), 'foo')

            with override_flag('foo', active=False):
                assert not waffle.flag_is_active(req(), 'foo')

        assert waffle.flag_is_active(req(), 'foo')
示例#9
0
    def test_cache_is_flushed_by_testutils_even_in_transaction(self):
        waffle.get_waffle_flag_model().objects.create(name='foo', everyone=True)

        with transaction.atomic():
            with override_flag('foo', active=True):
                assert waffle.flag_is_active(req(), 'foo')

            with override_flag('foo', active=False):
                assert not waffle.flag_is_active(req(), 'foo')

        assert waffle.flag_is_active(req(), 'foo')
示例#10
0
    def test_course_waffle_flag(self, data, mock_set_custom_metric):
        """
        Tests various combinations of a flag being set in waffle and overridden
        for a course.
        """
        with patch.object(WaffleFlagCourseOverrideModel, 'override_value', return_value=data['course_override']):
            with override_flag(self.NAMESPACED_FLAG_NAME, active=data['waffle_enabled']):
                # check twice to test that the result is properly cached
                self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_KEY), data['result'])
                self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_KEY), data['result'])
                # result is cached, so override check should happen once
                WaffleFlagCourseOverrideModel.override_value.assert_called_once_with(
                    self.NAMESPACED_FLAG_NAME,
                    self.TEST_COURSE_KEY
                )

        self._assert_waffle_flag_metric(mock_set_custom_metric, expected_flag_value=str(data['result']))
        mock_set_custom_metric.reset_mock()

        # check flag for a second course
        if data['course_override'] == WaffleFlagCourseOverrideModel.ALL_CHOICES.unset:
            # When course override wasn't set for the first course, the second course will get the same
            # cached value from waffle.
            second_value = data['waffle_enabled']
            self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_2_KEY), second_value)
        else:
            # When course override was set for the first course, it should not apply to the second
            # course which should get the default value of False.
            second_value = False
            self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_2_KEY), second_value)

        expected_flag_value = None if second_value == data['result'] else 'Both'
        self._assert_waffle_flag_metric(mock_set_custom_metric, expected_flag_value=expected_flag_value)
示例#11
0
def test_translate_template(client):
    url = reverse('pontoon.translate.next')

    with override_flag('translate_next', active=True):
        response = client.get(url)
        assert response.status_code == 200
        assert 'Translate.Next' in response.content
示例#12
0
    def test_delete_errors(self, app, member, manager, user, osf_group, url):
        with override_flag(OSF_GROUPS, active=True):
            # test invalid user
            invalid_payload = create_bulk_delete_payload(osf_group._id, '12345')
            res = app.delete_json_api(url, {'data': [invalid_payload]}, auth=manager.auth, expect_errors=True, bulk=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'Could not find all objects to delete.'

            # test user does not belong to group
            invalid_payload = create_bulk_delete_payload(osf_group._id, user._id)
            res = app.delete_json_api(url, {'data': [invalid_payload]}, auth=manager.auth, expect_errors=True, bulk=True)
            assert res.status_code == 404
            assert res.json['errors'][0]['detail'] == '{} cannot be found in this OSFGroup'.format(user._id)

            # test user is last manager
            invalid_payload = create_bulk_delete_payload(osf_group._id, manager._id)
            res = app.delete_json_api(url, {'data': [invalid_payload]}, auth=manager.auth, expect_errors=True, bulk=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'Group must have at least one manager.'

            # test user is last registered manager
            osf_group.add_unregistered_member('Crazy 8s', '*****@*****.**', Auth(manager), MANAGER)
            assert len(osf_group.managers) == 2
            res = app.delete_json_api(url, {'data': [invalid_payload]}, auth=manager.auth, expect_errors=True, bulk=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'Group must have at least one manager.'
示例#13
0
    def test_matching_org_override_waffle_flag(self, waffle_enabled,
                                               org_override_choice,
                                               is_enabled):
        """
        Tests various combinations of a flag being set in waffle and overridden for an org
        which is the org which authored/owns the course.
        Since the org-level override has the same org as the course being checked, the org-level
        override's on/off/unset state determines whether is CourseWaffleFlag is active or not.

        on    = active (enabled)
        off   = inactive (disabled)
        unset = mirror the base waffle flag's activity
        """
        WaffleFlagOrgOverrideModel.objects.create(
            waffle_flag=self.NAMESPACED_FLAG_NAME,
            org=self.TEST_ORG,
            override_choice=org_override_choice,
            note='',
            enabled=True)
        # Both course keys should match the org-level override.
        with override_flag(self.NAMESPACED_FLAG_NAME, active=waffle_enabled):
            assert self.TEST_COURSE_FLAG.is_enabled(
                self.TEST_COURSE_KEY) == is_enabled
            assert self.TEST_COURSE_FLAG.is_enabled(
                self.TEST_COURSE_2_KEY) == is_enabled
示例#14
0
    def test_bulk_create_group_member_perms(self, app, url, manager, member, user, user3, osf_group):
        with override_flag(OSF_GROUPS, active=True):
            payload_user_three = make_bulk_create_payload(MANAGER, user3)
            payload_user = make_bulk_create_payload(MEMBER, user)
            bulk_payload = [payload_user_three, payload_user]

            # unauthenticated
            res = app.post_json_api(url, {'data': bulk_payload}, expect_errors=True, bulk=True)
            assert res.status_code == 401

            # non member
            res = app.post_json_api(url, {'data': bulk_payload}, auth=user.auth, expect_errors=True, bulk=True)
            assert res.status_code == 403

            # member
            res = app.post_json_api(url, {'data': bulk_payload}, auth=member.auth, expect_errors=True, bulk=True)
            assert res.status_code == 403

            # manager
            res = app.post_json_api(url, {'data': bulk_payload}, auth=manager.auth, bulk=True)
            assert res.status_code == 201
            assert len(res.json['data']) == 2

            assert osf_group.is_member(user) is True
            assert osf_group.is_member(user3) is True
            assert osf_group.is_manager(user) is False
            assert osf_group.is_manager(user3) is True
示例#15
0
    def test_update_role(self, app, member, manager, user, osf_group, url):
        with override_flag(OSF_GROUPS, active=True):
            payload = build_bulk_update_payload(osf_group._id, member._id, MANAGER)
            bulk_payload = {'data': [payload]}

            # test unauthenticated
            res = app.patch_json_api(url, bulk_payload, expect_errors=True, bulk=True)
            assert res.status_code == 401

            # test user
            res = app.patch_json_api(url, bulk_payload, auth=user.auth, expect_errors=True, bulk=True)
            assert res.status_code == 403

            # test member
            res = app.patch_json_api(url, bulk_payload, auth=member.auth, expect_errors=True, bulk=True)
            assert res.status_code == 403

            # test manager
            res = app.patch_json_api(url, bulk_payload, auth=manager.auth, expect_errors=True, bulk=True)
            assert res.status_code == 200
            assert res.json['data'][0]['attributes']['role'] == MANAGER
            assert res.json['data'][0]['attributes']['full_name'] == member.fullname
            assert res.json['data'][0]['id'] == '{}-{}'.format(osf_group._id, member._id)

            payload = build_bulk_update_payload(osf_group._id, member._id, MEMBER)
            bulk_payload = {'data': [payload]}
            res = app.patch_json_api(url, bulk_payload, auth=manager.auth, expect_errors=True, bulk=True)
            assert res.status_code == 200
            assert res.json['data'][0]['attributes']['role'] == MEMBER
            assert res.json['data'][0]['attributes']['full_name'] == member.fullname
            assert res.json['data'][0]['id'] == '{}-{}'.format(osf_group._id, member._id)
示例#16
0
    def test_course_waffle_flag(self, data):
        """
        Tests various combinations of a flag being set in waffle and overridden
        for a course.
        """
        with patch.object(WaffleFlagCourseOverrideModel, 'override_value', return_value=data['course_override']):
            with override_flag(self.NAMESPACED_FLAG_NAME, active=data['waffle_enabled']):
                # check twice to test that the result is properly cached
                assert self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_KEY) == data['result']
                assert self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_KEY) == data['result']
                # result is cached, so override check should happen once
                # pylint: disable=no-member
                WaffleFlagCourseOverrideModel.override_value.assert_called_once_with(
                    self.NAMESPACED_FLAG_NAME,
                    self.TEST_COURSE_KEY
                )

        # check flag for a second course
        if data['course_override'] == WaffleFlagCourseOverrideModel.ALL_CHOICES.unset:
            # When course override wasn't set for the first course, the second course will get the same
            # cached value from waffle.
            second_value = data['waffle_enabled']
            assert self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_2_KEY) == second_value
        else:
            # When course override was set for the first course, it should not apply to the second
            # course which should get the default value of False.
            second_value = False
            assert self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_2_KEY) == second_value
    def test_node_group_detail(self, app, public_detail_url, osf_group,
                               public_project):
        with override_flag(OSF_GROUPS, active=True):
            # res for group not attached to node raised permissions error
            res = app.get(public_detail_url, expect_errors=True)
            assert res.status_code == 404
            assert res.json['errors'][0][
                'detail'] == 'Group {} does not have permissions to node {}.'.format(
                    osf_group._id, public_project._id)

            public_project.add_osf_group(osf_group, permissions.WRITE)

            # test attributes
            res = app.get(public_detail_url)
            attributes = res.json['data']['attributes']
            assert attributes['date_created'] == osf_group.created.replace(
                tzinfo=None).isoformat()
            assert attributes['date_modified'] == osf_group.modified.replace(
                tzinfo=None).isoformat()
            assert attributes['name'] == osf_group.name
            assert attributes['permission'] == permissions.WRITE

            # test relationships
            relationships = res.json['data']['relationships']
            assert list(relationships.keys()) == ['groups']
            assert osf_group._id in relationships['groups']['links'][
                'related']['href']

            # get group that does not exist
            res = app.get(public_detail_url.replace(osf_group._id,
                                                    'hellonotarealroute'),
                          expect_errors=True)
            assert res.status_code == 404
示例#18
0
    def test_course_waffle_flag(self, data):
        """
        Tests various combinations of a flag being set in waffle and overridden
        for a course.
        """
        with patch.object(WaffleFlagCourseOverrideModel, 'override_value', return_value=data['course_override']):
            with override_flag(self.NAMESPACED_FLAG_NAME, active=data['waffle_enabled']):
                # check twice to test that the result is properly cached
                self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_KEY), data['result'])
                self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_KEY), data['result'])
                # result is cached, so override check should happen once
                WaffleFlagCourseOverrideModel.override_value.assert_called_once_with(
                    self.NAMESPACED_FLAG_NAME,
                    self.TEST_COURSE_KEY
                )

        # check flag for a second course
        if data['course_override'] == WaffleFlagCourseOverrideModel.ALL_CHOICES.unset:
            # When course override wasn't set for the first course, the second course will get the same
            # cached value from waffle.
            self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_2_KEY), data['waffle_enabled'])
        else:
            # When course override was set for the first course, it should not apply to the second
            # course which should get the default value of False.
            self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_2_KEY), False)
示例#19
0
    def test_return(self, app, member, manager, user, osf_group, url):
        with override_flag(OSF_GROUPS, active=True):
            # test nonauthenticated
            res = app.get(url)
            assert res.status_code == 200
            data = res.json['data']
            assert len(data) == 0

            # test authenticated user
            res = app.get(url, auth=user.auth)
            assert res.status_code == 200
            data = res.json['data']
            assert len(data) == 0

            # test authenticated member
            res = app.get(url, auth=member.auth)
            assert res.status_code == 200
            data = res.json['data']
            assert len(data) == 1
            assert data[0]['id'] == osf_group._id
            assert data[0]['type'] == 'groups'
            assert data[0]['attributes']['name'] == osf_group.name

            # test authenticated manager
            res = app.get(url, auth=manager.auth)
            assert res.status_code == 200
            data = res.json['data']
            assert len(data) == 1
            assert data[0]['id'] == osf_group._id
            assert data[0]['type'] == 'groups'
            assert data[0]['attributes']['name'] == osf_group.name
示例#20
0
    def test_patch_osf_group_perms(self, app, member, manager, user, osf_group,
                                   url, name_payload, new_name):
        with override_flag(OSF_GROUPS, active=True):
            # test unauthenticated
            res = app.patch_json_api(url, expect_errors=True)
            assert res.status_code == 401

            # test authenticated_user
            res = app.patch_json_api(url, {},
                                     auth=user.auth,
                                     expect_errors=True)
            assert res.status_code == 403

            # test authenticated_member
            res = app.patch_json_api(url, {},
                                     auth=member.auth,
                                     expect_errors=True)
            assert res.status_code == 403

            # test authenticated_manager
            res = app.patch_json_api(url,
                                     name_payload,
                                     auth=manager.auth,
                                     expect_errors=True)
            assert res.status_code == 200
            assert res.json['data']['attributes']['name'] == new_name
示例#21
0
文件: conftest.py 项目: mozilla/kuma
def moderator_client(client, wiki_moderator):
    """A test client with wiki_moderator logged in."""
    wiki_moderator.set_password('password')
    wiki_moderator.save()
    client.login(username=wiki_moderator.username, password='******')
    with override_flag('kumaediting', True):
        yield client
    def test_node_group_detail_perms(self, app, non_contrib, osf_group, member,
                                     public_project, private_project,
                                     public_detail_url, private_url):
        with override_flag(OSF_GROUPS, active=True):
            public_project.add_osf_group(osf_group, permissions.READ)
            private_project.add_osf_group(osf_group, permissions.WRITE)
            private_detail_url = private_url + osf_group._id + '/'

            # nonauth
            res = app.get(private_detail_url, expect_errors=True)
            assert res.status_code == 401

            res = app.get(public_detail_url)
            assert res.status_code == 200

            # noncontrib
            res = app.get(private_detail_url,
                          auth=non_contrib.auth,
                          expect_errors=True)
            assert res.status_code == 403

            res = app.get(public_detail_url, auth=non_contrib.auth)
            assert res.status_code == 200

            # member
            res = app.get(private_detail_url, auth=member.auth)
            assert res.status_code == 200

            res = app.get(public_detail_url, auth=member.auth)
            assert res.status_code == 200
示例#23
0
        def wrapper(*args, **kwargs):
            """
            Provides the actual override functionality of the decorator.

            Saves the previous cached value of the flag and restores it (if it
            was set), after overriding it.

            """
            waffle_namespace = flag.waffle_namespace
            namespaced_flag_name = waffle_namespace._namespaced_name(
                flag.flag_name)

            # save previous value and whether it existed in the cache
            cached_value_existed = namespaced_flag_name in waffle_namespace._cached_flags
            if cached_value_existed:
                previous_value = waffle_namespace._cached_flags[
                    namespaced_flag_name]

            # set new value
            waffle_namespace._cached_flags[namespaced_flag_name] = active

            with override_flag(namespaced_flag_name, active):
                # call wrapped function
                function(*args, **kwargs)

            # restore value
            if cached_value_existed:
                waffle_namespace._cached_flags[
                    namespaced_flag_name] = previous_value
            elif namespaced_flag_name in waffle_namespace._cached_flags:
                del waffle_namespace._cached_flags[namespaced_flag_name]
    def test_bulk_create_unregistered(self, app, manager, user, osf_group,
                                      url):
        with override_flag(OSF_GROUPS, active=True):
            payload_user = make_bulk_create_payload(MEMBER, user)
            payload_unregistered = make_bulk_create_payload(
                MEMBER, user=None, full_name='Crazy 8s', email='*****@*****.**')
            res = app.post_json_api(
                url, {'data': [payload_user, payload_unregistered]},
                auth=manager.auth,
                bulk=True)
            unreg_user = OSFUser.objects.get(username='******')
            assert res.status_code == 201
            ids = [user_data['id'] for user_data in res.json['data']]
            roles = [
                user_data['attributes']['role']
                for user_data in res.json['data']
            ]
            assert '{}-{}'.format(osf_group._id, user._id) in ids
            assert '{}-{}'.format(osf_group._id, unreg_user._id) in ids
            assert roles[0] == MEMBER
            assert roles[1] == MEMBER
            unregistered_names = [
                user_data['attributes']['unregistered_member']
                for user_data in res.json['data']
            ]
            assert set(['Crazy 8s', None]) == set(unregistered_names)

            assert osf_group.has_permission(user, MANAGE) is False
            assert osf_group.has_permission(user, MEMBER) is True
            assert osf_group.has_permission(unreg_user, MANAGE) is False
            assert osf_group.has_permission(unreg_user, MEMBER) is True
            assert osf_group.is_member(unreg_user) is True
            assert osf_group.is_manager(unreg_user) is False
示例#25
0
        def wrapper(*args, **kwargs):
            """
            Provides the actual override functionality of the decorator.

            Saves the previous cached value of the flag and restores it (if it
            was set), after overriding it.

            """
            waffle_namespace = flag.waffle_namespace
            namespaced_flag_name = waffle_namespace._namespaced_name(flag.flag_name)

            # save previous value and whether it existed in the cache
            cached_value_existed = namespaced_flag_name in waffle_namespace._cached_flags
            if cached_value_existed:
                previous_value = waffle_namespace._cached_flags[namespaced_flag_name]

            # set new value
            waffle_namespace._cached_flags[namespaced_flag_name] = active

            with override_flag(namespaced_flag_name, active):
                # call wrapped function
                function(*args, **kwargs)

            # restore value
            if cached_value_existed:
                waffle_namespace._cached_flags[namespaced_flag_name] = previous_value
            elif namespaced_flag_name in waffle_namespace._cached_flags:
                del waffle_namespace._cached_flags[namespaced_flag_name]
示例#26
0
def moderator_client(client, wiki_moderator):
    """A test client with wiki_moderator logged in."""
    wiki_moderator.set_password('password')
    wiki_moderator.save()
    client.login(username=wiki_moderator.username, password='******')
    with override_flag('kumaediting', True):
        yield client
示例#27
0
    def test_delete_specifics(self, app, osf_group, manager, member, user,
                              url):
        with override_flag(OSF_GROUPS, active=True):
            osf_group_name = osf_group.name
            manager_group_name = osf_group.manager_group.name
            member_group_name = osf_group.member_group.name

            assert manager_group_name in manager.groups.values_list('name',
                                                                    flat=True)
            assert member_group_name in member.groups.values_list('name',
                                                                  flat=True)

            res = app.delete_json_api(url, auth=manager.auth)
            assert res.status_code == 204

            assert not OSFGroup.objects.filter(name=osf_group_name).exists()
            assert not Group.objects.filter(name=manager_group_name).exists()
            assert not Group.objects.filter(name=member_group_name).exists()

            assert manager_group_name not in manager.groups.values_list(
                'name', flat=True)
            assert member_group_name not in member.groups.values_list(
                'name', flat=True)

            res = app.get(url, auth=manager.auth, expect_errors=True)
            assert res.status_code == 404
示例#28
0
    def test_should_not_cache_if_waffled(self, waffle_active):
        """ Verify that the decorator does not cache the waffle flag is turned off """
        def key_func(**kwargs):  # pylint: disable=unused-argument
            return self.cache_response_key

        class TestView(views.APIView):
            permission_classes = [permissions.AllowAny]
            renderer_classes = [JSONRenderer]

            @compressed_cache_response(key_func=key_func)
            def get(self, request, *_args, **_kwargs):
                return Response('test response')

        with override_flag('compressed_cache.TestView.get',
                           active=waffle_active):

            view_instance = TestView()
            view_instance.headers = {}  # pylint: disable=attribute-defined-outside-init
            view_instance.dispatch(request=self.request)

        # Verify nothing was cached
        if waffle_active:
            self.assertIsNot(cache.get(self.cache_response_key), None)
        else:
            self.assertIs(cache.get(self.cache_response_key), None)
示例#29
0
    def test_create_osf_group_validation_errors(self, app, url, manager,
                                                simple_payload):
        # Need data key
        with override_flag(OSF_GROUPS, active=True):
            res = app.post_json_api(url,
                                    simple_payload['data'],
                                    auth=manager.auth,
                                    expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0][
                'detail'] == 'Request must include /data.'

            # Incorrect type
            simple_payload['data']['type'] = 'incorrect_type'
            res = app.post_json_api(url,
                                    simple_payload,
                                    auth=manager.auth,
                                    expect_errors=True)
            assert res.status_code == 409

            # Required name field
            payload = {'data': {'type': 'groups'}}
            res = app.post_json_api(url,
                                    payload,
                                    auth=manager.auth,
                                    expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'This field is required.'
示例#30
0
    def test_add_unregistered_member(self, app, manager, osf_group, url):
        with override_flag(OSF_GROUPS, active=True):
            full_name = 'Crazy 8s'
            payload = make_create_payload(MEMBER, user=None, full_name=full_name, email='*****@*****.**')
            res = app.post_json_api(url, payload, auth=manager.auth)
            assert res.status_code == 201
            data = res.json['data']
            assert data['attributes']['role'] == MEMBER
            user = OSFUser.load(data['id'].split('-')[1])
            assert user._id in data['relationships']['users']['links']['related']['href']
            assert osf_group.has_permission(user, MANAGE) is False
            assert data['attributes']['full_name'] == full_name
            assert data['attributes']['unregistered_member'] == full_name
            assert osf_group.has_permission(user, MEMBER) is True
            assert user in osf_group.members_only
            assert user not in osf_group.managers

            # test unregistered user is already a member
            res = app.post_json_api(url, payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'User already exists.'

            # test unregistered user email is blocked
            payload['data']['attributes']['email'] = '*****@*****.**'
            res = app.post_json_api(url, payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'Email address domain is blocked.'
示例#31
0
    def test_groups_filter(self, app, member, manager, user, osf_group, url):
        with override_flag(OSF_GROUPS, active=True):
            second_group = OSFGroupFactory(name='Apples', creator=manager)
            res = app.get(url + '?filter[name]=Platform', auth=manager.auth)
            assert res.status_code == 200
            data = res.json['data']
            assert len(data) == 1
            assert data[0]['id'] == osf_group._id

            res = app.get(url + '?filter[name]=Apple', auth=manager.auth)
            assert res.status_code == 200
            data = res.json['data']
            assert len(data) == 1
            assert data[0]['id'] == second_group._id

            res = app.get(url + '?filter[bad_field]=Apple',
                          auth=manager.auth,
                          expect_errors=True)
            assert res.status_code == 400

            res = app.get(url + '?filter[name]=Platform')
            assert res.status_code == 200
            data = res.json['data']
            assert len(data) == 0

            res = app.get(url + '?filter[name]=Apple')
            assert res.status_code == 200
            data = res.json['data']
            assert len(data) == 0
    def test_return_member(self, app, member, manager, osf_group, url):
        with override_flag(OSF_GROUPS, active=True):
            res = app.get(url)
            assert res.status_code == 200
            data = res.json['data']
            assert data['id'] == '{}-{}'.format(osf_group._id, member._id)
            assert data['type'] == 'group-members'
            assert data['attributes']['role'] == MEMBER
            assert data['attributes']['unregistered_member'] is None
            assert data['attributes']['full_name'] == member.fullname
            assert member._id in data['relationships']['users']['links'][
                'related']['href']

            user = osf_group.add_unregistered_member('Crazy 8s',
                                                     '*****@*****.**',
                                                     Auth(manager), MANAGER)
            res = app.get('/{}groups/{}/members/{}/'.format(
                API_BASE, osf_group._id, user._id))
            assert res.status_code == 200
            data = res.json['data']
            assert data['id'] == '{}-{}'.format(osf_group._id, user._id)
            assert data['type'] == 'group-members'
            assert data['attributes']['role'] == MANAGER
            assert data['attributes']['unregistered_member'] == 'Crazy 8s'
            assert data['attributes']['full_name'] == 'Crazy 8s'
            assert res.json['data']['attributes']['full_name'] == 'Crazy 8s'
示例#33
0
 def test_return_members(self, app, member, manager, user, osf_group, url):
     with override_flag(OSF_GROUPS, active=True):
         res = app.get(url)
         data = res.json['data']
         assert len(data) == 2
         member_ids = [mem['id'] for mem in data]
         assert '{}-{}'.format(osf_group._id, manager._id) in member_ids
         assert '{}-{}'.format(osf_group._id, member._id) in member_ids
    def test_filter_groups(self, app, osf_group, private_project, manager,
                           private_url, make_group_id):
        with override_flag(OSF_GROUPS, active=True):
            read_group = OSFGroupFactory(creator=manager, name='house')
            write_group = OSFGroupFactory(creator=manager, name='doghouse')
            private_project.add_osf_group(read_group, permissions.READ)
            private_project.add_osf_group(write_group, permissions.WRITE)
            private_project.add_osf_group(osf_group, permissions.ADMIN)

            # test filter on permission
            url = private_url + '?filter[permission]=admin'
            res = app.get(url, auth=private_project.creator.auth)
            resp_json = res.json['data']
            ids = [each['id'] for each in resp_json]
            assert make_group_id(private_project, osf_group) in ids
            assert make_group_id(private_project, write_group) not in ids
            assert make_group_id(private_project, read_group) not in ids

            url = private_url + '?filter[permission]=write'
            res = app.get(url, auth=private_project.creator.auth)
            resp_json = res.json['data']
            ids = [each['id'] for each in resp_json]
            assert make_group_id(private_project, osf_group) in ids
            assert make_group_id(private_project, write_group) in ids
            assert make_group_id(private_project, read_group) not in ids

            url = private_url + '?filter[permission]=read'
            res = app.get(url, auth=private_project.creator.auth)
            resp_json = res.json['data']
            ids = [each['id'] for each in resp_json]
            assert make_group_id(private_project, osf_group) in ids
            assert make_group_id(private_project, write_group) in ids
            assert make_group_id(private_project, read_group) in ids

            # test_filter_on_invalid_permission
            url = private_url + '?filter[permission]=bad_perm'
            res = app.get(url,
                          auth=private_project.creator.auth,
                          expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0][
                'detail'] == 'bad_perm is not a filterable permission.'

            url = private_url + '?filter[name]=Plat'
            res = app.get(url, auth=private_project.creator.auth)
            resp_json = res.json['data']
            ids = [each['id'] for each in resp_json]
            assert make_group_id(private_project, osf_group) in ids
            assert make_group_id(private_project, write_group) not in ids
            assert make_group_id(private_project, read_group) not in ids

            url = private_url + '?filter[name]=house'
            res = app.get(url, auth=private_project.creator.auth)
            resp_json = res.json['data']
            ids = [each['id'] for each in resp_json]
            assert make_group_id(private_project, osf_group) not in ids
            assert make_group_id(private_project, write_group) in ids
            assert make_group_id(private_project, read_group) in ids
示例#35
0
    def test_enable_hoist_order_history(self, enable_hoist_order_history_flag):
        """ Verify that orders contain the Order History flag value """
        with override_flag(ENABLE_HOIST_ORDER_HISTORY, active=enable_hoist_order_history_flag):
            create_order(site=self.site, user=self.user)
            response = self.client.get(self.path, HTTP_AUTHORIZATION=self.token)
            self.assertEqual(response.status_code, 200)
            content = json.loads(response.content.decode('utf-8'))

            self.assertEqual(content['results'][0]['enable_hoist_order_history'], enable_hoist_order_history_flag)
示例#36
0
    def test_create_members_errors(self, app, manager, member, user3, osf_group, url):
        with override_flag(OSF_GROUPS, active=True):
            # invalid user
            bad_user_payload = make_create_payload(MEMBER, user=user3)
            bad_user_payload['data']['relationships']['users']['data']['id'] = 'bad_user_id'
            res = app.post_json_api(url, bad_user_payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 404
            assert res.json['errors'][0]['detail'] == 'User with id bad_user_id not found.'

            # invalid type
            bad_type_payload = make_create_payload(MEMBER, user=user3)
            bad_type_payload['data']['type'] = 'bad_type'
            res = app.post_json_api(url, bad_type_payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 409

            # invalid role
            bad_perm_payload = make_create_payload('bad_role', user=user3)
            res = app.post_json_api(url, bad_perm_payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'bad_role is not a valid role; choose manager or member.'

            # fullname not included
            unregistered_payload = make_create_payload(MEMBER, user=None, full_name=None, email='*****@*****.**')
            res = app.post_json_api(url, unregistered_payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'You must provide a full_name/email combination to add an unconfirmed member.'

            # email not included
            unregistered_payload = make_create_payload(MEMBER, user=None, full_name='Crazy 8s', email=None)
            res = app.post_json_api(url, unregistered_payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'You must provide a full_name/email combination to add an unconfirmed member.'

            # user is already a member
            existing_member_payload = make_create_payload(MEMBER, user=member)
            res = app.post_json_api(url, existing_member_payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'User is already a member of this group.'

            # Disabled user
            user3.date_disabled = timezone.now()
            user3.save()
            payload = make_create_payload(MEMBER, user=user3)
            res = app.post_json_api(url, payload, auth=manager.auth, expect_errors=True)
            assert res.status_code == 400
            assert res.json['errors'][0]['detail'] == 'Deactivated users cannot be added to OSF Groups.'

            # No role specified - given member by default
            user3.date_disabled = None
            user3.save()
            payload = make_create_payload(MEMBER, user=user3)
            payload['attributes'] = {}
            res = app.post_json_api(url, payload, auth=manager.auth)
            assert res.status_code == 201
            assert res.json['data']['attributes']['role'] == MEMBER
            assert osf_group.has_permission(user3, 'member')
            assert not osf_group.has_permission(user3, 'manager')
示例#37
0
def test_translate_behind_flag(client):
    url = reverse('pontoon.translate.next')

    response = client.get(url)
    assert response.status_code == 302

    with override_flag('translate_next', active=True):
        response = client.get(url)
        assert response.status_code == 200
示例#38
0
    def test_start_date_handling(self):
        """
        Verify that the course home page handles start dates correctly.
        """
        # The course home page should 404 for a course starting in the future
        future_course = self.create_future_course(datetime(2030, 1, 1, tzinfo=UTC))
        url = course_home_url(future_course)
        response = self.client.get(url)
        self.assertRedirects(response, '/dashboard?notlive=Jan+01%2C+2030')

        # With the Waffle flag enabled, the course should be visible
        with override_flag(COURSE_PRE_START_ACCESS_FLAG.namespaced_flag_name, True):
            url = course_home_url(future_course)
            response = self.client.get(url)
            self.assertEqual(response.status_code, 200)
示例#39
0
    def test_course_waffle_flag(self, data):
        """
        Tests various combinations of a flag being set in waffle and overridden
        for a course.
        """
        RequestCache.clear_request_cache()

        with patch.object(WaffleFlagCourseOverrideModel, 'override_value', return_value=data['course_override']):
            with override_flag(self.NAMESPACED_FLAG_NAME, active=data['waffle_enabled']):
                # check twice to test that the result is properly cached
                self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_KEY), data['result'])
                self.assertEqual(self.TEST_COURSE_FLAG.is_enabled(self.TEST_COURSE_KEY), data['result'])
                # result is cached, so override check should happen once
                WaffleFlagCourseOverrideModel.override_value.assert_called_once_with(
                    self.NAMESPACED_FLAG_NAME,
                    self.TEST_COURSE_KEY
                )
示例#40
0
文件: conftest.py 项目: mozilla/kuma
def editor_client(user_client):
    """A test client with wiki_user logged in for editing."""
    with override_flag('kumaediting', True):
        yield user_client
示例#41
0
def enable_akismet_submissions(constance_config):
    constance_config.AKISMET_KEY = 'dashboard'
    with override_flag(SPAM_SUBMISSIONS_FLAG, True):
        yield constance_config
示例#42
0
def purge_client(admin_client):
    with override_flag('kumaediting', True):
        yield admin_client
示例#43
0
文件: conftest.py 项目: Elchi3/kuma
def spam_check_everyone(db):
    with override_flag(SPAM_CHECKS_FLAG, True):
        yield