def test_delete(self): u = User(email='*****@*****.**') u.is_active = True u.save() p = Profile(contact_email='*****@*****.**', user=u) p.save() r = Recommendation(profile=p, reviewer_email='*****@*****.**') r.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user_delete')) self.assertEqual(response.status_code, HTTPStatus.OK) User.objects.get(id=u.id) response = self.client.post(reverse('profiles:user_delete'), data={ 'confirm': True, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:login')) with self.assertRaises(User.DoesNotExist): User.objects.get(id=u.id) with self.assertRaises(Profile.DoesNotExist): Profile.objects.get(id=p.id) Recommendation.objects.get(id=r.id)
def test_account_changepassword(self): u = User(email='*****@*****.**') u.is_active = True u.set_password('Myunittest1!') u.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user_change_password')) self.assertEqual(response.status_code, HTTPStatus.OK) response = self.client.post(reverse('profiles:user_change_password'), data={ 'old_password': '******', 'new_password1': 'myunittest1!passchange', 'new_password2': 'myunittest1!passchange', }) self.assertEqual(response.status_code, HTTPStatus.OK) self.assertIn('old_password', response.context['form'].errors) response = self.client.post(reverse('profiles:user_change_password'), data={ 'old_password': '******', 'new_password1': 'Myunittest1!passchange', 'new_password2': 'Myunittest1!passchange', }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user'))
def handle(self, *args, **kwargs): east_blocks = [ 'PAKYONG', 'GANGTOK', 'REGU', 'RHENOCK', 'KHAMDONG', 'RAKDONG TINTEK', 'DUGA', 'RANKA', 'MARTAM', 'PARAKHA' ] west_blocks = [ 'GYALSHING', 'SORENG', 'KALUK', 'DENTAM', 'DARAMDIN', 'YUKSAM', 'BERMIOK MARTAM', 'CHONGRANG', 'CHUMBONG' ] north_blocks = ['MANGAN', 'CHUNGTHANG', 'KABI', 'PASSINGDONG'] south_blocks = [ 'YANGANG', 'SUMBUK', 'NAMCHI', 'RAVANGLA', 'TEMI', 'JORETHANG', 'SIKKIP', 'NAMTHANG' ] districts = ['east', 'west', 'north', 'south'] for district in districts: user_obj = User() user_obj.username = district + '_admin' user_obj.set_password('Pass@123') user_obj.is_district_admin = True user_obj.save() district_obj = District.objects.create( user=user_obj, district_name=district.upper() + ' SIKKIM') for block in eval(district + '_blocks'): b_user_obj = User() b_user_obj.username = block.lower() + '_admin' b_user_obj.set_password('Pass@123') b_user_obj.is_block_admin = True b_user_obj.save() block_obj = Block.objects.create(user=b_user_obj, block_name=block, block_district=district_obj) print("Done")
def test_account_edit(self): u = User(email='*****@*****.**') u.username = '******' u.name = 'Unit Test' u.is_active = True u.set_password('Myunitarytest1!') u.save() self.client.force_login(u) with self.settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend'): response = self.client.post(reverse('profiles:user_edit'), { 'username': u.username, 'name': 'Another Unit Test', 'email': u.email, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user')) self.assertEqual(len(mail.outbox), 1) u.refresh_from_db() self.assertTrue(u.is_active) self.assertEqual(u.name, 'Another Unit Test')
def test_profile_delete(self): u = User(email='*****@*****.**') u.is_active = True u.save() p = Profile(contact_email='*****@*****.**', user=u) p.save() r = Recommendation(profile=p, reviewer_email='*****@*****.**') r.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user_profile_delete')) self.assertEqual(response.status_code, HTTPStatus.OK) User.objects.get(id=u.id) response = self.client.post(reverse('profiles:user_profile_delete'), data={ 'confirm': True, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user')) p = Profile.all_objects.get(id=p.id) self.assertNotEquals(p.deleted_at, None)
def get(self, request): args = dict( client_id=settings.FACEBOOK_APP_ID, redirect_uri=settings.FACEBOOK_URL_CALLBACK, scope="email,user_about_me,friends_about_me,user_hometown,publish_stream", state='454k4j54fijfs', ) try: args['code'] = request.GET['code'] args["client_secret"] = settings.FACEBOOK_APP_TOKEN response = parse_qs(urlopen("https://graph.facebook" ".com/oauth/access_token?" + urlencode(args)).read()) access_token = response["access_token"][-1] profile = json.load(urlopen("https://graph.facebook.com/me?" + urlencode(dict(access_token=access_token)))) f_friends = json.load( urlopen("https://graph.facebook.com/me/friends?fields=id&" + urlencode(dict(access_token=access_token, limit=0))) ) try: user = User.objects.get(id=profile['id']) user.token = access_token user.save() request.session['fb_id'] = user.id request.session['token'] = access_token if user.meta == None: return HttpResponseRedirect('/registrar/') return HttpResponseRedirect('/') except: user = User( id = profile['id'], token = access_token, name = force_unicode(profile['name']), email = profile['email'], picture = 'http://graph.facebook' '.com/%s/picture?type=large' % profile['id'], username = profile.get('username', profile['id']), ) user.save() friends_fb = [ int(i[u'id']) for i in f_friends['data']] for user_friend in User.objects.filter(id__in=friends_fb): user_friend.friends.add(user) user.friends.add(user_friend) user.save() return HttpResponseRedirect('/registrar/') except Exception, er: print er return HttpResponseRedirect('https://www.facebook' '.com/dialog/oauth?'+ urlencode(args))
def test_account_page(self): u = User(email='*****@*****.**') u.is_active = True u.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user')) self.assertEqual(response.status_code, HTTPStatus.OK) self.assertContains(response, '*****@*****.**')
def create_user(self): user = User( first_name=chance.first(gender='m', language='ru'), last_name=chance.last(gender='m', language='ru'), email=chance.email(), is_active=True, is_email_confirmed=True, ) user.set_password("@120804a") user.save() self.stdout.write('Successfully created user (%s)' % (user.email, )) return user
def create_user(self): user = User( first_name="Davit", last_name="Vardanyan", email="*****@*****.**", is_active=True, is_email_confirmed=True, is_superuser=True, is_staff=True, ) user.set_password("@120804a") user.save() self.stdout.write('Successfully created user (%s)' % (user.email, )) return user
def test_account_login(self): u = User(email='*****@*****.**') u.is_active = True u.set_password('Myunitarytest1!') u.save() next = reverse('profiles:user_edit') response = self.client.get(reverse('profiles:login') + '?next=' + next) response = self.client.post(reverse('profiles:login'), { 'username': '******', 'password': '******', }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, next)
def test_account_edit_email(self): u = User(email='*****@*****.**') u.username = '******' u.name = 'Unit Test' u.is_active = True u.set_password('Myunitarytest1!') u.save() self.client.force_login(u) with self.settings( EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend'): response = self.client.post(reverse('profiles:user_edit'), { 'username': u.username, 'name': 'Another Unit Test', 'email': '*****@*****.**', }, follow=True) self.assertRedirects( response, reverse('profiles:login') + '?next=' + reverse('profiles:user')) self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(mail.outbox), 2) self.assertEqual(mail.outbox[0].to, ['*****@*****.**']) self.assertEqual(mail.outbox[1].to, ['*****@*****.**']) u.refresh_from_db() self.assertFalse(u.is_active) self.assertEqual(u.name, 'Another Unit Test') self.assertEqual(u.email, '*****@*****.**') token = mail.outbox[0].context['token'] self.assertTrue('user_confirmation_token' in self.client.session) self.assertEqual(self.client.session['user_confirmation_token'], token) response = self.client.get(reverse('profiles:signup_confirm'), data={ 'token': token, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user'))
def save(self, commit=True): employee = super().save(commit=False) user = User() user.username = f'emp_{randint(1000, 9999)}' user.set_password('Pass@123') user.first_name = self.cleaned_data['first_name'] user.middle_name = self.cleaned_data['middle_name'] user.last_name = self.cleaned_data['last_name'] user.date_of_birth = self.cleaned_data['date_of_birth'] user.gender = self.cleaned_data['gender'] user.is_employee = True user.is_teacher = self.cleaned_data['is_teacher'] user.save() employee.employee_user = user if commit: return employee.save() return employee
def test_profile_delete_new(self): u = User(email='*****@*****.**') u.is_active = True u.save() p = Profile(contact_email='*****@*****.**', user=u) p.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user_profile_delete')) self.assertEqual(response.status_code, HTTPStatus.OK) User.objects.get(id=u.id) response = self.client.post(reverse('profiles:user_profile_delete'), data={ 'confirm': True, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user')) with self.assertRaises(Profile.DoesNotExist): p.refresh_from_db()
from hackathons.models import Hackathon, Tier, Perk, Sponsorship fake = Faker() print("Generating users...") users = [] for i in range(10): username = fake.user_name() user = User( first_name=fake.first_name(), last_name=fake.last_name(), email=fake.email(), username=username, password=username, ) user.save() users.append(user) print("Generating industries...") industries = [] colors = { "Web Dev": "blue", "ML": "green", "Software": "purple", "Systems": "orange", "Data": "yellow", } for t in colors.keys(): industry = Industry(name=t, color=colors[t]) industry.save() industries.append(industry)
def create_test_user(self, email=None, password=None): user = User(email=email or REGISTER_EMAIL) user.set_password(REGISTER_PASS or REGISTER_PASS) user.save() return user
author_pk = post["fields"]["author"] # Find author by pk for user in users: if user["pk"] == author_pk: author_saved = user # Get author data username = author_saved["fields"]["username"] password = author_saved["fields"]["password"] karma = author_saved["fields"]["karma"] # Get or create author try: author = User.objects.get(username=username) except: author = User(username=username, password=password, karma=karma) author.save() series = None # Create a series for the chapter, with slug/title of a parent if post["fields"]["post_type"] == "chapter": # Find parent for p in posts: if p["pk"] == post["fields"]["parent"]: parent = p # Modify slug to parent-slug-chapter-slug. slug = parent["fields"]["slug"] + "-" + slug # Create series for a parent series_title = parent["fields"]["title"] series_slug = parent["fields"]["slug"]