def login(): from members.models import Profile if 'app-key' not in request.headers: response = jsonify({'code': 401, 'message': 'App-Key header is required.'}) response.status_code = 401 return response application = Application.find_by_key(request.headers['app-key']) if not application: response = jsonify({'code': 401, 'message': 'Invalid App-key.'}) response.status_code = 401 return response g.application = application form = AuthenticationForm(request.form) if not form.validate(): return form.errors_as_json() profile = Profile.find_by_email(form.email.data) if not profile: form.errors['email'] = ['Invalid password or account does not exists.'] return form.errors_as_json() if not profile.check_passwd(form.password.data): form.errors['email'] = ['Invalid password or account does not exists.'] return form.errors_as_json() session = Session(profile.id) session.save() return jsonify(session.to_json()), 200
def test_send_renewal_accepted_message(self, send_email): member = Member( email="*****@*****.**", first_name="John", last_name="Doe", profile=Profile(), ) renewal = Renewal(pk=0, member=member, contribution=2) emails.send_renewal_accepted_message(renewal) with translation.override(renewal.member.profile.language): send_email.assert_called_once_with( renewal.member.email, _("Renewal accepted"), "registrations/email/renewal_accepted.txt", { "name": renewal.member.get_full_name(), "fees": floatformat(renewal.contribution, 2), "thalia_pay_enabled": settings.THALIA_PAY_ENABLED_PAYMENT_METHOD, "url": (settings.BASE_URL + reverse("registrations:renew", )), }, )
def test_form_valid(self, board_mail, references_mail): mock_form = Mock(spec=RenewalFormView) member = Member( email="*****@*****.**", first_name="John", last_name="Doe", profile=Profile(), ) renewal = Renewal(pk=0, member=member) mock_form.save = MagicMock(return_value=renewal) with self.subTest("No references required"): renewal.no_references = True return_value = self.view.form_valid(mock_form) board_mail.assert_called_once_with(renewal) self.assertFalse(references_mail.called) self.assertEqual(return_value.status_code, 302) self.assertEqual(return_value.url, reverse("registrations:renew-success")) board_mail.reset_mock() with self.subTest("References required"): renewal.no_references = False return_value = self.view.form_valid(mock_form) board_mail.assert_called_once_with(renewal) references_mail.assert_called_once_with(renewal) self.assertEqual(return_value.status_code, 302) self.assertEqual(return_value.url, reverse("registrations:renew-success"))
def update(): if len(request.form) > 0: form = AccountForm(request.form, obj=g.member) else: form = AccountForm(ImmutableMultiDict(request.json), obj=g.member) if not form.validate(): return form.errors_as_json() if not Profile.is_email_available(form.email.data, g.member.profile.id): form.errors['email'] = [] form.errors['email'].append('This email is already taken.') return form.errors_as_json() g.member = g.member.update(form=None, **form.get_as_dict()) if hasattr(g.member, 'modified'): g.member.modified = datetime.datetime.utcnow() g.member.profile.email = form.email.data if form.password.data: g.member.profile.set_password(form.password.data) g.member.save() return jsonify(g.member.to_json()), 200
def setUpTestData(cls): # Add 10 members with default membership members = [Member(id=i, username=i) for i in range(10)] Member.objects.bulk_create(members) memberships = [ Membership(user_id=i, type=Membership.MEMBER) for i in range(10) ] Membership.objects.bulk_create(memberships) profiles = [Profile(user_id=i) for i in range(10)] Profile.objects.bulk_create(profiles)
def test_send_references_information_message(self, send_email): with self.subTest("Registrations"): registration = Registration( language="en", email="*****@*****.**", first_name="John", last_name="Doe", pk=uuid.uuid4(), ) emails.send_references_information_message(registration) send_email.assert_called_once_with( "*****@*****.**", "Information about references", "registrations/email/references_information.txt", { "name": registration.get_full_name(), "reference_link": ( "https://thalia.localhost" + reverse("registrations:reference", args=[registration.pk]) ), }, ) send_email.reset_mock() with self.subTest("Renewals"): member = Member( email="*****@*****.**", first_name="John", last_name="Doe", profile=Profile(language="en"), ) renewal = Renewal(pk=uuid.uuid4(), member=member) emails.send_references_information_message(renewal) send_email.assert_called_once_with( "*****@*****.**", "Information about references", "registrations/email/references_information.txt", { "name": renewal.member.get_full_name(), "reference_link": ( "https://thalia.localhost" + reverse("registrations:reference", args=[renewal.pk]) ), }, )
def setUpTestData(cls): # Add 10 members with default membership members = [Member(id=i, username=i) for i in range(7)] Member.objects.bulk_create(members) profiles = [Profile(user_id=i) for i in range(7)] Profile.objects.bulk_create(profiles) Membership(user_id=0, type=Membership.HONORARY, until=date.today() + timedelta(days=1)).save() Membership( user_id=1, type=Membership.BENEFACTOR, until=date.today() + timedelta(days=1), ).save() Membership(user_id=2, type=Membership.MEMBER, until=date.today() + timedelta(days=1)).save() Membership(user_id=3, type=Membership.MEMBER, until=date.today() + timedelta(days=1)).save() Membership( user_id=3, type=Membership.MEMBER, until=date.today() - timedelta(days=365 * 10), ).save() Membership( user_id=4, type=Membership.BENEFACTOR, until=date.today() + timedelta(days=1), ).save() Membership( user_id=4, type=Membership.MEMBER, until=date.today() - timedelta(days=365 * 10), ).save() Membership( user_id=5, type=Membership.MEMBER, until=date.today() - timedelta(days=365 * 10), ).save()
def test_send_renewal_complete_message(self, send_email): member = Member( email="*****@*****.**", first_name="John", last_name="Doe", profile=Profile(), ) renewal = Renewal(pk=0, member=member) emails.send_renewal_complete_message(renewal) with translation.override(renewal.member.profile.language): send_email.assert_called_once_with( renewal.member.email, _("Renewal successful"), "registrations/email/renewal_complete.txt", {"name": renewal.member.get_full_name()}, )
def test_send_new_renewal_board_message(self, send_email): member = Member( email="*****@*****.**", first_name="John", last_name="Doe", profile=Profile(), ) renewal = Renewal(pk=0, member=member) emails.send_new_renewal_board_message(renewal) send_email.assert_called_once_with( settings.BOARD_NOTIFICATION_ADDRESS, "New renewal", "registrations/email/renewal_board.txt", { "name": renewal.member.get_full_name(), "url": ("https://thalia.localhost" + reverse( "admin:registrations_renewal_change", args=[renewal.pk])), }, )
def register_view(request): if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): first_name = form.data["first_name"] last_name = form.data["last_name"] username = form.data["username"] password = form.data["password"] email = form.data["email"] user = User.objects.create_user(username=username, first_name=first_name, last_name=last_name, password=password, email=email) user.save() login(request, user) date_of_birth = form.data["date_of_birth"] #country = form.data["country"] country = "France" phone1 = form.data["phone1"] phone2 = form.data["phone2"] phone3 = form.data["phone3"] profile = Profile(user=user, date_of_birth=date_of_birth, country=country, phone1=phone1, phone2=phone2, phone3=phone3) return render(request, 'members/register_success.html') else: form = RegisterForm() return render(request, 'members/register.html', {'form': form})
def test_send_renewal_accepted_message(self, send_email): member = Member( email="*****@*****.**", first_name="John", last_name="Doe", profile=Profile(language="en"), ) renewal = Renewal(pk=0, member=member) payment = Payment(amount=2,) emails.send_renewal_accepted_message(renewal, payment) with translation.override(renewal.member.profile.language): send_email.assert_called_once_with( renewal.member.email, _("Renewal accepted"), "registrations/email/renewal_accepted.txt", { "name": renewal.member.get_full_name(), "fees": floatformat(payment.amount, 2), }, )