Skip to content

Instantly share code, notes, and snippets.

@4heck
Last active March 16, 2021 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4heck/dc41679c3aaf1f041db822ef41ca9128 to your computer and use it in GitHub Desktop.
Save 4heck/dc41679c3aaf1f041db822ef41ca9128 to your computer and use it in GitHub Desktop.
Mircod Internship // Task #5
from django.db import models
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from stdimage import StdImageField
from core.models.base import BaseModel
class GoalCategory(BaseModel):
title = models.CharField(max_length=255)
image = models.ImageField(upload_to="goal_categories")
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "goal categories"
class Goal(BaseModel):
title = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
image = StdImageField(upload_to="goal_images", variations={"thumbnail": {"width": 100, "height": 75}})
max_number_of_members = models.PositiveSmallIntegerField()
category = models.ForeignKey("GoalCategory", on_delete=models.CASCADE, null=True)
def __str__(self):
return self.title
class GoalParty(BaseModel):
goal = models.ForeignKey("Goal", on_delete=models.CASCADE)
admin = models.ForeignKey("User", on_delete=models.CASCADE, related_name="admin_of_goal_party")
members = models.ManyToManyField("User", blank=True)
def __str__(self):
return f"{_('Party for goal')} {self.goal}"
class Meta:
verbose_name_plural = _("goal parties")
class GoalPartyRequest(BaseModel):
goal = models.ForeignKey("Goal", on_delete=models.CASCADE)
user = models.ForeignKey("User", on_delete=models.CASCADE)
def __str__(self):
return f"{_('Request to party')} {self.goal} {_('from')} {self.user}"
import random
import pytest
from rest_framework import status
from core.models.goal import GoalCategory, GoalPartyRequest, GoalParty
from core.services.goal import (
YOU_HAVE_ALREADY_SUBMITTED_A_REQUEST_TO_JOIN_THIS_GOAL,
YOU_ARE_ALREADY_ATTENDING_A_PARTY_FOR_THIS_GOAL,
)
from core.tests.factories.goal_factory import GoalFactory, GoalCategoryFactory
from core.tests.factories.user_factory import UserFactory
@pytest.mark.django_db
def test_get_goal_list(api_client):
goals_count = random.randint(1, 10)
[GoalFactory.create() for _ in range(0, goals_count)]
r = api_client.get(path="/api/goal/")
assert r.status_code == status.HTTP_200_OK
assert len(r.json()) == goals_count
goal_category_pk = random.choice(GoalCategory.objects.all()).pk
r = api_client.get(path=f"/api/goal/?category_id={goal_category_pk}")
for goal in r.json():
assert goal["category"]["id"] == goal_category_pk
@pytest.mark.django_db
def test_get_goal_category_list(api_client):
goals_category_count = random.randint(1, 10)
[GoalCategoryFactory.create() for _ in range(0, goals_category_count)]
r = api_client.get(path="/api/goal/categories/")
assert r.status_code == status.HTTP_200_OK
assert len(r.json()) == goals_category_count
assert r.json()[0]["id"] and r.json()[0]["title"] and r.json()[0]["image"]
@pytest.mark.django_db
def test_get_goal_not_authenticated_user(api_client):
goal = GoalFactory.create()
r = api_client.get(path=f"/api/goal/{goal.pk}/")
assert r.status_code == status.HTTP_200_OK
assert r.json()["id"] == goal.pk
assert not r.json()["my_goal_party"]
@pytest.mark.django_db
def test_get_another_goal_authenticated_user(api_client):
user = UserFactory.create()
goal_party = GoalParty.objects.create(goal=GoalFactory.create(), admin=UserFactory.create())
goal_party.members.add(user)
api_client.force_authenticate(user=user)
r = api_client.get(path=f"/api/goal/{GoalFactory.create().pk}/")
assert r.status_code == status.HTTP_200_OK
assert not r.json()["my_goal_party"]
@pytest.mark.django_db
def test_get_goal_authenticated_admin_user(api_client):
goal = GoalFactory.create()
user = UserFactory.create()
goal_party = GoalParty.objects.create(goal=goal, admin=user)
api_client.force_authenticate(user=user)
r = api_client.get(path=f"/api/goal/{goal.pk}/")
assert r.status_code == status.HTTP_200_OK
r_data = r.json()
assert r_data["my_goal_party"]["id"] == goal_party.pk
assert r_data["id"] == goal.pk
@pytest.mark.django_db
def test_get_goal_authenticated_member_user(api_client):
user = UserFactory.create()
goal = GoalFactory.create()
goal_party = GoalParty.objects.create(goal=goal, admin=UserFactory.create())
goal_party.members.add(user)
api_client.force_authenticate(user=user)
r = api_client.get(path=f"/api/goal/{goal.pk}/")
assert r.status_code == status.HTTP_200_OK
r_data = r.json()
assert r_data["my_goal_party"]["id"] == goal_party.pk
assert r_data["id"] == goal.pk
@pytest.mark.django_db
def test_join_goal_success(api_client):
goal = GoalFactory.create()
user = UserFactory.create()
api_client.force_authenticate(user=user)
r = api_client.post(path=f"/api/goal/{goal.pk}/join/")
assert r.status_code == status.HTTP_200_OK
assert r.json()["goal"]["id"] == goal.pk
assert r.json()["user"]["id"] == user.pk
assert GoalPartyRequest.objects.get(goal=goal, user=user)
@pytest.mark.django_db
def test_join_goal_fail(api_client):
# if the user is not authenticated
goal = GoalFactory.create()
r = api_client.post(path=f"/api/goal/{goal.pk}/join/")
assert r.status_code == status.HTTP_401_UNAUTHORIZED
# if the user has already sent a request for joining this goal
user = UserFactory.create()
GoalPartyRequest.objects.create(goal=goal, user=user)
api_client.force_authenticate(user=user)
r = api_client.post(path=f"/api/goal/{goal.pk}/join/")
assert r.status_code == status.HTTP_400_BAD_REQUEST
assert r.json()[0] == YOU_HAVE_ALREADY_SUBMITTED_A_REQUEST_TO_JOIN_THIS_GOAL
# if the user is a member of the goal
goal = GoalFactory.create()
goal_party = GoalParty.objects.create(goal=goal, admin=UserFactory.create())
goal_party.members.add(user)
r = api_client.post(path=f"/api/goal/{goal.pk}/join/")
assert r.status_code == status.HTTP_400_BAD_REQUEST
assert r.json()[0] == YOU_ARE_ALREADY_ATTENDING_A_PARTY_FOR_THIS_GOAL
# if the user is a goal admin
goal = GoalFactory.create()
GoalParty.objects.create(goal=goal, admin=user)
r = api_client.post(path=f"/api/goal/{goal.pk}/join/")
assert r.status_code == status.HTTP_400_BAD_REQUEST
assert r.json()[0] == YOU_ARE_ALREADY_ATTENDING_A_PARTY_FOR_THIS_GOAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment