def test_update(self): contract = ContractFactory(bank='Informação antiga') self.assertEqual(Contract.objects.first().bank, 'Informação antiga') contract.bank = 'Nova informação' contract.save() self.assertEqual(Contract.objects.first().bank, 'Nova informação')
def test_list_two_users_success(self): user = UserFactory() ContractFactory.create_batch(3, client=user) self.assertEqual(Contract.objects.count(), 3) self.set_user(user) path = self.get_path() response = self.client.get(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(len(response.data.get("results")), 3, msg=response.data) user2 = UserFactory() ContractFactory.create_batch(2, client=user2) self.assertEqual(Contract.objects.count(), 5) self.set_user(user2) path = self.get_path() response = self.client.get(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(len(response.data.get("results")), 2, msg=response.data)
def test_put_success(self): contract = ContractFactory(bank="flooo") self.assertEqual(Contract.objects.count(), 1) self.set_user(contract.client) self.assertEqual(Contract.objects.first().bank, "flooo") data = { "client_id": contract.client.id, "bank": "foo", "amount": 150, "interest_rate": 0, "submission_date": "2019-01-01" } path = self.get_path(id_detail=contract.id) response = self.client.put(path, data=data, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(Contract.objects.count(), 1) self.assertEqual(Contract.objects.first().client, contract.client) self.assertEqual(Contract.objects.first().bank, "foo") self.assertEqual(Contract.objects.first().amount, 150) self.assertEqual(Contract.objects.first().interest_rate, 0) self.assertEqual(Contract.objects.first().submission_date.isoformat(), "2019-01-01")
def test_amount_due(self): contract = ContractFactory(amount=150, interest_rate=0) self.assertIsInstance(contract, Contract) PaymentFactory.create_batch(2, contract=contract, value=10) self.assertEqual(contract.amount_due, 130)
def test_list_admin_success(self): user = UserFactory() ContractFactory.create_batch(3, client=user) self.assertEqual(Contract.objects.count(), 3) admin = UserFactory(is_staff=True, is_superuser=True) self.set_user(admin) path = self.get_path() response = self.client.get(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(len(response.data.get("results")), 3, msg=response.data)
def test_updated_value(self): contract = ContractFactory(amount=150, interest_rate=1, submission_date='2019-04-01') self.assertIsInstance(contract, Contract) self.assertEqual(contract.updated_value, 300) self.assertEqual(contract.amount_due, 300)
def test_get_fail_unauthorized(self): contract = ContractFactory(bank="foo") self.assertEqual(Contract.objects.count(), 1) self.set_user(UserFactory()) path = self.get_path(id_detail=contract.id) response = self.client.get(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, msg=response.data)
def test_delete_success(self): contract = ContractFactory() self.assertEqual(Contract.objects.count(), 1) self.set_user(contract.client) path = self.get_path(id_detail=contract.id) response = self.client.delete(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, msg=response.data) self.assertEqual(Contract.objects.count(), 0)
def test_get_admin_success(self): contract = ContractFactory(bank="foo") self.assertEqual(Contract.objects.count(), 1) self.set_user(UserFactory(is_staff=True, is_superuser=True)) path = self.get_path(id_detail=contract.id) response = self.client.get(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(response.data.get("bank"), "foo")
def test_get_success(self): contract = ContractFactory(bank="foo", ip_address="127.0.0.1") self.assertEqual(Contract.objects.count(), 1) self.set_user(contract.client) path = self.get_path(id_detail=contract.id) response = self.client.get(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(response.data.get("bank"), "foo") self.assertEqual(response.data.get("ip_address"), "127.0.0.1")
def test_list_success(self): contract = ContractFactory() PaymentFactory.create_batch(3, contract=contract) self.assertEqual(Payment.objects.count(), 3) self.set_user(contract.client) path = self.get_path() response = self.client.get(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(len(response.data.get("results")), 3, msg=response.data)
def test_create_fail(self): contract = ContractFactory() self.assertEqual(Payment.objects.count(), 0) self.set_user(contract.client) data = {"contract_id": contract.id, "value": -1} path = self.get_path() response = self.client.post(path, data=data, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, msg=response.data) self.assertEqual(Payment.objects.count(), 0)
def test_put_fail_missing_field(self): contract = ContractFactory(bank="flooo") self.assertEqual(Contract.objects.count(), 1) self.set_user(contract.client) self.assertEqual(Contract.objects.first().bank, "flooo") data = {"bank": "foo"} path = self.get_path(id_detail=contract.id) response = self.client.put(path, data=data, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, msg=response.data) self.assertEqual(Contract.objects.count(), 1) self.assertEqual(Contract.objects.first().client, contract.client) self.assertEqual(Contract.objects.first().bank, "flooo")
def test_patch_success(self): contract = ContractFactory(bank="flooo") self.assertEqual(Contract.objects.count(), 1) self.set_user(contract.client) self.assertEqual(Contract.objects.first().bank, "flooo") data = {"bank": "foo"} path = self.get_path(id_detail=contract.id) response = self.client.patch(path, data=data, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(Contract.objects.count(), 1) self.assertEqual(Contract.objects.first().client, contract.client) self.assertEqual(Contract.objects.first().bank, "foo")
def test_put_fail_unauthorized(self): contract = ContractFactory(bank="flooo") self.assertEqual(Contract.objects.count(), 1) self.set_user(UserFactory()) self.assertEqual(Contract.objects.first().bank, "flooo") data = { "client_id": contract.client.id, "bank": "foo", "amount": 150, "interest_rate": 0, "submission_date": "2019-01-01" } path = self.get_path(id_detail=contract.id) response = self.client.put(path, data=data, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, msg=response.data) self.assertEqual(Contract.objects.first().bank, "flooo")
def test_create_success(self): contract = ContractFactory() self.assertEqual(Payment.objects.count(), 0) self.set_user(contract.client) data = { "contract_id": contract.id, "value": 10.5, "date": "2019-06-06" } path = self.get_path() response = self.client.post(path, data=data, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_201_CREATED, msg=response.data) self.assertEqual(Payment.objects.count(), 1) self.assertEqual(Payment.objects.first().contract, contract) self.assertEqual(Payment.objects.first().value, 10.5) self.assertEqual(Payment.objects.first().date.isoformat(), "2019-06-06")
def test_create(self): ContractFactory() self.assertEqual(Contract.objects.count(), 1)
def test_delete(self): contract = ContractFactory() self.assertEqual(Contract.objects.count(), 1) contract.delete() self.assertEqual(Contract.objects.count(), 0)