Ejemplo n.º 1
0
    def form_valid(self, form):
        # name field required 
        try:
            self.request.POST["teams_team_name"]
        except ObjectDoesNotExist:
            messages.add_message(
                    self.request,
                    messages.ERROR,
                    "ERROR : team name form empty"
                    )
            return HttpResponseRedirect(reverse("maketeam"))           
        else:
            name = self.request.POST["teams_team_name"]

        # valid and unique team name test
        try:        
            team = Team.objects.create_team(name)
        except ValidationError:
            messages.add_message(
                    self.request,
                    messages.ERROR,
                    "ERROR : invalid team name"
                    )
            return HttpResponseRedirect(reverse("maketeam"))          
        except IntegrityError:
            messages.add_message(
                    self.request, 
                    messages.ERROR, 
                    "ERROR : The team name already exists"
                    )
            return HttpResponseRedirect(reverse("maketeam"))
        else:
            # link user to team using Membership  
            email = self.request.user
            user = User.objects.get_user(email)
            membership = Membership()
            membership = Membership(team = team, member = user, is_admin = True)
            membership.save()
 
            messages.add_message(
                    self.request, 
                    messages.SUCCESS, 
                    "Team created successfully"
                    )
            return HttpResponseRedirect(reverse("home"))
Ejemplo n.º 2
0
class TestAPITeamUserList(TestCase):
    def setUp(self):
        """
        * test scenario
        - 1 team
        - 2 users belongs to team ( A, B )
        - 1 user not belongs to team ( C )

        request teamuser-list API
        - response should contain A, B users info
        - response should not contain C user info
        """
        self.team = Team.objects.create_team(
            name="test_teamname"
        )
        self.first_user_belongs_to_team = User.objects.create_user(
            email="*****@*****.**",
            password="******",
        )
        self.second_user_belongs_to_team = User.objects.create_user(
            email="*****@*****.**",
            password="******",
        )
        self.user_not_belongs_to_team = User.objects.create_user(
            email="*****@*****.**",
            password="******",
        )

        """
        * should implement later using some eloquent method.
        there is no method for make relation between user and team
        in this test case, going to use Membership Model directly.
        """

        self.first_membership = Membership()
        self.first_membership.team = self.team
        self.first_membership.member = self.first_user_belongs_to_team
        self.first_membership.save()

        self.second_membership = Membership()
        self.second_membership.team = self.team
        self.second_membership.member = self.second_user_belongs_to_team
        self.second_membership.save()

        self.client = Client()
        self.response = self.client.get("/api/teams/" + self.team.name + "/users/")

        self.not_exist_teamname = "test_teamname_not_exist"
        self.response_with_not_exist_teamname = \
            self.client.get("/api/teams/" + self.not_exist_teamname + "/users/")

    def test_api_teamuser_list_request_should_return_200(self):
        self.assertEqual(self.response.status_code, 200)

    def test_api_teamuser_list_request_with_not_exist_teamname_should_return_404(self):
        self.assertEqual(self.response_with_not_exist_teamname.status_code, 404)

    def test_api_teamuser_list_request_should_return_json(self):
        self.assertEqual(self.response["Content-Type"], "application/json")

    def test_api_teamuser_list_request_should_contain_users_belong_to_team(self):
        self.assertContains(self.response, self.first_user_belongs_to_team.email)
        self.assertContains(self.response, self.second_user_belongs_to_team.email)

    def test_api_teamuser_list_request_should_not_contain_user_not_belong_to_team(self):
        self.assertNotContains(self.response, self.user_not_belongs_to_team.email)
class TestAPITeamUserListSearch(TestCase):
    """
    * Test Scenario

    - 2 teams ( first_team, second_team )
    - we are going to search for first_team
    ( /api/teams/first_team_name/users/?search={query} )

    - 3 users
        - A : user_in_first_team_with_test_string
        - B : user_in_first_team_without_test_string
        - C : user_in_second_team_with_test_string

    - search result should ...
        - contain A
        - NOT contain B
        - NOT contain C
    """
    def setUp(self):
        # Generate Teams
        self.first_team = Team.objects.create_team(
            name="test_teamname_first",
        )
        self.second_team = Team.objects.create_team(
            name="test_teamname_second",
        )

        self.test_string = "test_string"

        # Generate Users
        self.user_in_first_team_with_test_string = User.objects.create_user(
            email="test_username_with" + self.test_string + "@firstteam.com",
            password="******",
        )
        self.user_in_first_team_without_test_string = User.objects.create_user(
            email="test_username_without" + "@firstteam.com",
            password="******",
        )
        self.user_in_second_team_with_test_string = User.objects.create_user(
            email="test_username_with_" + self.test_string + "@secondteam.com",
            password="******",
        )

        # Generate Membership
        self.first_membership = Membership()
        self.first_membership.team = self.first_team
        self.first_membership.member = self.user_in_first_team_with_test_string
        self.first_membership.save()

        self.second_membership = Membership()
        self.second_membership.team = self.first_team
        self.second_membership.member = self.user_in_first_team_without_test_string
        self.second_membership.save()

        self.third_membership = Membership()
        self.third_membership.team = self.second_team
        self.third_membership.member = self.user_in_second_team_with_test_string
        self.third_membership.save()

        # Get Response
        self.client = Client()
        self.response = self.client.get("/api/teams/" + self.first_team.name + "/users/?search=" + self.test_string)

    def test_api_teamuser_list_search_should_return_valid_result(self):
        self.assertContains(self.response, self.user_in_first_team_with_test_string.email)
        self.assertNotContains(self.response, self.user_in_first_team_without_test_string.email)
        self.assertNotContains(self.response, self.user_in_second_team_with_test_string.email)