def setUp(self): self.list_view = ViewSet.as_view({'get': 'list'}) self.detailed_view = ViewSet.as_view({'get': 'retrieve'}) self.anonymous_user = AnonymousUserFactory() self.user_without_sources = UserFactory.create( username='******') self.user_with_sources = UserFactory.create( username='******') self.user = self.user_with_sources self.provider = ProviderFactory.create() self.user_identity = IdentityFactory.create_identity( created_by=self.user_without_sources, provider=self.provider) self.user_identity = IdentityFactory.create_identity( created_by=self.user_with_sources, provider=self.provider) self.allocation_source_1 = AllocationSourceFactory.create( name='TG-TRA110001', compute_allowed=1000) self.allocation_source_2 = AllocationSourceFactory.create( name='TG-TRA220002', compute_allowed=2000) self.allocation_source_3 = AllocationSourceFactory.create( name='TG-TRA330003', compute_allowed=3000) UserAllocationSourceFactory.create( user=self.user_with_sources, allocation_source=self.allocation_source_1) UserAllocationSourceFactory.create( user=self.user_with_sources, allocation_source=self.allocation_source_2)
def setUp(self): self.list_view = ViewSet.as_view({'get': 'list'}) self.detailed_view = ViewSet.as_view({'get': 'retrieve'}) self.anonymous_user = AnonymousUserFactory() self.user_without_sources = UserFactory.create(username='******') self.user_with_sources = UserFactory.create(username='******') self.user = self.user_with_sources self.provider = ProviderFactory.create() self.user_identity = IdentityFactory.create_identity( created_by=self.user_without_sources, provider=self.provider) self.user_identity = IdentityFactory.create_identity( created_by=self.user_with_sources, provider=self.provider) self.allocation_source_1 = AllocationSourceFactory.create(name='TG-TRA110001', compute_allowed=1000) self.allocation_source_2 = AllocationSourceFactory.create(name='TG-TRA220002', compute_allowed=2000) self.allocation_source_3 = AllocationSourceFactory.create(name='TG-TRA330003', compute_allowed=3000) UserAllocationSourceFactory.create(user=self.user_with_sources, allocation_source=self.allocation_source_1) UserAllocationSourceFactory.create(user=self.user_with_sources, allocation_source=self.allocation_source_2)
def test_anonymous_user_cant_see_allocation_sources(self): request_factory = APIRequestFactory() list_view = ViewSet.as_view({'get': 'list'}) url = urlresolvers.reverse('api:v2:allocationsource-list') self.assertEqual(url, '/api/v2/allocation_sources') request = request_factory.get(url) force_authenticate(request, user=self.anonymous_user) response = list_view(request) self.assertEqual(response.status_code, 403) self.assertEqual(response.status_text, 'Forbidden')
def test_loggedin_user_can_get_allocation_source(self): request_factory = APIRequestFactory() retrieve_view = ViewSet.as_view({'get': 'retrieve'}) url = urlresolvers.reverse('api:v2:allocationsource-detail', args=(self.allocation_source_1.id,)) self.assertEqual(url, '/api/v2/allocation_sources/{}'.format(self.allocation_source_1.id)) request = request_factory.get(url) force_authenticate(request, user=self.user_with_sources) response = retrieve_view(request) self.assertEqual(response.status_code, 200) self.assertEqual(response.status_text, 'OK')
def test_loggedin_user_with_no_sources_cant_see_allocation_sources(self): request_factory = APIRequestFactory() list_view = ViewSet.as_view({'get': 'list'}) url = urlresolvers.reverse('api:v2:allocationsource-list') self.assertEqual(url, '/api/v2/allocation_sources') request = request_factory.get(url) force_authenticate(request, user=self.user_without_sources) response = list_view(request) self.assertEqual(response.status_code, 200) self.assertEqual(response.status_text, 'OK') self.assertEqual(response.data['count'], 0)
def test_loggedin_user_can_list_allocation_sources(self): request_factory = APIRequestFactory() list_view = ViewSet.as_view({'get': 'list'}) url = urlresolvers.reverse('api:v2:allocationsource-list') self.assertEqual(url, '/api/v2/allocation_sources') request = request_factory.get(url) force_authenticate(request, user=self.user_with_sources) response = list_view(request) self.assertEqual(response.status_code, 200) self.assertEqual(response.status_text, 'OK') expected_values = [{ 'name': 'TG-TRA110001', 'compute_allowed': 1000 }, { 'name': 'TG-TRA220002', 'compute_allowed': 2000 }] self.assertEqual(response.data['count'], len(expected_values)) for allocation_source, expected_dict in itertools.izip_longest( expected_values, response.data['results']): self.assertDictContainsSubset(allocation_source, expected_dict)
def test_loggedin_user_can_list_allocation_sources(self): request_factory = APIRequestFactory() list_view = ViewSet.as_view({'get': 'list'}) url = urlresolvers.reverse('api:v2:allocationsource-list') self.assertEqual(url, '/api/v2/allocation_sources') request = request_factory.get(url) force_authenticate(request, user=self.user_with_sources) response = list_view(request) self.assertEqual(response.status_code, 200) self.assertEqual(response.status_text, 'OK') expected_values = [ { 'name': 'TG-TRA110001', 'compute_allowed': 1000 }, { 'name': 'TG-TRA220002', 'compute_allowed': 2000 } ] self.assertEqual(response.data['count'], len(expected_values)) for allocation_source, expected_dict in itertools.izip_longest(expected_values, response.data['results']): self.assertDictContainsSubset(allocation_source, expected_dict)
def save_model(self, request, obj, form, change): from api.v2.views import AllocationSourceViewSet request.data = { "renewal_strategy": obj.renewal_strategy, "name": obj.name, "compute_allowed": obj.compute_allowed } if not change: api = AllocationSourceViewSet() api.create(request) else: request.data = {} # renewal strategy modified if form.initial['renewal_strategy'] != obj.renewal_strategy: request.data['renewal_strategy'] = obj.renewal_strategy # compute allowed modified if form.initial['compute_allowed'] != obj.compute_allowed: request.data['compute_allowed'] = obj.compute_allowed if request.data: api = AllocationSourceViewSet() api.update(request, obj.uuid) # if allocation source is end dated if form.initial['end_date'] != obj.end_date: api = AllocationSourceViewSet() api.perform_destroy(obj, request=request)