def test_get_bills_list(self, new_user, other_user): Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance( mixer.faker.genre()), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance( mixer.faker.genre()), user=new_user) good_list_view = BillViewSet.as_view({'get': 'list'}) good_list_request = factory.get( BILLS_URI, HTTP_AUTHORIZATION=get_jwt_header(new_user.email), ) good_list_response = good_list_view(good_list_request) assert good_list_response.status_code == 200 bills = good_list_response.data assert len(bills) == 2 for bill in bills: assert bill.get('id') assert bill.get('name') assert bill.get('description') assert bill.get('due_date') assert bill.get('service_id') assert bill.get('user_id') == new_user.id
def test_service_get_list(self, new_user): Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('House'), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('House'), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('Medical'), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('Medical'), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('Medical'), user=new_user) view = ServiceList.as_view() request = factory.get(SERVICES_URI, content_type='application/json', HTTP_AUTHORIZATION=get_jwt_header( new_user.email)) response = view(request) assert response.status_code == 200 assert len(response.data) == 2 for service in response.data: assert service.get('id') if service.get('name') == 'Medical': assert len(service.get('bills')) == 3 elif service.get('name') == 'House': assert len(service.get('bills')) == 2 for bill in service.get('bills'): assert bill.get('id') assert bill.get('name') assert bill.get('description') assert bill.get('due_date') assert bill.get('service_id') == service.get('id') assert bill.get('user_id') == new_user.id
def test_get_bills_and_query_by_service(self, new_user): Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('House'), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('House'), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('House'), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('Membership'), user=new_user) Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance('Membership'), user=new_user) view = BillViewSet.as_view({'get': 'list'}) Bill.objects.count() == 5 membership_request = factory.get( BILLS_URI + '?service=Membership', HTTP_AUTHORIZATION=get_jwt_header(new_user.email), ) membership_response = view(membership_request) assert membership_response.status_code == 200 assert len(membership_response.data) == 2 for bill in membership_response.data: assert bill.get('id') assert bill.get('service_id') assert bill.get('user_id') == new_user.id house_request = factory.get( BILLS_URI + '?service=House', HTTP_AUTHORIZATION=get_jwt_header(new_user.email), ) house_response = view(house_request) assert house_response.status_code == 200 assert len(house_response.data) == 3 for bill in house_response.data: assert bill.get('id') assert bill.get('service_id') assert bill.get('user_id') == new_user.id
def test_handle_service_instance(self): school_service_1 = handle_service_instance('school') assert school_service_1.name == 'School' assert school_service_1.id assert Service.objects.count() == 1 school_service_2 = handle_service_instance('ScHooL') assert school_service_2.name == 'School' assert school_service_2.id == school_service_1.id assert Service.objects.count() == 1 work_service = handle_service_instance('WORK') assert work_service.name == 'Work' assert work_service.id assert Service.objects.count() == 2
def test_bill_update_by_good_user(self, new_user, other_bill_info): b1 = Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance( mixer.faker.genre()), user=new_user) assert b1.id assert b1.user == new_user view = BillViewSet.as_view({'put': 'update'}) request = factory.put(BILLS_URI, data=json.dumps(other_bill_info), content_type='application/json', HTTP_AUTHORIZATION=get_jwt_header( new_user.email)) response = view(request, pk=b1.id) print(response.data) assert response.status_code == 200 assert response.data.get('id') == b1.id assert response.data.get('name') == other_bill_info['name'] assert response.data.get( 'description') == other_bill_info['description'] assert response.data.get('due_date') == other_bill_info['due_date'] service_id = response.data.get('service_id') assert service_id assert Service.objects.get(id=service_id)
def create(self, data): print('Creating a Bill with the following data: ', data) print('request.user: '******'request'].user) service_name = data.get('service_name') service_instance = handle_service_instance(service_name) print('service_instance: ', service_instance) due_date = data.get('due_date') if due_date: if due_date > 31 or due_date < 1: raise ValueError('Due date must be between 1 and 31') else: pass new_bill = Bill.objects.create(name=data.get('name'), description=data.get('description'), due_date=due_date, service=service_instance, user=self.context['request'].user) print('New bill created: ', new_bill) return new_bill
def new_bill(): user = new_user() return Bill.objects.create(name=mixer.faker.genre(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance( mixer.faker.genre()), user=user)
def update(self, bill_instance, data): print('Updating: ', bill_instance) print('Data: ', data) bill_instance.name = data.get('name') bill_instance.description = data.get('description') bill_instance.due_date = data.get('due_date') service_name = data.get('service_name') if service_name: bill_instance.service = handle_service_instance(service_name) bill_instance.save() print('Updated instance: ', bill_instance) return bill_instance
def test_model_create_bill(self, new_user, new_bill_info): new_bill_instance = Bill.objects.create( name=new_bill_info['name'], description=new_bill_info['description'], due_date=new_bill_info['due_date'], service=handle_service_instance(new_bill_info['service_name']), user=new_user ) assert new_bill_instance.id assert new_bill_instance.name == new_bill_info['name'] assert new_bill_instance.description == new_bill_info['description'] assert new_bill_instance.due_date == new_bill_info['due_date'] assert Service.objects.get(name=new_bill_info['service_name'].title()).id assert new_bill_instance.user == new_user
def test_bill_update_by_bad_user(self, new_bill_info, other_bill_info, new_user, other_user): new_bill_instance = Bill.objects.create( name=new_bill_info['name'], description=new_bill_info['description'], due_date=new_bill_info['due_date'], service=handle_service_instance(new_bill_info['service_name']), user=new_user) assert new_bill_instance.id view = BillViewSet.as_view({'put': 'update'}) request = factory.put(BILLS_URI, data=json.dumps(other_bill_info), content_type='application/json', HTTP_AUTHORIZATION=get_jwt_header( other_user.email)) response = view(request, pk=new_bill_instance.id) assert response.status_code == 403
def test_get_other_bills_should_be_forbidden(self, new_user, other_user): b1 = Bill.objects.create(name=mixer.faker.first_name(), description=mixer.faker.text(), due_date=randint(1, 31), service=handle_service_instance( mixer.faker.genre()), user=new_user) assert b1.id assert b1.user == new_user bad_retrieve_view = BillViewSet.as_view({'get': 'retrieve'}) bad_retrieve_request = factory.get( BILLS_URI, HTTP_AUTHORIZATION=get_jwt_header(other_user.email), ) bad_retrieve_response = bad_retrieve_view(bad_retrieve_request, pk=b1.id) assert bad_retrieve_response.status_code == 403