def setUp(self): self.user = UserFactory(username='******', email='*****@*****.**', password='******') self.user.save() create_token(user=self.user) self.customer = CustomerFactory(user=self.user)
def test_add_products_to_a_non_existing_wish_list(self): product = ProductFactory() data = json.dumps({ 'product': { 'id': product.id, 'title': product.title, 'brand': product.brand, 'reviewScore': product.reviewScore, 'price': product.price, 'image': product.image, } }) user = UserFactory(username='******', email='*****@*****.**', password='******') create_token(user=user) token = get_token_from_user(user=user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.post(reverse('wish-list-product-list', kwargs={'version': 'v1'}), data=data, content_type='application/json') self.assertEqual(response.status_code, status.HTTP_412_PRECONDITION_FAILED) response_wish_list_product_data = json.loads(response.content) self.assertEqual(response_wish_list_product_data, 'User has no wish list')
def setUp(self): self.user = UserFactory(username='******', email='*****@*****.**', password='******') create_token(user=self.user) self.product = ProductFactory( title='pro controller', brand='nintendo', price=200.00, image='http://example.com/pro_controller', )
def setUp(self): self.user = UserFactory(username='******', email='*****@*****.**', password='******') create_token(user=self.user) self.wish_list = WishListFactory(user=self.user, name='santa_claws_wish_list') self.product = ProductFactory( title='pro controller', brand='nintendo', reviewScore=8, price=200.00, image='http://example.com/pro_controller', ) self.wish_list_product = WishListProductFactory( wish_list=self.wish_list, product=self.product)
def test_delete_existing_user(self): user = UserFactory(username='******') create_token(user=user) token = get_token_from_user(user=user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) created_user = User.objects.filter(username='******') self.assertEqual(created_user.first().username, user.username) response = self.client.delete(reverse('user-detail', kwargs={ 'version': 'v1', 'pk': user.id }), content_type='application/json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) deleted_user = User.objects.filter(username='******') self.assertFalse(deleted_user.exists())
class TestUserViewSet(APITestCase): client = APIClient() def setUp(self): self.user = UserFactory(username='******', email='*****@*****.**', password='******') self.user.save() create_token(user=self.user) self.customer = CustomerFactory(user=self.user) def test_get_all_users(self): token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.get( reverse('user-list', kwargs={'version': 'v1'})) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['results'][0]['email'], '*****@*****.**') def test_create_user(self): data = json.dumps({ 'email': '*****@*****.**', 'password': '******', 'username': '******', 'customer': { 'name': 'wonder', } }) token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.post(reverse('user-list', kwargs={'version': 'v1'}), data=data, content_type='application/json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) user = User.objects.get(email='*****@*****.**') self.assertEqual(user.customer.name, 'wonder') self.assertEqual(user.email, '*****@*****.**') def test_does_not_add_user_with_the_same_email(self): data = json.dumps({ 'email': '*****@*****.**', 'password': '******', 'username': '******', 'customer': { 'name': 'wonder', } }) token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.post(reverse('user-list', kwargs={'version': 'v1'}), data=data, content_type='application/json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) response_user_data = json.loads(response.content) self.assertEqual( response_user_data['non_field_errors'][0], USER_ALREADY_HAS_THIS_EMAIL_ERROR_MESSAGE.format('*****@*****.**')) def test_update_existing_user(self): data = json.dumps({ 'username': '******', 'password': '******', 'customer': { 'name': 'wonder_updated', } }) token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.put(reverse('user-detail', kwargs={ 'version': 'v1', 'pk': self.user.id }), data=data, content_type='application/json') self.assertEqual(response.status_code, status.HTTP_200_OK) updated_user = User.objects.get(id=self.user.id) self.assertEqual(updated_user.customer.name, 'wonder_updated') def test_delete_existing_user(self): user = UserFactory(username='******') create_token(user=user) token = get_token_from_user(user=user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) created_user = User.objects.filter(username='******') self.assertEqual(created_user.first().username, user.username) response = self.client.delete(reverse('user-detail', kwargs={ 'version': 'v1', 'pk': user.id }), content_type='application/json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) deleted_user = User.objects.filter(username='******') self.assertFalse(deleted_user.exists())
def setUp(self): self.user = UserFactory(username='******', email='*****@*****.**', password='******') create_token(user=self.user) self.product_review = ProductReviewFactory()