def test_valid(self): """Two users with no existing invites between them are valid.""" from_user = UserFactory() to_user = UserFactory() data = {"from_user": str(from_user.id), "to_user": str(to_user.id)} form = SendInviteForm(data=data) is_valid = form.is_valid() assert is_valid
def test_max_connections_accepted_invite(self, mock_users_constants): """An accepted invite does not count against the max connections total.""" mock_users_constants.MAX_USER_CONNECTIONS = 1 from_user = UserFactory() to_user = UserFactory() InviteFactory(from_user=from_user, status=Invite.InviteStatus.ACCEPTED) data = {"from_user": str(from_user.id), "to_user": str(to_user.id)} form = SendInviteForm(data=data) is_valid = form.is_valid() assert is_valid
def test_valid_save(self): """A valid form creates an invite.""" from_user = UserFactory() to_user = UserFactory() data = {"from_user": str(from_user.id), "to_user": str(to_user.id)} form = SendInviteForm(data=data) form.is_valid() form.save() assert (Invite.objects.filter( from_user=from_user, to_user=to_user, status=Invite.InviteStatus.PENDING).count() == 1)
def test_max_connections(self, mock_users_constants): """A user may not send any invites when they are at their max connections.""" mock_users_constants.MAX_USER_CONNECTIONS = 1 from_user = UserFactory() to_user = UserFactory() InviteFactory(from_user=from_user) data = {"from_user": str(from_user.id), "to_user": str(to_user.id)} form = SendInviteForm(data=data) is_valid = form.is_valid() assert not is_valid assert ("You are at your maximum number of connections." in form.non_field_errors())
def test_authenticated(self, user: User, rf: RequestFactory): request = rf.get("/fake-url/") request.user = UserFactory() response = user_detail_view(request, username=user.username) assert response.status_code == 200
def test_clean_username(self): # A user with proto_user params does not exist yet. proto_user = UserFactory.build() form = UserCreationForm({ "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, }) assert form.is_valid() assert form.clean_username() == proto_user.username # Creating a user. form.save() # The user with proto_user params already exists, # hence cannot be created. form = UserCreationForm({ "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, }) assert not form.is_valid() assert len(form.errors) == 1 assert "username" in form.errors
def test_get_ok(self): user = self.make_user() to_user = UserFactory() with self.login(user): self.get_check_200("invites:send_invite", to_user.username) form = self.get_context("form") assert form.is_valid() assert self.get_context("to_user") == to_user
def test_ok_other(self): """The other user is in the context with a different name.""" user = self.make_user() other_user = UserFactory() with self.login(user): self.get_check_200("users:detail", other_user.username) assert self.get_context("user") == user assert self.get_context("viewed_user") == other_user
def test_in_flight_invites(self, mock_constants): """The user can't submit more than the max in flight.""" mock_constants.MAX_IN_FLIGHT_INVITES = 1 from_user = self.make_user() to_user = UserFactory() InviteFactory(from_user=from_user) data = {"from_user": str(from_user.id), "to_user": str(to_user.id)} form = SendInviteForm(data=data) is_valid = form.is_valid() assert not is_valid assert ("You have too many sent invitations out currently." in form.non_field_errors())
def test_post_ok(self): """A successful post creates an invite.""" user = self.make_user() to_user = UserFactory() data = {"from_user": str(user.id), "to_user": str(to_user.id)} with self.login(user): response = self.post("invites:send_invite", to_user.username, data=data) assert Invite.objects.count() == 1 assert response.status_code == 302 assert response["Location"] == self.reverse("core:index") message = list(get_messages(response.wsgi_request))[0] assert str(message) == f"Invite sent to {to_user.username}."
def user() -> User: return UserFactory()
def test_case_sensitivity(self, rf: RequestFactory): request = rf.get("/fake-url/") request.user = UserFactory(username="******") with pytest.raises(Http404): user_detail_view(request, username="******")