Skip to content

Commit

Permalink
✅(back-end) modify dimail-related tests after adding container
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
mjeammet committed Sep 2, 2024
1 parent 7b3d79b commit 16c3f5e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 102 deletions.
34 changes: 0 additions & 34 deletions src/backend/mailbox_manager/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,37 +79,3 @@ class Meta:
)
domain = factory.SubFactory(MailDomainEnabledFactory)
secondary_email = factory.Faker("email")

@classmethod
def _create(cls, model_class, *args, use_mock=True, **kwargs):
domain = kwargs["domain"]
if use_mock and isinstance(domain, models.MailDomain):
with responses.RequestsMock() as rsps:
# Ensure successful response using "responses":
rsps.add(
rsps.GET,
re.compile(r".*/token/"),
body='{"access_token": "domain_owner_token"}',
status=status.HTTP_200_OK,
content_type="application/json",
)
rsps.add(
rsps.POST,
re.compile(
rf".*/domains/{domain.name}/mailboxes/{kwargs['local_part']}"
),
body=str(
{
"email": f"{kwargs['local_part']}@{domain.name}",
"password": "newpass",
"uuid": "uuid",
}
),
status=status.HTTP_201_CREATED,
content_type="application/json",
)

result = super()._create(model_class, *args, **kwargs)
else:
result = super()._create(model_class, *args, **kwargs)
return result
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

def test_api_mailboxes__list_anonymous():
"""Anonymous users should not be allowed to list mailboxes."""
mail_domain = factories.MailDomainEnabledFactory()
mail_domain = factories.MailDomainEnabledFactory(secret="bGFfcmVnaWU6cGFzc3dvcmQ=")
factories.MailboxFactory.create_batch(2, domain=mail_domain)

response = APIClient().get(f"/api/v1.0/mail-domains/{mail_domain.slug}/mailboxes/")
Expand Down
94 changes: 27 additions & 67 deletions src/backend/mailbox_manager/tests/test_models_mailboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,18 @@ def test_models_mailboxes__wrong_secret():

domain = factories.MailDomainEnabledFactory()

with responses.RequestsMock() as rsps:
# Ensure successful response by scim provider using "responses":
rsps.add(
rsps.GET,
re.compile(r".*/token/"),
body='{"detail": "Permission denied"}',
status=status.HTTP_403_FORBIDDEN,
content_type="application/json",
)
rsps.add(
rsps.POST,
re.compile(rf".*/domains/{domain.name}/mailboxes/"),
body='{"detail": "Permission denied"}',
status=status.HTTP_403_FORBIDDEN,
content_type="application/json",
)

with pytest.raises(
exceptions.PermissionDenied,
match=f"Please check secret of the mail domain {domain.name}",
):
mailbox = factories.MailboxFactory(use_mock=False, domain=domain)
# Payload sent to mailbox provider
payload = json.loads(rsps.calls[1].request.body)
assert payload == {
"displayName": f"{mailbox.first_name} {mailbox.last_name}",
"givenName": mailbox.first_name,
"surName": mailbox.last_name,
}
with pytest.raises(
exceptions.PermissionDenied,
match=f"Please check secret of the mail domain {domain.name}",
):
mailbox = factories.MailboxFactory(domain=domain)
# Payload sent to mailbox provider
payload = json.loads(rsps.calls[1].request.body)
assert payload == {
"displayName": f"{mailbox.first_name} {mailbox.last_name}",
"givenName": mailbox.first_name,
"surName": mailbox.last_name,
}


@mock.patch.object(Logger, "error")
Expand All @@ -206,45 +189,22 @@ def test_models_mailboxes__create_mailbox_success(mock_info, mock_error):
factories.MailboxFactory.build(domain=domain)
).data

with responses.RequestsMock() as rsps:
# Ensure successful response using "responses":
rsps.add(
rsps.GET,
re.compile(r".*/token/"),
body='{"access_token": "domain_owner_token"}',
status=status.HTTP_200_OK,
content_type="application/json",
)
rsps.add(
rsps.POST,
re.compile(rf".*/domains/{domain.name}/mailboxes/"),
body=str(
{
"email": f"{mailbox_data['local_part']}@{domain.name}",
"password": "newpass",
"uuid": "uuid",
}
),
status=status.HTTP_201_CREATED,
content_type="application/json",
)

mailbox = factories.MailboxFactory(
use_mock=False, local_part=mailbox_data["local_part"], domain=domain
)

# Check headers
headers = rsps.calls[1].request.headers
# assert "Authorization" not in headers
assert headers["Content-Type"] == "application/json"
mailbox = factories.MailboxFactory(
local_part=mailbox_data["local_part"], domain=domain
)

# Payload sent to mailbox provider
payload = json.loads(rsps.calls[1].request.body)
assert payload == {
"displayName": f"{mailbox.first_name} {mailbox.last_name}",
"givenName": mailbox.first_name,
"surName": mailbox.last_name,
}
# Check headers
headers = rsps.calls[1].request.headers
# assert "Authorization" not in headers
assert headers["Content-Type"] == "application/json"

# Payload sent to mailbox provider
payload = json.loads(rsps.calls[1].request.body)
assert payload == {
"displayName": f"{mailbox.first_name} {mailbox.last_name}",
"givenName": mailbox.first_name,
"surName": mailbox.last_name,
}

# Logger
assert not mock_error.called
Expand Down

0 comments on commit 16c3f5e

Please sign in to comment.