Exemplo n.º 1
0
class TestMyProfileAPIView(BaseTenantTestCase):
    def setUp(self):
        super(TestMyProfileAPIView, self).setUp()
        self.unicef_staff = UserFactory(is_staff=True)
        self.unicef_superuser = UserFactory(is_superuser=True)
        self.url = reverse("users_v3:myprofile-detail")

    def test_get(self):
        response = self.forced_auth_req(
            "get",
            self.url,
            user=self.unicef_staff,
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["name"],
                         self.unicef_staff.get_full_name())
        self.assertEqual(response.data["is_superuser"], "False")

    def test_get_no_profile(self):
        """Ensure profile is created for user, if it does not exist"""
        user = UserFactory()
        UserProfile.objects.get(user=user).delete()
        self.assertFalse(UserProfile.objects.filter(user=user).exists())
        response = self.forced_auth_req(
            "get",
            self.url,
            user=self.unicef_staff,
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["name"], user.get_full_name())
        self.assertFalse(UserProfile.objects.filter(user=user).exists())

    def test_patch(self):
        self.assertNotEqual(self.unicef_staff.profile.oic,
                            self.unicef_superuser)
        data = {
            "oic": self.unicef_superuser.id,
        }
        response = self.forced_auth_req('patch',
                                        self.url,
                                        user=self.unicef_staff,
                                        data=data)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["oic"], self.unicef_superuser.id)
        self.assertEqual(response.data["is_superuser"], "False")

        response = self.forced_auth_req(
            'get',
            self.url,
            user=self.unicef_staff,
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["oic"], self.unicef_superuser.id)
        self.assertEqual(response.data["is_superuser"], "False")
Exemplo n.º 2
0
 def test_get_no_profile(self):
     """Ensure profile is created for user, if it does not exist"""
     user = UserFactory()
     UserProfile.objects.get(user=user).delete()
     self.assertFalse(UserProfile.objects.filter(user=user).exists())
     response = self.forced_auth_req(
         "get",
         self.url,
         user=self.unicef_staff,
     )
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data["name"], user.get_full_name())
     self.assertFalse(UserProfile.objects.filter(user=user).exists())
Exemplo n.º 3
0
class TestUsersListAPIView(BaseTenantTestCase):
    def setUp(self):
        self.unicef_staff = UserFactory(is_staff=True)
        self.unicef_superuser = UserFactory(is_superuser=True)
        self.partnership_manager_user = UserFactory(is_staff=True)
        self.group = GroupFactory()
        self.partnership_manager_user.groups.add(self.group)
        self.url = reverse("users_v3:users-list")

    def test_api_users_list(self):
        response = self.forced_auth_req('get',
                                        self.url,
                                        user=self.unicef_staff)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 2)

    def test_users_api_list_values(self):
        response = self.forced_auth_req(
            'get',
            self.url,
            user=self.unicef_staff,
            data={
                "values":
                "{},{}".format(self.partnership_manager_user.id,
                               self.unicef_superuser.id)
            })
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 1)

    def test_api_users_list_values_bad(self):
        response = self.forced_auth_req('get',
                                        self.url,
                                        user=self.unicef_staff,
                                        data={"values": '1],2fg'})

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data,
                         [u'Query parameter values are not integers'])

    def test_api_users_list_managers(self):
        response = self.forced_auth_req('get',
                                        self.url,
                                        user=self.unicef_staff,
                                        data={"partnership_managers": True})

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 2)

    def test_api_users_retrieve_myprofile(self):
        response = self.forced_auth_req(
            'get',
            reverse("users_v3:myprofile-detail"),
            user=self.unicef_staff,
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["name"],
                         self.unicef_staff.get_full_name())

    def test_api_users_retrieve_myprofile_show_ap_false(self):
        self.assertNotIn(self.unicef_staff.profile.country.name,
                         AP_ALLOWED_COUNTRIES)
        response = self.forced_auth_req(
            'get',
            reverse("users_v3:myprofile-detail"),
            user=self.unicef_staff,
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["show_ap"], False)

    def test_api_users_retrieve_myprofile_show_ap(self):
        self.unicef_staff.profile.country.name = AP_ALLOWED_COUNTRIES[0]
        self.unicef_staff.profile.country.save()
        self.assertIn(self.unicef_staff.profile.country.name,
                      AP_ALLOWED_COUNTRIES)
        response = self.forced_auth_req(
            'get',
            reverse("users_v3:myprofile-detail"),
            user=self.unicef_staff,
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["show_ap"], True)

    def test_minimal_verbosity(self):
        response = self.forced_auth_req('get',
                                        self.url,
                                        data={'verbosity': 'minimal'},
                                        user=self.unicef_superuser)
        response_json = json.loads(response.rendered_content)
        self.assertEqual(len(response_json), 2)
Exemplo n.º 4
0
class TestUserModel:
    """
    Test for the Users Model.
    """

    def setup(self):
        """
        Sets up test fixtures using Factory Boy instances. See factories.py module
        for more information.
        """
        self.user1 = UserFactory()
        self.user2 = UserFactory(
            first_name='John', last_name='Doe', email='*****@*****.**')
        self.users = User.objects.all()

    # Test of User Model created instances
    def test_user1_instance_created(self):
        """
        Test for creation of a user instance in the database.
        """
        assert self.user1.first_name == 'Testy0', 'Should return user first name.'
        assert self.user1.email == '*****@*****.**', 'Should return user email.'
        assert self.user1.password == 'testpassword0000', 'Should return user password.'

    def test_multiple_users_saved_in_database(self):
        """
        Test for number of user instances created in database.
        """
        assert len(
            self.users) == 2, 'Should return number of instances created in database'

    def test_user_instance_get_full_name_method(self):
        """
        Test the user model's get_full_name() method.
        """
        assert self.user1.get_full_name(
        ) == 'Testy4McTesty4', 'Should return full_name method value.'
        assert self.user2.get_full_name(
        ) == 'JohnDoe', 'Should return full_name method value.'

    def test_user_get_short_name_method(self):
        """
        Test the user model's get_short_name() method.
        """
        assert self.user1.get_short_name(
        ) == 'TMcTesty6', 'Should return short_name method value.'
        assert self.user2.get_short_name(
        ) == 'JDoe', 'Should return short_name method value.'

    def test_user_model_saves(self):
        """
        Test user model saves/updates user changed/new data.
        """
        self.user1.password = '******'
        self.user1.is_active = True
        self.user1.acct_type = 'EDU'
        self.user1.save()
        assert self.user1.password == 'testpassword12345'
        assert self.user1.is_active == True
        assert self.user1.acct_type == 'EDU'

    # Test of User Model methods
    def test_user_model_unicode_method(self):
        """
        Test the user model's __unicode__() method.
        """
        assert self.user1.__unicode__(
        ) == 'Testy10McTesty10', 'Should return unicode method value.'
        assert self.user2.__unicode__(
        ) == 'JohnDoe', 'Should return unicode method value.'

    def test_user_model_str_method(self):
        """
        Test the user model's __str__() method.
        """
        assert self.user1.__str__(
        ) == 'Testy12McTesty12', 'Should return str method value.'
        assert self.user2.__str__(
        ) == 'JohnDoe', 'Should return str method value.'
Exemplo n.º 5
0
 def test_user_factory(self):
     user = UserFactory(first_name="Jhon", last_name="Doe")
     self.assertEqual(user.get_full_name(), "Jhon Doe")
     self.assertEqual(user.get_short_name(), "Jhon")
     self.assertIsNotNone(user.email)
     self.assertIsNotNone(user.profile_image)