def request_user(strategy, details, user=None, request=None, is_new=False, uid=None, **kwargs): if user: return elif is_new: username = re.sub("[^a-zA-Z0-9_]", "_", details["username"].lower()) if ProfileRequest.objects.filter(username=username).count() > 0: messages.add_message(request, messages.WARNING, "Profile request already submitted") return redirect(reverse('request_profile')) first, last = _get_first_last(details) email = details["email"] pr = ProfileRequest( username=username, first_name=first, last_name=last, email=email, affiliation=UserProfile.STATUS_CHOICES[0][0], provider=strategy.backend.name, uid=uid, ) pr.save() messages.add_message(request, messages.SUCCESS, "Your account request has been submitted.") return redirect(reverse('request_profile'))
class TestSocialRequest(TestCase): def setUp(self): self.su = User.objects.create_user(username="******", password="******") self.su.is_staff, self.su.is_superuser = True, True self.su.save() self.pr = ProfileRequest(username="******", email="*****@*****.**", affiliation=UserProfile.RESIDENT, provider="github", uid="1234567890") self.pr.save() self.client.login(username="******", password="******") def test_profile_request_view(self): response = self.client.get("/custom_admin/profile_requests/{0}/" .format(self.pr.pk)) self.assertEqual(response.status_code, 200) self.assertContains(response, self.pr.email) def test_approve_profile_request(self): response = self.client.post("/custom_admin/profile_requests/{0}/" .format(self.pr.pk), { "username": self.pr.username, "first_name": "first", "last_name": "last", "email": self.pr.email, "phone_number": "", "status": self.pr.affiliation, "current_room": "", "former_rooms": "", "former_houses": "", "is_active": "on", "add_user": "", }, follow=True) self.assertRedirects(response, "/custom_admin/profile_requests/") self.assertContains( response, "User {0} was successfully added".format(self.pr.username), ) def test_settings(self): for lib in settings.SOCIAL_AUTH_PIPELINE: module, func = lib.rsplit(".", 1) self.assertNotEqual(None, __import__(module, fromlist=[func])) self.assertIn("social.pipeline.social_auth.social_details", settings.SOCIAL_AUTH_PIPELINE) self.assertIn("social.pipeline.social_auth.social_uid", settings.SOCIAL_AUTH_PIPELINE) self.assertIn("social.pipeline.social_auth.auth_allowed", settings.SOCIAL_AUTH_PIPELINE) self.assertIn("social.pipeline.social_auth.social_user", settings.SOCIAL_AUTH_PIPELINE) self.assertIn("social.pipeline.user.get_username", settings.SOCIAL_AUTH_PIPELINE) self.assertIn("base.pipeline.request_user", settings.SOCIAL_AUTH_PIPELINE)
def save(self): hashed_password = hashers.make_password(self.cleaned_data['password']) profile_request = ProfileRequest( username=self.cleaned_data['username'], first_name=self.cleaned_data['first_name'], last_name=self.cleaned_data['last_name'], email=self.cleaned_data['email'], affiliation=self.cleaned_data['affiliation_with_the_house'], password=hashed_password, ) profile_request.save() return profile_request
def setUp(self): self.su = User.objects.create_user(username="******", password="******") self.su.is_staff, self.su.is_superuser = True, True self.su.save() self.pr = ProfileRequest(username="******", email="*****@*****.**", affiliation=UserProfile.RESIDENT, provider="github", uid="1234567890") self.pr.save() self.client.login(username="******", password="******")
def setUp(self): self.su = User.objects.create_user(username="******", password="******") self.su.is_staff, self.su.is_superuser = True, True self.su.save() self.pr = ProfileRequest(username="******", email="*****@*****.**", first_name="Test First Name", last_name="Test Last Name", affiliation=UserProfile.RESIDENT) self.pr.save() self.client.login(username="******", password="******")
def test_duplicate_request(self): pr = ProfileRequest( username="******", first_name="Tester", last_name="Tested", ) pr.save() response = self.client.post("/request_profile/", { "username": "******", "first_name": pr.first_name, "last_name": pr.last_name, "email": "*****@*****.**", "affiliation_with_the_house": UserProfile.RESIDENT, "password": "******", "confirm_password": "******", }, follow=True) self.assertContains( response, MESSAGES["PROFILE_TAKEN"].format(first_name=pr.first_name, last_name=pr.last_name), ) self.assertEqual(response.status_code, 200) self.assertEqual(0, ProfileRequest.objects.filter(username="******").count())
def setUp(self): self.u = User.objects.create_user(username="******", password="******") self.st = User.objects.create_user(username="******", password="******") self.pu = User.objects.create_user(username="******", password="******") self.su = User.objects.create_user(username="******", password="******") self.np = User.objects.create_user(username="******", password="******") self.st.is_staff = True self.su.is_staff, self.su.is_superuser = True, True self.u.save() self.st.save() self.pu.save() self.su.save() self.np.save() self.m = Manager(title="House President", url_title="president", president=True) self.m.incumbent = UserProfile.objects.get(user=self.pu) self.m.save() self.rt = RequestType(name="Food", url_name="food", enabled=True) self.rt.save() self.rt.managers = [self.m] self.rt.save() self.a = Announcement( manager=self.m, incumbent=self.m.incumbent, body="Test Announcement Body", post_date=datetime.now(), ) self.a.save() self.request = Request(owner=UserProfile.objects.get(user=self.u), body="request body", request_type=self.rt) self.request.save() UserProfile.objects.get(user=self.np).delete() self.pr = ProfileRequest(username="******", email="*****@*****.**", affiliation=UserProfile.STATUS_CHOICES[0][0]) self.pr.save()
class TestPermissions(TestCase): def setUp(self): self.u = User.objects.create_user(username="******", password="******") self.st = User.objects.create_user(username="******", password="******") self.pu = User.objects.create_user(username="******", password="******") self.su = User.objects.create_user(username="******", password="******") self.np = User.objects.create_user(username="******", password="******") self.st.is_staff = True self.su.is_staff, self.su.is_superuser = True, True self.u.save() self.st.save() self.pu.save() self.su.save() self.np.save() self.m = Manager(title="House President", url_title="president", president=True) self.m.incumbent = UserProfile.objects.get(user=self.pu) self.m.save() self.rt = RequestType(name="Food", url_name="food", enabled=True) self.rt.save() self.rt.managers = [self.m] self.rt.save() self.a = Announcement( manager=self.m, incumbent=self.m.incumbent, body="Test Announcement Body", post_date=datetime.now(), ) self.a.save() self.request = Request(owner=UserProfile.objects.get(user=self.u), body="request body", request_type=self.rt) self.request.save() UserProfile.objects.get(user=self.np).delete() self.pr = ProfileRequest(username="******", email="*****@*****.**", affiliation=UserProfile.STATUS_CHOICES[0][0]) self.pr.save() def _admin_required(self, url, success_target=None): response = self.client.get(url) self.assertRedirects(response, "/login/?next=" + url) self.client.login(username="******", password="******") response = self.client.get(url, follow=True) self.assertRedirects(response, "/landing/") self.assertContains(response, MESSAGES["NO_PROFILE"]) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url, follow=True) self.assertRedirects(response, "/") self.assertContains(response, MESSAGES["ADMINS_ONLY"]) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url, follow=True) self.assertRedirects(response, "/") self.assertContains(response, MESSAGES["ADMINS_ONLY"]) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url) if success_target is None: self.assertEqual(response.status_code, 200) else: self.assertRedirects(response, success_target) self.client.logout() def _president_admin_required(self, url, success_target=None): response = self.client.get(url) self.assertRedirects(response, "/login/?next=" + url) self.client.login(username="******", password="******") response = self.client.get(url, follow=True) self.assertRedirects(response, "/landing/") self.assertContains(response, MESSAGES["NO_PROFILE"]) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url, follow=True) self.assertRedirects(response, "/") self.assertContains(response, MESSAGES["PRESIDENTS_ONLY"]) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url, follow=True) self.assertRedirects(response, "/") self.assertContains(response, MESSAGES["PRESIDENTS_ONLY"]) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url) if success_target is None: self.assertEqual(response.status_code, 200) else: self.assertRedirects(response, success_target) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url) if success_target is None: self.assertEqual(response.status_code, 200) else: self.assertRedirects(response, success_target) self.client.logout() def _profile_required(self, url, success_target=None): response = self.client.get(url) self.assertRedirects(response, "/login/?next=" + url) self.client.login(username="******", password="******") response = self.client.get(url, follow=True) self.assertRedirects(response, "/landing/") self.assertContains(response, MESSAGES["NO_PROFILE"]) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url, follow=True) if success_target is None: self.assertEqual(response.status_code, 200) else: self.assertRedirects(response, success_target) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url, follow=True) if success_target is None: self.assertEqual(response.status_code, 200) else: self.assertRedirects(response, success_target) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(url, follow=True) if success_target is None: self.assertEqual(response.status_code, 200) else: self.assertRedirects(response, success_target) self.client.logout() def test_admin_required(self): pages = [ "profile_requests", "profile_requests/{0}".format(self.pr.pk), "manage_users", "modify_user/{0}".format(self.u.username), "add_user", "utilities", ] for page in pages: self._admin_required("/custom_admin/" + page + "/") self._admin_required("/custom_admin/recount/", success_target="/custom_admin/utilities/") self._admin_required("/custom_admin/anonymous_login/", success_target="/") self._admin_required("/custom_admin/end_anonymous_session/", success_target="/custom_admin/utilities/") def test_president_admin_required(self): pages = [ "managers", "managers/president", "add_manager", "request_types", "request_types/{0}".format(self.rt.url_name), "add_request_type", ] for page in pages: self._president_admin_required("/custom_admin/" + page + "/") def test_profile_required(self): pages = [ "manager_directory", "manager_directory/president", "profile/{0}/requests".format(self.u.username), "requests/{0}".format(self.rt.url_name), "archives/all_requests", "requests/{0}/all".format(self.rt.url_name), "my_requests", "request/{0}".format(self.request.pk), "announcements", "announcements/{0}".format(self.a.pk), "announcements/{0}/edit".format(self.a.pk), "announcements/all", ] for page in pages: self._profile_required("/" + page + "/")
class TestProfileRequestAdmin(TestCase): def setUp(self): self.su = User.objects.create_user(username="******", password="******") self.su.is_staff, self.su.is_superuser = True, True self.su.save() self.pr = ProfileRequest(username="******", email="*****@*****.**", first_name="Test First Name", last_name="Test Last Name", affiliation=UserProfile.RESIDENT) self.pr.save() self.client.login(username="******", password="******") def test_view(self): response = self.client.get("/custom_admin/profile_requests/{0}/" .format(self.pr.pk)) self.assertEqual(response.status_code, 200) self.assertContains(response, self.pr.email) def test_approve(self): response = self.client.post("/custom_admin/profile_requests/{0}/" .format(self.pr.pk), { "username": self.pr.username, "first_name": self.pr.first_name, "last_name": self.pr.last_name, "email": self.pr.email, "phone_number": "", "status": self.pr.affiliation, "current_room": "", "former_rooms": "", "former_houses": "", "is_active": "on", "add_user": "", }, follow=True) self.assertRedirects(response, "/custom_admin/profile_requests/") self.assertContains( response, "User {0} was successfully added".format(self.pr.username), ) u = User.objects.get(username=self.pr.username) self.assertEqual(u.email, self.pr.email) def test_missing(self): response = self.client.post("/custom_admin/profile_requests/{0}/" .format(self.pr.pk + 1), { "username": self.pr.username, "first_name": self.pr.first_name, "last_name": self.pr.last_name, "email": self.pr.email, "phone_number": "", "status": self.pr.affiliation, "current_room": "", "former_rooms": "", "former_houses": "", "is_active": "on", "add_user": "", }) self.assertEqual(response.status_code, 404) def test_delete(self): response = self.client.post("/custom_admin/profile_requests/{0}/" .format(self.pr.pk), { "delete_request": "", }, follow=True) self.assertRedirects(response, reverse('manage_profile_requests')) self.assertContains( response, MESSAGES['PREQ_DEL'].format(first_name=self.pr.first_name, last_name=self.pr.last_name, username=self.pr.username), )