Example #1
0
    def test_ambassador_cant_see_other_ambassadors_projects(self):
        """
        Test that one ambassador cannot see the projects on the dashboard created by
        another ambassador.

        Creates two ambassador users, creates a project for each of them, then checks
        that the dashboard for each of them does not contain a reference to the other's
        project.
        """
        amb1_user = User.objects.create_user(username="******",
                                             password="******")
        amb2_user = User.objects.create_user(username="******",
                                             password="******")
        amb1 = get_profile(amb1_user)
        amb2 = get_profile(amb2_user)
        amb1.make_ambassador()
        amb2.make_ambassador()
        project1 = Project.factories.drafted.create(ambassador=amb1)
        project2 = Project.factories.drafted.create(ambassador=amb2)

        self.send_user_login_request(amb1_user, "amb1_pass", webtest=True)
        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        self.assert_in_response_html(dash_resp, "project-%i" % project1.pk)
        self.assert_not_in_response_html(dash_resp, "project-%i" % project2.pk)

        self.app.get("/logout/")
        self.send_user_login_request(amb2_user, "amb2_pass", webtest=True)
        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        self.assert_in_response_html(dash_resp, "project-%i" % project2.pk)
        self.assert_not_in_response_html(dash_resp, "project-%i" % project1.pk)
Example #2
0
    def test_ambassador_cant_see_other_ambassadors_projects(self):
        """
        Test that one ambassador cannot see the projects on the dashboard created by
        another ambassador.

        Creates two ambassador users, creates a project for each of them, then checks
        that the dashboard for each of them does not contain a reference to the other's
        project.
        """
        amb1_user = User.objects.create_user(username="******", password="******")
        amb2_user = User.objects.create_user(username="******", password="******")
        amb1 = get_profile(amb1_user)
        amb2 = get_profile(amb2_user)
        amb1.make_ambassador()
        amb2.make_ambassador()
        project1 = Project.factories.drafted.create(ambassador=amb1)
        project2 = Project.factories.drafted.create(ambassador=amb2)

        self.send_user_login_request(amb1_user, "amb1_pass", webtest=True)
        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        self.assert_in_response_html(dash_resp, "project-%i" % project1.pk)
        self.assert_not_in_response_html(dash_resp, "project-%i" % project2.pk)

        self.app.get("/logout/")
        self.send_user_login_request(amb2_user, "amb2_pass", webtest=True)
        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        self.assert_in_response_html(dash_resp, "project-%i" % project2.pk)
        self.assert_not_in_response_html(dash_resp, "project-%i" % project1.pk)
Example #3
0
    def test_admin_can_approve_unapprove_or_deny_proposed_project(self):
        """
        Test that an admin can approve, unapprove, and deny proposed projects from the dashboard.

        Creates two projects, both proposed, and tests that the admin can use the buttons
        on the dashboard to approve/unapprove and deny, respectively, each one. Then, tests that
        the ambassador that created the projects can see them in their resulting state.
        """
        amb_user = User.objects.create_user(username="******",
                                            password="******")
        admin_user = User.objects.create_user(username="******",
                                              password="******")
        ambassador = get_profile(amb_user)
        admin = get_profile(admin_user)
        ambassador.make_ambassador()
        admin.make_administrator()
        project1 = Project.factories.proposed.create(ambassador=ambassador)
        project2 = Project.factories.proposed.create(ambassador=ambassador)

        self.send_user_login_request(admin_user, "admin_pass", webtest=True)
        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        staged_resp = dash_resp.forms["stage_deny_form_%i" %
                                      project1.pk].submit(
                                          "_stage").maybe_follow()
        self.assertEqual(staged_resp.status_code, 200)
        self.assertEqual(
            Project.objects.get(id=project1.pk).project_status, Project.STAGED)

        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        approved_resp = dash_resp.forms[
            "approve_form_%i" % project1.pk].submit("_approve").maybe_follow()
        self.assertEqual(approved_resp.status_code, 200)
        self.assertEqual(
            Project.objects.get(id=project1.pk).project_status, Project.ACTIVE)

        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        unapproved_resp = dash_resp.forms["complete_form_%i" %
                                          project1.pk].submit(
                                              "_unapprove").maybe_follow()
        self.assertEqual(unapproved_resp.status_code, 200)
        self.assertEqual(
            Project.objects.get(id=project1.pk).project_status, Project.STAGED)

        denied_resp = dash_resp.forms[
            "stage_deny_form_%i" % project2.pk].submit("_deny").maybe_follow()
        self.assertEqual(denied_resp.status_code, 200)
        self.assertEqual(
            Project.objects.get(id=project2.pk).project_status,
            Project.DRAFTED)

        self.app.get("/logout/")
        self.send_user_login_request(amb_user, "amb_pass", webtest=True)
        amb_dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        self.assert_in_response_html(
            amb_dash_resp,
            "project-status-%i-%s" % (project1.pk, Project.STAGED))
        self.assert_in_response_html(
            amb_dash_resp,
            "project-status-%i-%s" % (project2.pk, Project.DRAFTED))
