def test_update_customer(self, user, mock_storage): # make sure user is a MANAGER of customer # (so they have permission for the view) customer = CustomerFactory(logo=None) (customer_user, _) = customer.add_user(user, type="MANAGER", status="ACTIVE") _, key = create_auth_token(user) client = APIClient() client.credentials(HTTP_AUTHORIZATION=f"Token {key}") url = reverse("customers-detail", args=[customer.id]) response = client.get(url, format="json") content = response.json() new_name = shuffle_string(content["name"]).strip() content["name"] = new_name response = client.put(url, content, format="json") assert status.is_success(response.status_code) customer.refresh_from_db() assert customer.name == new_name # in addition to making the change, # the view should also notify the user assert len(mail.outbox) == 1 message = mail.outbox[0] assert "Update on your customer" in message.subject
def test_add_new_customer_user(self, user_data, mock_storage): customer = CustomerFactory(logo=None) customer_user, _ = customer.add_user(UserFactory(avatar=None), type="MANAGER", status="ACTIVE") _, key = create_auth_token(customer_user.user) client = APIClient() client.credentials(HTTP_AUTHORIZATION=f"Token {key}") url = reverse("customer-users-list", args=[customer.id]) data = { # no need to specify type/status - they have default values "customer": customer.name, "user": user_data, } response = client.post(url, data, format="json") content = response.json() assert status.is_success(response.status_code) customer.refresh_from_db() user = User.objects.get(email=user_data["email"]) assert customer.customer_users.count() == 2 assert customer.customer_users.filter( id__in=user.customer_users.all()).exists() assert user.change_password is True assert user.accepted_terms is False assert user.email == content["user"]["email"]
def test_add_new_invalid_customer_user(self, user_data, mock_storage): user_data["email"] = "invalid_email_address" customer = CustomerFactory(logo=None) customer_user, _ = customer.add_user(UserFactory(avatar=None), type="MANAGER", status="ACTIVE") _, key = create_auth_token(customer_user.user) client = APIClient() client.credentials(HTTP_AUTHORIZATION=f"Token {key}") url = reverse("customer-users-list", args=[customer.id]) data = { # no need to specify type/status - they have default values "customer": customer.name, "user": user_data, } response = client.post(url, data, format="json") content = response.json() customer.refresh_from_db() assert status.is_client_error(response.status_code) assert content["user"] == {"email": ["Enter a valid email address."]} assert customer.customer_users.count() != 2
def test_add_existing_customer_user(self, mock_storage): customer = CustomerFactory(logo=None) # the user making the request must be a manager customer_user, _ = customer.add_user(UserFactory(avatar=None), type="MANAGER", status="ACTIVE") client = APIClient() _, key = create_auth_token(customer_user.user) client.credentials(HTTP_AUTHORIZATION=f"Token {key}") test_user = UserFactory(avatar=None) url = reverse("customer-users-list", args=[customer.id]) data = { # no need to specify type/status - they have default values "customer": customer.name, "user": UserSerializerBasic(test_user).data, } response = client.post(url, data, format="json") assert status.is_success(response.status_code) customer.refresh_from_db() test_user.refresh_from_db() assert customer.customer_users.count() == 2 assert test_user.customer_users.count() == 1 assert customer.customer_users.filter( id__in=test_user.customer_users.all()).exists() assert test_user.change_password is not True