-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
457 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from myhpi.core.auth import MyHPIOIDCAB | ||
from myhpi.tests.core.utils import MyHPIPageTestCase | ||
|
||
|
||
class AuthTests(MyHPIPageTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.auth_backend = MyHPIOIDCAB() | ||
|
||
def test_create_user(self): | ||
claims = { | ||
"email": "[email protected]", | ||
"given_name": "Ali", | ||
"family_name": "Gator", | ||
"sub": "ali.gator", | ||
} | ||
user = self.auth_backend.create_user(claims) | ||
self.assertEqual(user.username, "ali.gator") | ||
self.assertFalse(user.groups.filter(name="Student").exists()) | ||
|
||
matching_users = self.auth_backend.filter_users_by_claims(claims) | ||
self.assertEqual(len(matching_users), 1) | ||
|
||
def test_create_student(self): | ||
claims = { | ||
"email": "[email protected]", | ||
"given_name": "Grace", | ||
"family_name": "Hopper", | ||
"sub": "grace.hopper", | ||
} | ||
user = self.auth_backend.create_user(claims) | ||
self.assertEqual(user.username, "grace.hopper") | ||
self.assertEqual(user.email, "[email protected]") | ||
self.assertTrue(user.groups.filter(name="Student").exists()) | ||
|
||
def test_update_user(self): | ||
claims = { | ||
"email": "[email protected]", | ||
"given_name": "Johann Wolfgang", | ||
"family_name": "Goethe", | ||
"sub": "jw.goethe", | ||
} | ||
user = self.auth_backend.create_user(claims) | ||
self.assertEqual(user.username, "jw.goethe") | ||
self.assertEqual(user.last_name, "Goethe") | ||
claims["family_name"] = "von Goethe" | ||
user = self.auth_backend.update_user(user, claims) | ||
self.assertEqual(user.first_name, "Johann Wolfgang") | ||
self.assertEqual(user.last_name, "von Goethe") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from unittest import TestCase | ||
|
||
from myhpi import settings | ||
from myhpi.core.utils import ( | ||
alternative_emails, | ||
email_belongs_to_domain, | ||
replace_email_domain, | ||
toggle_institution, | ||
) | ||
|
||
|
||
class EmailUtilTest(TestCase): | ||
def test_email_belongs_to_domain(self): | ||
emails = ["[email protected]", "[email protected]"] | ||
domains = ["example.com", "myhpi.de"] | ||
for email, domain in zip(emails, domains): | ||
self.assertTrue(email_belongs_to_domain(email, domain)) | ||
self.assertFalse(email_belongs_to_domain(emails[0], domains[1])) | ||
|
||
def test_replace_email_domain(self): | ||
email = "[email protected]" | ||
original_domain = "example.com" | ||
new_domain = "myhpi.de" | ||
self.assertEqual(replace_email_domain(email, original_domain, new_domain), "[email protected]") | ||
|
||
def test_toggle_institution(self): | ||
emails = ["[email protected]", "[email protected]", "[email protected]"] | ||
expected = ["[email protected]", "[email protected]", "[email protected]"] | ||
for email, expected_email in zip(emails, expected): | ||
toggled = list(toggle_institution(email)) | ||
if not "unrelated" in email: | ||
self.assertEqual(toggled[0], expected_email) | ||
|
||
def test_alternative_emails(self): | ||
email = "[email protected]" | ||
alternatives = [ | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
] | ||
self.assertSetEqual(set(alternative_emails(email)), set(alternatives)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from myhpi.polls.models import Poll, PollChoice, PollList | ||
from myhpi.tests.core.utils import MyHPIPageTestCase | ||
|
||
|
||
class PollTests(MyHPIPageTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.poll_list = PollList( | ||
title="Polls", | ||
slug="polls", | ||
path="0001000200010005", | ||
depth=4, | ||
is_public=True, | ||
) | ||
self.information_menu.add_child(instance=self.poll_list) | ||
|
||
self.poll = Poll( | ||
title="How are you?", | ||
slug="how-are-you", | ||
question="How are you?", | ||
description="This is a poll to check how you are.", | ||
start_date=datetime.now() - timedelta(days=1), | ||
end_date=datetime.now() + timedelta(days=1), | ||
max_allowed_answers=1, | ||
results_visible=False, | ||
is_public=True, | ||
) | ||
|
||
self.poll_list.add_child(instance=self.poll) | ||
|
||
self.choice_good = PollChoice( | ||
text="Good", | ||
page=self.poll, | ||
votes=0, | ||
) | ||
self.choice_good.save() | ||
self.choice_bad = PollChoice( | ||
text="Bad", | ||
page=self.poll, | ||
votes=0, | ||
) | ||
self.choice_bad.save() | ||
|
||
def test_can_vote_once(self): | ||
self.sign_in_as_student() | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
self.poll.participants.add(self.student) | ||
self.assertFalse(self.poll.can_vote(self.student)) | ||
|
||
def test_post_vote(self): | ||
self.sign_in_as_student() | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
self.client.post( | ||
self.poll.url, | ||
data={"choice": [self.choice_good.id]}, | ||
) | ||
self.choice_good.refresh_from_db() | ||
self.assertEqual(self.choice_good.votes, 1) | ||
self.assertEqual(self.choice_good.percentage(), 100) | ||
self.assertEqual(self.choice_bad.percentage(), 0) | ||
self.assertFalse(self.poll.can_vote(self.student)) | ||
|
||
def test_post_vote_invalid_choice(self): | ||
self.sign_in_as_student() | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
self.client.post( | ||
self.poll.url, | ||
data={"choice": [self.choice_good.id + 9999]}, | ||
) | ||
self.choice_good.refresh_from_db() | ||
self.assertEqual(self.choice_good.votes, 0) | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
|
||
def test_post_vote_no_choice(self): | ||
self.sign_in_as_student() | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
response = self.client.post( | ||
self.poll.url, | ||
data={"choice": []}, | ||
) | ||
self.assertContains(response, "You must select at least one choice.") | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
|
||
def test_post_vote_too_many_choices(self): | ||
self.sign_in_as_student() | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
response = self.client.post( | ||
self.poll.url, | ||
data={"choice": [self.choice_good.id, self.choice_bad.id]}, | ||
) | ||
self.assertContains(response, "You can only select up to 1 options.", 1) | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
|
||
def test_post_vote_before_start_date(self): | ||
self.sign_in_as_student() | ||
self.poll.start_date = datetime.now() + timedelta(days=1) | ||
self.poll.save() | ||
self.assertTrue(self.poll.can_vote(self.student)) | ||
response = self.client.post( | ||
self.poll.url, data={"choice": [self.choice_good.id]}, follow=True | ||
) | ||
self.assertContains(response, "This poll has not yet started.") | ||
self.assertTrue(self.poll.can_vote(self.student)) |
Oops, something went wrong.