Example #4
0
 def create_user(self, *args, **kwargs):
     """
     For purposes of testing and DRYness, it is often useful to create
     a user and return the associated RevolvUserProfile.
     """
     user = User.objects.create_user(*args, **kwargs)
     return get_profile(user)
Example #5
0
 def create_user(self, *args, **kwargs):
     """
     For purposes of testing and DRYness, it is often useful to create
     a user and return the associated RevolvUserProfile.
     """
     user = User.objects.create_user(*args, **kwargs)
     return get_profile(user)
Example #6
0
 def setUp(self):
     """Every test in this mixin has a test user."""
     self.test_user = User.objects.create_user(
         "John",
         "*****@*****.**",
         "test_user_password"
     )
     self.test_profile = get_profile(self.test_user)
Example #7
0
 def create_new_user_with_password(self, username, password, ambassador=False, admin=False):
     """Create a new user with the given credentials and return their user object and profile."""
     user = User.objects.create_user(username=username, password=password)
     profile = get_profile(user)
     if ambassador:
         profile.make_ambassador()
     if admin:
         profile.make_administrator()
     return user, profile
Example #8
0
    def test_admin_can_approve_unapprove_or_deny_proposed_project(self):
        """
        Test that an admin can approve, unapprove, and deny proposed projects from the dashboard.

        Creates two projects, both proposed, and tests that the admin can use the buttons
        on the dashboard to approve/unapprove and deny, respectively, each one. Then, tests that
        the ambassador that created the projects can see them in their resulting state.
        """
        amb_user = User.objects.create_user(username="******", password="******")
        admin_user = User.objects.create_user(username="******", password="******")
        ambassador = get_profile(amb_user)
        admin = get_profile(admin_user)
        ambassador.make_ambassador()
        admin.make_administrator()
        project1 = Project.factories.proposed.create(ambassador=ambassador)
        project2 = Project.factories.proposed.create(ambassador=ambassador)

        self.send_user_login_request(admin_user, "admin_pass", webtest=True)
        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        staged_resp = dash_resp.forms["stage_deny_form_%i" % project1.pk].submit("_stage").maybe_follow()
        self.assertEqual(staged_resp.status_code, 200)
        self.assertEqual(Project.objects.get(id=project1.pk).project_status, Project.STAGED)

        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        approved_resp = dash_resp.forms["approve_form_%i" % project1.pk].submit("_approve").maybe_follow()
        self.assertEqual(approved_resp.status_code, 200)
        self.assertEqual(Project.objects.get(id=project1.pk).project_status, Project.ACTIVE)

        dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        unapproved_resp = dash_resp.forms["complete_form_%i" % project1.pk].submit("_unapprove").maybe_follow()
        self.assertEqual(unapproved_resp.status_code, 200)
        self.assertEqual(Project.objects.get(id=project1.pk).project_status, Project.STAGED)

        denied_resp = dash_resp.forms["stage_deny_form_%i" % project2.pk].submit("_deny").maybe_follow()
        self.assertEqual(denied_resp.status_code, 200)
        self.assertEqual(Project.objects.get(id=project2.pk).project_status, Project.DRAFTED)

        self.app.get("/logout/")
        self.send_user_login_request(amb_user, "amb_pass", webtest=True)
        amb_dash_resp = self.app.get("/my-portfolio/").maybe_follow()
        self.assert_in_response_html(amb_dash_resp, "project-status-%i-%s" % (project1.pk, Project.STAGED))
        self.assert_in_response_html(amb_dash_resp, "project-status-%i-%s" % (project2.pk, Project.DRAFTED))
