def test_view_public_to_mm_when_no_common_programs(self): """ Users are not allowed to view public_to_mm profile if there are no common programs. """ new_user = SocialProfileFactory.create(account_privacy=Profile.PUBLIC_TO_MM).user verified_user = SocialProfileFactory.create(verified_micromaster_user=True).user request = Mock(user=verified_user) view = Mock(kwargs={'user': self.get_social_auth_uid(new_user)}) with self.assertRaises(Http404): self.perm.has_permission(request, view)
def test_can_view_if_verified_mm_user(self): """ Verified MM users are allowed to view public_to_mm profile. """ new_user = SocialProfileFactory.create(account_privacy=Profile.PUBLIC_TO_MM).user verified_user = SocialProfileFactory.create(verified_micromaster_user=True).user program = ProgramFactory.create() for user in [new_user, verified_user]: ProgramEnrollment.objects.create( program=program, user=user, ) request = Mock(user=verified_user) view = Mock(kwargs={'user': self.get_social_auth_uid(new_user)}) assert self.perm.has_permission(request, view) is True
def test_roles_can_see_profile(self, role_to_set): """ Staff and Instructors can see private profile of user with same program """ # Create a private profile new_user = SocialProfileFactory.create( verified_micromaster_user=False, account_privacy=Profile.PRIVATE, ).user program = ProgramFactory.create() ProgramEnrollment.objects.create( program=program, user=new_user, ) # Make self.unverified_user a staff of that program role = Role.objects.create( user=self.user, program=program, role=role_to_set, ) request = Mock(user=self.user) view = Mock(kwargs={'user': self.get_social_auth_uid(new_user)}) assert self.perm.has_permission(request, view) is True # Change role.program and assert that user no longer has permission to see private profile role.program = ProgramFactory.create() role.save() with self.assertRaises(Http404): self.perm.has_permission(request, view)
def create_user_for_login(is_staff=True, username=None): """Create a test user that can sign into the app""" later = now_in_utc() + timedelta(weeks=5000) with mute_signals(post_save): user = SocialProfileFactory.create( validated=True, user__is_staff=is_staff, image=None, # make these None so the default image is used image_small=None, image_medium=None, **({ 'user__username': username } if username is not None else {}), user__social_auth__extra_data={ 'access_token': 'fake', 'refresh_token': 'fake', 'updated_at': later.timestamp(), 'expires_in': 3600, }).user UserCacheRefreshTime.objects.create( user=user, enrollment=later, certificate=later, current_grade=later, ) user.set_password(DEFAULT_PASSWORD) user.save() return user
def create_user_for_login(is_staff=True, username=None): """Create a test user that can log into the app""" later = now_in_utc() + timedelta(weeks=5000) with mute_signals(post_save): user = SocialProfileFactory.create( validated=True, user__is_staff=is_staff, image=None, # make these None so the default image is used image_small=None, image_medium=None, **({'user__username': username} if username is not None else {}), user__social_auth__extra_data={ 'access_token': 'fake', 'refresh_token': 'fake', 'updated_at': later.timestamp(), 'expires_in': 3600, } ).user UserCacheRefreshTime.objects.create( user=user, enrollment=later, certificate=later, current_grade=later, ) user.set_password(DEFAULT_PASSWORD) user.save() return user
def test_full_name(self): """ test full name of user on given profile. """ first = "Tester" last = "KK" profile = SocialProfileFactory.create(first_name=first, last_name=last) assert util.full_name(profile.user) == "{} {}".format(first, last)
def test_full_name_when_first_name_empty(self): """ Test full name when first name is set empty on profile. """ first = "" last = "Tester" profile = SocialProfileFactory.create(first_name=first, last_name=last) assert util.full_name(profile.user) == "{} {}".format(profile.user.username, last)
def test_full_name_when_last_name_empty(self): """ Test full name when last name is set empty on profile. """ first = "Tester" last = "" profile = SocialProfileFactory.create(first_name=first, last_name=last) assert util.full_name(profile.user) == "{name} ".format(name=first)
def test_users_can_view_public_profile(self): """ Users are allowed to view public profile. """ new_profile = SocialProfileFactory.create(account_privacy=Profile.PUBLIC) request = Mock(user=self.user) view = Mock(kwargs={'user': self.get_social_auth_uid(new_profile.user)}) assert self.perm.has_permission(request, view) is True
def test_can_view_public_if_anonymous_user(self): """ Anonymous can view public profiles. """ new_profile = SocialProfileFactory.create(account_privacy=Profile.PUBLIC) request = Mock(user=Mock(is_anonymous=True)) view = Mock(kwargs={'user': self.get_social_auth_uid(new_profile.user)}) assert self.perm.has_permission(request, view) is True
def setUpTestData(cls): """ Create user, run, and coupons for testing """ super().setUpTestData() cls.user = SocialProfileFactory.create().user UserSocialAuthFactory.create(user=cls.user, provider='not_edx') run = CourseRunFactory.create(course__program__financial_aid_availability=True) cls.coupon = CouponFactory.create(content_object=run.course.program) UserCoupon.objects.create(coupon=cls.coupon, user=cls.user)
def test_cant_view_if_privacy_weird(self): """ Users can not open profiles with ambiguous account_privacy settings. """ new_profile = SocialProfileFactory.create(account_privacy='weird_setting') request = Mock(user=self.user) view = Mock(kwargs={'user': self.get_social_auth_uid(new_profile.user)}) with self.assertRaises(Http404): self.perm.has_permission(request, view)
def test_cant_view_if_non_verified_mm_user(self): """ Non verified micromaster users are not supposed to view public_to_mm profiles. """ new_profile = SocialProfileFactory.create(account_privacy=Profile.PUBLIC_TO_MM) request = Mock(user=self.user) view = Mock(kwargs={'user': self.get_social_auth_uid(new_profile.user)}) with self.assertRaises(Http404): self.perm.has_permission(request, view)
def setUpTestData(cls): """ Create user, run, and coupons for testing """ super().setUpTestData() cls.user = SocialProfileFactory.create().user UserSocialAuthFactory.create(user=cls.user, provider='not_edx') run = CourseRunFactory.create( course__program__financial_aid_availability=True) cls.coupon = CouponFactory.create(content_object=run.course.program) UserCoupon.objects.create(coupon=cls.coupon, user=cls.user)
def create_and_login_user(self): """ Create and login a user """ profile = SocialProfileFactory.create( agreed_to_terms_of_service=True, filled_out=True, ) UserSocialAuthFactory.create(user=profile.user, provider='not_edx') self.client.force_login(profile.user) return profile
def test_cant_view_if_privacy_is_private(self): """ Users are not supposed to view private profiles. """ new_profile = SocialProfileFactory.create(account_privacy=Profile.PRIVATE) request = Mock(user=self.user) view = Mock(kwargs={'user': self.get_social_auth_uid(new_profile.user)}) with self.assertRaises(Http404): self.perm.has_permission(request, view)
def test_cant_view_if_anonymous_user(self, account_privacy_setting): """ Anonymous are not supposed to view public_to_mm or private profiles. """ new_profile = SocialProfileFactory.create(account_privacy=account_privacy_setting) request = Mock(user=Mock(is_anonymous=True)) view = Mock(kwargs={'user': self.get_social_auth_uid(new_profile.user)}) with self.assertRaises(Http404): self.perm.has_permission(request, view)
def setUp(self): """ Create a user with a default social auth """ super().setUp() profile = SocialProfileFactory.create( agreed_to_terms_of_service=True, filled_out=True, ) self.user = profile.user UserSocialAuthFactory.create(user=self.user, provider='not_edx')
def setUp(self): """ Create a user with a default social auth """ super(SocialTests, self).setUp() profile = SocialProfileFactory.create( agreed_to_terms_of_service=True, filled_out=True, ) self.user = profile.user UserSocialAuthFactory.create(user=self.user, provider='not_edx')
def setUp(self): super().setUp() self.user = SocialProfileFactory.create(verified_micromaster_user=False).user self.perm = CanSeeIfNotPrivate()
def setUp(self): super(CanSeeIfNotPrivateTests, self).setUp() self.user = SocialProfileFactory.create(verified_micromaster_user=False).user self.perm = CanSeeIfNotPrivate()