2015-02-12 22:42:54 +01:00
|
|
|
import json
|
|
|
|
|
2018-08-22 06:22:28 +02:00
|
|
|
from django.urls import reverse
|
2015-02-12 22:42:54 +01:00
|
|
|
from rest_framework.test import APIClient
|
|
|
|
|
|
|
|
from openslides.utils.test import TestCase
|
|
|
|
|
|
|
|
|
|
|
|
class TestWhoAmIView(TestCase):
|
2019-01-06 16:22:33 +01:00
|
|
|
url = reverse("user_whoami")
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def test_get_anonymous(self):
|
|
|
|
response = self.client.get(self.url)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2015-12-10 00:02:16 +01:00
|
|
|
self.assertEqual(
|
2015-12-11 15:47:22 +01:00
|
|
|
json.loads(response.content.decode()),
|
2019-01-06 16:22:33 +01:00
|
|
|
{"user_id": None, "user": None, "guest_enabled": False},
|
|
|
|
)
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def test_get_authenticated_user(self):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
response = self.client.get(self.url)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(json.loads(response.content.decode()).get("user_id"), 1)
|
|
|
|
self.assertEqual(
|
|
|
|
json.loads(response.content.decode()).get("guest_enabled"), False
|
|
|
|
)
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def test_post(self):
|
|
|
|
response = self.client.post(self.url)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 405)
|
|
|
|
|
|
|
|
|
|
|
|
class TestUserLogoutView(TestCase):
|
2019-01-06 16:22:33 +01:00
|
|
|
url = reverse("user_logout")
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
response = self.client.get(self.url)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 405)
|
|
|
|
|
|
|
|
def test_post_anonymous(self):
|
|
|
|
response = self.client.post(self.url)
|
|
|
|
|
2015-12-11 16:28:56 +01:00
|
|
|
self.assertEqual(response.status_code, 400)
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def test_post_authenticated_user(self):
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
|
|
|
self.client.session["test_key"] = "test_value"
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
response = self.client.post(self.url)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertFalse(hasattr(self.client.session, "test_key"))
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestUserLoginView(TestCase):
|
2019-01-06 16:22:33 +01:00
|
|
|
url = reverse("user_login")
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
response = self.client.get(self.url)
|
|
|
|
|
2016-01-09 01:10:37 +01:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertTrue(json.loads(response.content.decode()).get("info_text"))
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def test_post_no_data(self):
|
|
|
|
response = self.client.post(self.url)
|
|
|
|
|
2015-12-11 16:28:56 +01:00
|
|
|
self.assertEqual(response.status_code, 400)
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def test_post_correct_data(self):
|
|
|
|
response = self.client.post(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.url, {"username": "admin", "password": "admin"}
|
|
|
|
)
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(json.loads(response.content.decode()).get("user_id"), 1)
|
2015-02-12 22:42:54 +01:00
|
|
|
|
|
|
|
def test_post_incorrect_data(self):
|
|
|
|
response = self.client.post(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.url, {"username": "wrong", "password": "wrong"}
|
|
|
|
)
|
2015-02-12 22:42:54 +01:00
|
|
|
|
2015-12-11 16:28:56 +01:00
|
|
|
self.assertEqual(response.status_code, 400)
|