Example #9
0
    def test_post_updates_for_active_and_completed_projects(self):
        """
        Test that project updates can be added by an admin user and the ambassador that
        created the project, both when the project is active and completed.
        """
        amb_user = User.objects.create_user(username="******", password="******")
        amb = get_profile(amb_user)
        amb.make_ambassador()
        admin_user = User.objects.create_user(username="******", password="******")
        admin = get_profile(admin_user)
        admin.make_administrator()
        project = Project.factories.active.create(ambassador=amb)

        self.assert_can_post_update_for_project(amb_user, "amb_pass", project)
        self.assert_can_post_update_for_project(admin_user, "admin_pass", project)

        project.complete_project()

        self.assert_can_post_update_for_project(amb_user, "amb_pass", project)
        self.assert_can_post_update_for_project(admin_user, "admin_pass", project)
Example #10
0
    def test_post_updates_for_active_and_completed_projects(self):
        """
        Test that project updates can be added by an admin user and the ambassador that
        created the project, both when the project is active and completed.
        """
        amb_user = User.objects.create_user(username="******", password="******")
        amb = get_profile(amb_user)
        amb.make_ambassador()
        admin_user = User.objects.create_user(username="******", password="******")
        admin = get_profile(admin_user)
        admin.make_administrator()
        project = Project.factories.active.create(ambassador=amb)

        self.assert_can_post_update_for_project(amb_user, "amb_pass", project)
        self.assert_can_post_update_for_project(admin_user, "admin_pass", project)

        project.complete_project()

        self.assert_can_post_update_for_project(amb_user, "amb_pass", project)
        self.assert_can_post_update_for_project(admin_user, "admin_pass", project)
Example #11
0
def is_ambassador(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
    """
    Decorator for views that checks that the user is logged in and an ambassador, redirecting
    to the log-in page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_authenticated() and get_profile(u).is_ambassador(),
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if function:
        return actual_decorator(function)
    return actual_decorator
Example #12
0
 def create_new_user_with_password(self,
                                   username,
                                   password,
                                   ambassador=False,
                                   admin=False):
     """Create a new user with the given credentials and return their user object and profile."""
     user = User.objects.create_user(username=username, password=password)
     profile = get_profile(user)
     if ambassador:
         profile.make_ambassador()
     if admin:
         profile.make_administrator()
     return user, profile
Example #13
0
def is_ambassador(function=None,
                  redirect_field_name=REDIRECT_FIELD_NAME,
                  login_url=None):
    """
    Decorator for views that checks that the user is logged in and an ambassador, redirecting
    to the log-in page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_authenticated() and get_profile(u).is_ambassador(),
        login_url=login_url,
        redirect_field_name=redirect_field_name)
    if function:
        return actual_decorator(function)
    return actual_decorator
Example #14
0
 def bust_test_user_cache(self):
     """Make sure that the user and profile are up to date (avoid db caching)."""
     self.test_user = User.objects.get(username=self.test_user.username)
     self.test_profile = get_profile(self.test_user)
Example #15
0
 def setUp(self):
     """Every test in this mixin has a test user."""
     self.test_user = User.objects.create_user("John", "*****@*****.**",
                                               "test_user_password")
     self.test_profile = get_profile(self.test_user)
Example #16
0
 def bust_test_user_cache(self):
     """Make sure that the user and profile are up to date (avoid db caching)."""
     self.test_user = User.objects.get(username=self.test_user.username)
     self.test_profile = get_profile(self.test_user)