1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00
supysonic/tests/frontend/test_user.py

261 lines
9.8 KiB
Python
Raw Normal View History

2017-11-24 18:24:58 +00:00
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
2018-03-04 20:49:56 +00:00
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
2017-11-24 18:24:58 +00:00
#
# Distributed under terms of the GNU AGPLv3 license.
import unittest
2017-11-24 18:24:58 +00:00
import uuid
from flask import escape
2017-12-19 22:16:55 +00:00
from supysonic.db import User, ClientPrefs
2017-11-24 18:24:58 +00:00
from .frontendtestbase import FrontendTestBase
2019-06-29 15:25:44 +00:00
2017-11-24 18:24:58 +00:00
class UserTestCase(FrontendTestBase):
def setUp(self):
2020-11-22 15:12:14 +00:00
super().setUp()
2017-11-24 18:24:58 +00:00
2022-12-31 15:47:24 +00:00
self.users = {u.name: u.id for u in User.select()}
2017-11-24 18:24:58 +00:00
def test_index(self):
2019-06-29 15:25:44 +00:00
self._login("bob", "B0b")
rv = self.client.get("/user", follow_redirects=True)
self.assertIn("There's nothing much to see", rv.data)
self.assertNotIn("Users", rv.data)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.get("/user")
self.assertIn("Users", rv.data)
2017-11-24 18:24:58 +00:00
def test_details(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.get("/user/string", follow_redirects=True)
self.assertIn("badly formed", rv.data)
rv = self.client.get("/user/" + str(uuid.uuid4()), follow_redirects=True)
self.assertIn("No such user", rv.data)
rv = self.client.get("/user/" + str(self.users["bob"]))
self.assertIn("bob", rv.data)
2017-11-24 18:24:58 +00:00
self._logout()
2022-12-31 15:47:24 +00:00
ClientPrefs.create(user=User[self.users["bob"]], client_name="tests")
2017-11-24 18:24:58 +00:00
2019-06-29 15:25:44 +00:00
self._login("bob", "B0b")
rv = self.client.get("/user/" + str(self.users["alice"]), follow_redirects=True)
self.assertIn("There's nothing much to see", rv.data)
self.assertNotIn("<h2>bob</h2>", rv.data)
rv = self.client.get("/user/me")
self.assertIn("<h2>bob</h2>", rv.data)
self.assertIn("tests", rv.data)
2017-11-24 18:24:58 +00:00
def test_update_client_prefs(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.post("/user/me")
self.assertIn("updated", rv.data) # does nothing, says it's updated anyway
2017-11-24 18:24:58 +00:00
# error cases, silently ignored
2019-06-29 15:25:44 +00:00
self.client.post("/user/me", data={"garbage": "trash"})
self.client.post("/user/me", data={"a_b_c_d_e_f": "g_h_i_j_k"})
self.client.post("/user/me", data={"_l": "m"})
self.client.post("/user/me", data={"n_": "o"})
self.client.post("/user/me", data={"inexisting_client": "setting"})
2017-11-24 18:24:58 +00:00
2022-12-31 15:47:24 +00:00
ClientPrefs.create(user=User[self.users["alice"]], client_name="tests")
2017-11-24 18:24:58 +00:00
2019-06-29 15:25:44 +00:00
rv = self.client.post(
"/user/me", data={"tests_format": "mp3", "tests_bitrate": 128}
)
self.assertIn("updated", rv.data)
2022-12-31 15:47:24 +00:00
prefs = ClientPrefs[User[self.users["alice"]], "tests"]
self.assertEqual(prefs.format, "mp3")
self.assertEqual(prefs.bitrate, 128)
2017-11-24 18:24:58 +00:00
2019-06-29 15:25:44 +00:00
self.client.post("/user/me", data={"tests_delete": 1})
2022-12-31 15:47:24 +00:00
self.assertEqual(ClientPrefs.select().count(), 0)
2017-11-24 18:24:58 +00:00
def test_change_username_get(self):
2019-06-29 15:25:44 +00:00
self._login("bob", "B0b")
rv = self.client.get("/user/whatever/changeusername", follow_redirects=True)
self.assertIn("There's nothing much to see", rv.data)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.get("/user/whatever/changeusername", follow_redirects=True)
self.assertIn("badly formed", rv.data)
rv = self.client.get(
2023-01-08 15:16:28 +00:00
f"/user/{uuid.uuid4()}/changeusername", follow_redirects=True
2019-06-29 15:25:44 +00:00
)
self.assertIn("No such user", rv.data)
self.client.get("/user/{}/changeusername".format(self.users["bob"]))
2017-11-24 18:24:58 +00:00
def test_change_username_post(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.post("/user/whatever/changeusername", follow_redirects=True)
self.assertIn("badly formed", rv.data)
rv = self.client.post(
2023-01-08 15:16:28 +00:00
f"/user/{uuid.uuid4()}/changeusername", follow_redirects=True
2019-06-29 15:25:44 +00:00
)
self.assertIn("No such user", rv.data)
path = "/user/{}/changeusername".format(self.users["bob"])
rv = self.client.post(path, follow_redirects=True)
self.assertIn("required", rv.data)
rv = self.client.post(path, data={"user": "bob"}, follow_redirects=True)
self.assertIn("No changes", rv.data)
rv = self.client.post(
path, data={"user": "b0b", "admin": 1}, follow_redirects=True
)
self.assertIn("updated", rv.data)
self.assertIn("b0b", rv.data)
2022-12-31 15:47:24 +00:00
bob = User[self.users["bob"]]
self.assertEqual(bob.name, "b0b")
self.assertTrue(bob.admin)
2019-06-29 15:25:44 +00:00
rv = self.client.post(path, data={"user": "alice"}, follow_redirects=True)
2022-12-31 15:47:24 +00:00
self.assertEqual(User[self.users["bob"]].name, "b0b")
2017-11-24 18:24:58 +00:00
def test_change_mail_get(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
self.client.get("/user/me/changemail")
2017-11-24 18:24:58 +00:00
# whatever
def test_change_mail_post(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
self.client.post("/user/me/changemail")
2017-11-24 18:24:58 +00:00
# whatever
def test_change_password_get(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.get("/user/me/changepass")
self.assertIn("Current password", rv.data)
rv = self.client.get("/user/{}/changepass".format(self.users["bob"]))
self.assertNotIn("Current password", rv.data)
2017-11-24 18:24:58 +00:00
def test_change_password_post(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
path = "/user/me/changepass"
2017-11-24 18:24:58 +00:00
rv = self.client.post(path)
2019-06-29 15:25:44 +00:00
self.assertIn("required", rv.data)
rv = self.client.post(path, data={"current": "alice"})
self.assertIn("required", rv.data)
rv = self.client.post(path, data={"new": "alice"})
self.assertIn("required", rv.data)
rv = self.client.post(path, data={"current": "alice", "new": "alice"})
self.assertIn("password and its confirmation don", rv.data)
rv = self.client.post(
path, data={"current": "alice", "new": "alice", "confirm": "alice"}
)
self.assertIn("Wrong password", rv.data)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
rv = self._login("alice", "Alic3")
self.assertIn("Logged in", rv.data)
rv = self.client.post(
path,
data={"current": "Alic3", "new": "alice", "confirm": "alice"},
follow_redirects=True,
)
self.assertIn("changed", rv.data)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
rv = self._login("alice", "alice")
self.assertIn("Logged in", rv.data)
2017-11-24 18:24:58 +00:00
2019-06-29 15:25:44 +00:00
path = "/user/{}/changepass".format(self.users["bob"])
2017-11-24 18:24:58 +00:00
rv = self.client.post(path)
2019-06-29 15:25:44 +00:00
self.assertIn("required", rv.data)
rv = self.client.post(path, data={"new": "alice"})
self.assertIn("password and its confirmation don", rv.data)
rv = self.client.post(
path, data={"new": "alice", "confirm": "alice"}, follow_redirects=True
)
self.assertIn("changed", rv.data)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
rv = self._login("bob", "alice")
self.assertIn("Logged in", rv.data)
2017-11-24 18:24:58 +00:00
def test_add_get(self):
2019-06-29 15:25:44 +00:00
self._login("bob", "B0b")
rv = self.client.get("/user/add", follow_redirects=True)
self.assertIn("There's nothing much to see", rv.data)
self.assertNotIn("Add User", rv.data)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.get("/user/add")
self.assertIn("Add User", rv.data)
2017-11-24 18:24:58 +00:00
def test_add_post(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.post("/user/add")
self.assertIn("required", rv.data)
rv = self.client.post("/user/add", data={"user": "user"})
self.assertIn("Please provide a password", rv.data)
rv = self.client.post("/user/add", data={"passwd": "passwd"})
self.assertIn("required", rv.data)
rv = self.client.post("/user/add", data={"user": "name", "passwd": "passwd"})
self.assertIn("passwords don", rv.data)
rv = self.client.post(
"/user/add",
data={"user": "alice", "passwd": "passwd", "passwd_confirm": "passwd"},
)
self.assertIn(escape("User 'alice' exists"), rv.data)
2022-12-31 15:47:24 +00:00
self.assertEqual(User.select().count(), 2)
2017-11-24 18:24:58 +00:00
2019-06-29 15:25:44 +00:00
rv = self.client.post(
"/user/add",
data={
"user": "user",
"passwd": "passwd",
"passwd_confirm": "passwd",
"admin": 1,
},
follow_redirects=True,
)
self.assertIn("added", rv.data)
2022-12-31 15:47:24 +00:00
self.assertEqual(User.select().count(), 3)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
rv = self._login("user", "passwd")
self.assertIn("Logged in", rv.data)
2017-11-24 18:24:58 +00:00
def test_delete(self):
2019-06-29 15:25:44 +00:00
path = "/user/del/{}".format(self.users["bob"])
2017-11-24 18:24:58 +00:00
2019-06-29 15:25:44 +00:00
self._login("bob", "B0b")
rv = self.client.get(path, follow_redirects=True)
self.assertIn("There's nothing much to see", rv.data)
2022-12-31 15:47:24 +00:00
self.assertEqual(User.select().count(), 2)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.get("/user/del/string", follow_redirects=True)
self.assertIn("badly formed", rv.data)
rv = self.client.get("/user/del/" + str(uuid.uuid4()), follow_redirects=True)
self.assertIn("No such user", rv.data)
rv = self.client.get(path, follow_redirects=True)
self.assertIn("Deleted", rv.data)
2022-12-31 15:47:24 +00:00
self.assertEqual(User.select().count(), 1)
2017-11-24 18:24:58 +00:00
self._logout()
2019-06-29 15:25:44 +00:00
rv = self._login("bob", "B0b")
self.assertIn("Wrong username or password", rv.data)
2017-11-24 18:24:58 +00:00
def test_lastfm_link(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.get("/user/me/lastfm/link", follow_redirects=True)
self.assertIn("Missing LastFM auth token", rv.data)
rv = self.client.get(
"/user/me/lastfm/link",
query_string={"token": "abcdef"},
follow_redirects=True,
)
self.assertIn("No API key set", rv.data)
2017-11-24 18:24:58 +00:00
def test_lastfm_unlink(self):
2019-06-29 15:25:44 +00:00
self._login("alice", "Alic3")
rv = self.client.get("/user/me/lastfm/unlink", follow_redirects=True)
self.assertIn("Unlinked", rv.data)
2017-11-24 18:24:58 +00:00
2019-06-29 15:25:44 +00:00
if __name__ == "__main__":
unittest.main()