Exemplo n.º 1
0
 def _org_create(self):
     view = OrganizationProfileViewSet.as_view({
         'get': 'list',
         'post': 'create'
     })
     request = self.factory.get('/', **self.extra)
     response = view(request)
     self.assertEqual(response.status_code, 200)
     data = {
         'org': u'denoinc',
         'name': u'Dennis',
         'city': u'Denoville',
         'country': u'US',
         'home_page': u'deno.com',
         'twitter': u'denoinc',
         'description': u'',
         'address': u'',
         'phonenumber': u'',
         'require_auth': False,
     }
     request = self.factory.post(
         '/', data=json.dumps(data),
         content_type="application/json", **self.extra)
     response = view(request)
     self.assertEqual(response.status_code, 201)
     data['url'] = 'http://testserver/api/v1/orgs/denoinc'
     data['user'] = '******'
     data['creator'] = 'http://testserver/api/v1/users/bob'
     self.assertDictContainsSubset(data, response.data)
     self.company_data = response.data
     self.organization = OrganizationProfile.objects.get(
         user__username=data['org'])
    def test_put_bad_role(self):
        self._org_create()
        newname = 'aboy'
        view = OrganizationProfileViewSet.as_view({
            'get': 'retrieve',
            'post': 'members',
            'put': 'members'
        })

        User.objects.create(username=newname)
        data = {'username': newname}
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user='******')
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data, [u'denoinc', newname])

        data = {'username': newname, 'role': 42}
        request = self.factory.put(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user='******')
        self.assertEqual(response.status_code, 400)
    def test_role_for_org_non_owner(self):
        # creating org with member
        self._org_create()
        view = OrganizationProfileViewSet.as_view({
            'get': 'retrieve',
            'post': 'members'
        })

        User.objects.create(username='******')
        data = {'username': '******'}
        user_role = 'member'
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)
        response = view(request, user='******')

        # getting profile
        request = self.factory.get('/', **self.extra)
        response = view(request, user='******')
        self.assertEqual(response.status_code, 200)
        self.assertIn('users', response.data.keys())

        for user in response.data['users']:
            username = user['user']
            role = user['role']
            expected_role = 'owner' if username == 'denoinc' else user_role
            self.assertEqual(role, expected_role)
    def test_remove_members_from_org(self):
        self._org_create()
        newname = 'aboy'
        view = OrganizationProfileViewSet.as_view({
            'post': 'members',
            'delete': 'members'
        })

        User.objects.create(username=newname)
        data = {'username': newname}
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user='******')
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data, [u'denoinc', newname])

        request = self.factory.delete(
            '/', json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user='******')
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data, [u'denoinc'])
Exemplo n.º 5
0
 def _org_create(self):
     view = OrganizationProfileViewSet.as_view({
         'get': 'list',
         'post': 'create'
     })
     request = self.factory.get('/', **self.extra)
     response = view(request)
     self.assertEqual(response.status_code, 200)
     data = {
         'org': u'denoinc',
         'name': u'Dennis',
         'city': u'Denoville',
         'country': u'US',
         'home_page': u'deno.com',
         'twitter': u'denoinc',
         'description': u'',
         'address': u'',
         'phonenumber': u'',
         'require_auth': False,
     }
     request = self.factory.post('/',
                                 data=json.dumps(data),
                                 content_type="application/json",
                                 **self.extra)
     response = view(request)
     self.assertEqual(response.status_code, 201)
     data['url'] = 'http://testserver/api/v1/orgs/denoinc'
     data['user'] = '******'
     data['creator'] = 'http://testserver/api/v1/users/bob'
     self.assertDictContainsSubset(data, response.data)
     self.company_data = response.data
     self.organization = OrganizationProfile.objects.get(
         user__username=data['org'])
    def test_member_sees_orgs_added_to(self):
        self._org_create()
        view = OrganizationProfileViewSet.as_view({
            'get': 'list',
            'post': 'members'
        })

        member = 'aboy'
        expected_data = self.company_data
        expected_data['users'].append({'role': 'member', 'user': member})
        cur_username = self.profile_data['username']
        self.profile_data['username'] = member
        self._login_user_and_profile()
        self.profile_data['username'] = cur_username
        self._login_user_and_profile()

        data = {'username': member}
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)
        response = view(request, user='******')
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data, [u'denoinc', u'aboy'])

        self.profile_data['username'] = member
        self._login_user_and_profile()

        request = self.factory.get('/', **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [expected_data])
 def test_add_members_to_org_username_required(self):
     self._org_create()
     view = OrganizationProfileViewSet.as_view({
         'post': 'members'
     })
     request = self.factory.post('/', data={}, **self.extra)
     response = view(request, user='******')
     self.assertEqual(response.status_code, 400)
     self.assertEqual(response.data,
                      {u'username': [u"This field is required."]})
    def test_orgs_members_list(self):
        self._org_create()
        view = OrganizationProfileViewSet.as_view({
            'get': 'members'
        })

        request = self.factory.get('/', **self.extra)
        response = view(request, user='******')
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.data, [u'denoinc'])
    def test_add_members_to_org_user_does_not_exist(self):
        self._org_create()
        view = OrganizationProfileViewSet.as_view({
            'post': 'members'
        })
        data = {'username': '******'}
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user='******')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data,
                         {u'username': [u"User `aboy` does not exist."]})
 def test_orgs_get_anon(self):
     self._org_create()
     view = OrganizationProfileViewSet.as_view({
         'get': 'retrieve'
     })
     request = self.factory.get('/')
     response = view(request, user='******')
     self.assertNotEqual(response.get('Last-Modified'), None)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data, self.company_data)
     self.assertIn('users', response.data.keys())
     for user in response.data['users']:
         self.assertEqual(user['role'], 'owner')
         self.assertEqual(type(user['user']), unicode)
    def test_add_members_to_org_with_anonymous_user(self):
        self._org_create()
        view = OrganizationProfileViewSet.as_view({
            'post': 'members'
        })

        User.objects.create(username='******')
        data = {'username': '******'}
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json")

        response = view(request, user='******')
        self.assertEqual(response.status_code, 401)
        self.assertNotEquals(response.data, [u'denoinc', u'aboy'])
    def test_orgs_list_restricted(self):
        self._org_create()
        view = OrganizationProfileViewSet.as_view({
            'get': 'list'
        })

        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        self.assertEqual(self.user.username, 'alice')

        request = self.factory.get('/', **self.extra)
        response = view(request)

        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.data, [])
 def test_orgs_get_not_creator(self):
     self._org_create()
     view = OrganizationProfileViewSet.as_view({
         'get': 'retrieve'
     })
     alice_data = {'username': '******', 'email': '*****@*****.**'}
     previous_user = self.user
     self._login_user_and_profile(extra_post_data=alice_data)
     self.assertEqual(self.user.username, 'alice')
     self.assertNotEqual(previous_user,  self.user)
     request = self.factory.get('/', **self.extra)
     response = view(request, user='******')
     self.assertNotEqual(response.get('Last-Modified'), None)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data, self.company_data)
     self.assertIn('users', response.data.keys())
     for user in response.data['users']:
         self.assertEqual(user['role'], 'owner')
         self.assertEqual(type(user['user']), unicode)
    def test_add_members_to_org_with_non_member_user(self):
        self._org_create()
        view = OrganizationProfileViewSet.as_view({
            'post': 'members'
        })

        User.objects.create(username='******', )
        data = {'username': '******'}
        previous_user = self.user
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        self.assertEqual(self.user.username, 'alice')
        self.assertNotEqual(previous_user,  self.user)
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user='******')
        self.assertEqual(response.status_code, 404)
        self.assertNotEqual(response.data, [u'denoinc', u'aboy'])
    def test_put_change_role(self):
        self._org_create()
        newname = 'aboy'
        view = OrganizationProfileViewSet.as_view({
            'get': 'retrieve',
            'post': 'members',
            'put': 'members'
        })

        User.objects.create(username=newname)
        data = {'username': newname}
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user='******')
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data, [u'denoinc', newname])

        user_role = 'editor'
        data = {'username': newname, 'role': user_role}
        request = self.factory.put(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user='******')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [u'denoinc', newname])

        # getting profile
        request = self.factory.get('/', **self.extra)
        response = view(request, user='******')
        self.assertEqual(response.status_code, 200)
        self.assertIn('users', response.data.keys())

        for user in response.data['users']:
            username = user['user']
            role = user['role']
            expected_role = 'owner' if username == 'denoinc' else user_role
            self.assertEqual(role, expected_role)
 def setUp(self):
     super(self.__class__, self).setUp()
     self.view = OrganizationProfileViewSet.as_view({
         'get': 'list',
         'post': 'create'
     })