Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
estyxx committed Jan 30, 2024
1 parent 2157bfd commit 83c2aaa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 74 deletions.
6 changes: 6 additions & 0 deletions backend/grants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ class ApprovedType(models.TextChoices):
_("applicant reply deadline"), null=True, blank=True
)
applicant_message = models.TextField(_("applicant message"), null=True, blank=True)
plain_thread_id = models.CharField(
_("Plain threadID"),
max_length=50,
null=True,
blank=True,
)

# Voucher Management
voucher_code = models.TextField(
Expand Down
14 changes: 12 additions & 2 deletions backend/integrations/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings

from users.models import User
from grants.models import Grant

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -144,10 +145,19 @@ def _create_thread(customer_id: str, title: str, message: str):
)

_raise_mutation_error(response, "createThread")
thread_id = response["createThread"]["thread"]["id"]

logger.info("Thread created with id: %s", response["createThread"]["thread"]["id"])
logger.info("Thread created with id: %s", thread_id)
return thread_id


def send_message(user: User, title: str, message: str):
customer_id = create_customer(user)
_create_thread(customer_id, title, message)
thread_id = _create_thread(customer_id, title, message)

try:
grant = Grant.objects.get(user=user)
grant.plain_thread_id = thread_id
grant.save()
except Grant.DoesNotExist:
return None

Check warning on line 163 in backend/integrations/plain.py

View check run for this annotation

Codecov / codecov/patch

backend/integrations/plain.py#L162-L163

Added lines #L162 - L163 were not covered by tests
113 changes: 41 additions & 72 deletions backend/integrations/tests/test_plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from integrations.plain import (
PlainError,
_create_thread,
change_customer_status,
create_customer,
send_message,
)
from users.tests.factories import UserFactory
from grants.tests.factories import GrantFactory

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -66,74 +66,38 @@ def test_create_user_failed(settings, requests_mock):
create_customer(user)


def test_change_customer_status_successful(settings, requests_mock):
def test_create_thread_successful(settings, requests_mock):
settings.PLAIN_API = "https://api.plain.com/graphql/"
requests_mock.post(
settings.PLAIN_API,
json={
"data": {
"changeCustomerStatus": {
"customer": {"id": "c_01GRV5X4BKVSW0YNAXW954VBY6"},
"createThread": {
"thread": {
"__typename": "Thread",
"id": "th_0123456789ABCDEFGHILMNOPQR",
"customer": {"id": "c_0123456789ABCDEFGHILMNOPQR"},
"status": "TODO",
"statusChangedAt": {
"__typename": "DateTime",
"iso8601": "2024-01-30T12:52:21.884Z",
"unixTimestamp": "1706619141884",
},
"title": "Marcotte has some questions about his grant",
"previewText": "Can I have my grant?",
"priority": 2,
},
"error": None,
}
}
},
)

change_customer_status("c_ABC25904A1DA4E0A82934234F2")


def test_change_customer_status_failed(settings, requests_mock):
settings.PLAIN_API = "https://api.plain.com/graphql/"
requests_mock.post(
settings.PLAIN_API,
json={
"data": {
"changeCustomerStatus": {
"customer": None,
"error": {"message": "something went wrong"},
}
}
},
thread_id = _create_thread(
"c_0123456789ABCDEFGHILMNOPQR", title="wtf", message="hello world"
)

with pytest.raises(PlainError, match="something went wrong"):
change_customer_status("c_ABC25904A1DA4E0A82934234F2")


def test_change_customer_status_is_already_active(settings, requests_mock):
settings.PLAIN_API = "https://api.plain.com/graphql/"
requests_mock.post(
settings.PLAIN_API,
json={
"data": {
"changeCustomerStatus": {
"customer": None,
"error": {"message": "Customer already is status: ACTIVE"},
}
}
},
)

change_customer_status("c_ABC25904A1DA4E0A82934234F2")


def test_send_chat_successful(settings, requests_mock):
settings.PLAIN_API = "https://api.plain.com/graphql/"
requests_mock.post(
settings.PLAIN_API,
json={
"data": {
"upsertCustomTimelineEntry": {
"result": "CREATED",
"timelineEntry": {"id": "t_01GRXN6HTBRSERGSGHS60PVGBY"},
"error": None,
}
}
},
)

_create_thread("c_ABC25904A1DA4E0A82934234F2", title="wtf", message="hello world")
assert thread_id == "th_0123456789ABCDEFGHILMNOPQR"


def test_create_thread_failed(settings, requests_mock):
Expand All @@ -142,9 +106,7 @@ def test_create_thread_failed(settings, requests_mock):
settings.PLAIN_API,
json={
"data": {
"upsertCustomTimelineEntry": {
"result": "NOOP",
"timelineEntry": None,
"createThread": {
"error": {
"message": "There was a validation error.",
"type": "VALIDATION",
Expand Down Expand Up @@ -193,19 +155,21 @@ def test_send_message(settings, requests_mock):
{
"json": {
"data": {
"changeCustomerStatus": {
"customer": {"id": "c_01GRV5X4BKVSW0YNAXW954VBY6"},
"error": None,
}
}
}
},
{
"json": {
"data": {
"upsertCustomTimelineEntry": {
"result": "CREATED",
"timelineEntry": {"id": "t_01GRXN6HTBRSERGSGHS60PVGBY"},
"createThread": {
"thread": {
"__typename": "Thread",
"id": "th_0123456789ABCDEFGHILMNOPQR",
"customer": {"id": "c_0123456789ABCDEFGHILMNOPQR"},
"status": "TODO",
"statusChangedAt": {
"__typename": "DateTime",
"iso8601": "2024-01-30T12:52:21.884Z",
"unixTimestamp": "1706619141884",
},
"title": "Marcotte has some questions about his grant",
"previewText": "Can I have my grant?",
"priority": 2,
},
"error": None,
}
}
Expand All @@ -220,8 +184,13 @@ def test_send_message(settings, requests_mock):
email="ester@example.com",
username="",
)
grant = GrantFactory(user=user)

send_message(
user,
title="User has replied",
message="Hello World!",
)

grant.refresh_from_db()
assert grant.plain_thread_id == "th_0123456789ABCDEFGHILMNOPQR"

0 comments on commit 83c2aaa

Please sign in to comment.