1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00
supysonic/tests/api/test_user.py

229 lines
7.9 KiB
Python
Raw Normal View History

2017-11-01 19:55:35 +00:00
#!/usr/bin/env python
2018-03-04 20:49:56 +00:00
# coding: utf-8
2017-11-01 19:55:35 +00:00
#
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
# 2017 Óscar García Amor
2017-11-01 19:55:35 +00:00
#
# Distributed under terms of the GNU AGPLv3 license.
from ..utils import hexlify
2017-11-01 19:55:35 +00:00
from .apitestbase import ApiTestBase
2019-06-29 15:25:44 +00:00
2017-11-01 19:55:35 +00:00
class UserTestCase(ApiTestBase):
def test_get_user(self):
# missing username
2019-06-29 15:25:44 +00:00
self._make_request("getUser", error=10)
2017-11-01 19:55:35 +00:00
# non-existent user
2019-06-29 15:25:44 +00:00
self._make_request("getUser", {"username": "non existent"}, error=70)
2017-11-01 19:55:35 +00:00
# self
2019-06-29 15:25:44 +00:00
rv, child = self._make_request("getUser", {"username": "alice"}, tag="user")
self.assertEqual(child.get("username"), "alice")
self.assertEqual(child.get("adminRole"), "true")
2017-11-01 19:55:35 +00:00
# other
2019-06-29 15:25:44 +00:00
rv, child = self._make_request("getUser", {"username": "bob"}, tag="user")
self.assertEqual(child.get("username"), "bob")
self.assertEqual(child.get("adminRole"), "false")
2017-11-01 19:55:35 +00:00
# self from non-admin
2019-06-29 15:25:44 +00:00
rv, child = self._make_request(
"getUser", {"u": "bob", "p": "B0b", "username": "bob"}, tag="user"
)
self.assertEqual(child.get("username"), "bob")
self.assertEqual(child.get("adminRole"), "false")
2017-11-01 19:55:35 +00:00
# other from non-admin
2019-06-29 15:25:44 +00:00
self._make_request(
"getUser", {"u": "bob", "p": "B0b", "username": "alice"}, error=50
)
2017-11-01 19:55:35 +00:00
def test_get_users(self):
# non-admin
2019-06-29 15:25:44 +00:00
self._make_request("getUsers", {"u": "bob", "p": "B0b"}, error=50)
2017-11-01 19:55:35 +00:00
# admin
2019-06-29 15:25:44 +00:00
rv, child = self._make_request("getUsers", tag="users")
2017-11-01 19:55:35 +00:00
self.assertEqual(len(child), 2)
self.assertIsNotNone(self._find(child, "./user[@username='alice']"))
self.assertIsNotNone(self._find(child, "./user[@username='bob']"))
def test_create_user(self):
# non admin
2019-06-29 15:25:44 +00:00
self._make_request("createUser", {"u": "bob", "p": "B0b"}, error=50)
2017-11-01 19:55:35 +00:00
# missing params, testing every combination, maybe overkill
2019-06-29 15:25:44 +00:00
self._make_request("createUser", error=10)
self._make_request("createUser", {"username": "user"}, error=10)
self._make_request("createUser", {"password": "pass"}, error=10)
self._make_request("createUser", {"email": "email@example.com"}, error=10)
self._make_request(
"createUser", {"username": "user", "password": "pass"}, error=10
)
self._make_request(
"createUser", {"username": "user", "email": "email@example.com"}, error=10
)
self._make_request(
"createUser", {"password": "pass", "email": "email@example.com"}, error=10
)
2017-11-01 19:55:35 +00:00
# duplicate
2019-06-29 15:25:44 +00:00
self._make_request(
"createUser",
{"username": "bob", "password": "pass", "email": "me@bob.com"},
error=0,
)
2017-11-01 19:55:35 +00:00
# test we only got our two initial users
2019-06-29 15:25:44 +00:00
rv, child = self._make_request("getUsers", tag="users")
2017-11-01 19:55:35 +00:00
self.assertEqual(len(child), 2)
# create users
2019-06-29 15:25:44 +00:00
self._make_request(
"createUser",
{
"username": "charlie",
"password": "Ch4rl1e",
"email": "unicorn@example.com",
"adminRole": True,
},
skip_post=True,
)
rv, child = self._make_request("getUser", {"username": "charlie"}, tag="user")
self.assertEqual(child.get("username"), "charlie")
self.assertEqual(child.get("email"), "unicorn@example.com")
self.assertEqual(child.get("adminRole"), "true")
self._make_request(
"createUser",
{"username": "dave", "password": "Dav3", "email": "dave@example.com"},
skip_post=True,
)
rv, child = self._make_request("getUser", {"username": "dave"}, tag="user")
self.assertEqual(child.get("username"), "dave")
self.assertEqual(child.get("email"), "dave@example.com")
self.assertEqual(child.get("adminRole"), "false")
rv, child = self._make_request("getUsers", tag="users")
2017-11-01 19:55:35 +00:00
self.assertEqual(len(child), 4)
def test_delete_user(self):
# non admin
2019-06-29 15:25:44 +00:00
self._make_request(
"deleteUser", {"u": "bob", "p": "B0b", "username": "alice"}, error=50
)
2017-11-01 19:55:35 +00:00
# missing param
2019-06-29 15:25:44 +00:00
self._make_request("deleteUser", error=10)
2017-11-01 19:55:35 +00:00
# non existing
2019-06-29 15:25:44 +00:00
self._make_request("deleteUser", {"username": "charlie"}, error=70)
2017-11-01 19:55:35 +00:00
# test we still got our two initial users
2019-06-29 15:25:44 +00:00
rv, child = self._make_request("getUsers", tag="users")
2017-11-01 19:55:35 +00:00
self.assertEqual(len(child), 2)
# delete user
2019-06-29 15:25:44 +00:00
self._make_request("deleteUser", {"username": "bob"}, skip_post=True)
rv, child = self._make_request("getUsers", tag="users")
2017-11-01 19:55:35 +00:00
self.assertEqual(len(child), 1)
def test_change_password(self):
# missing parameter
2019-06-29 15:25:44 +00:00
self._make_request("changePassword", error=10)
self._make_request("changePassword", {"username": "alice"}, error=10)
self._make_request("changePassword", {"password": "newpass"}, error=10)
2017-11-01 19:55:35 +00:00
# admin change self
2019-06-29 15:25:44 +00:00
self._make_request(
"changePassword",
{"username": "alice", "password": "newpass"},
skip_post=True,
)
self._make_request("ping", error=40)
self._make_request("ping", {"u": "alice", "p": "newpass"})
self._make_request(
"changePassword",
{"u": "alice", "p": "newpass", "username": "alice", "password": "Alic3"},
skip_post=True,
)
2017-11-01 19:55:35 +00:00
# admin change other
2019-06-29 15:25:44 +00:00
self._make_request(
"changePassword", {"username": "bob", "password": "newbob"}, skip_post=True
)
self._make_request("ping", {"u": "bob", "p": "B0b"}, error=40)
self._make_request("ping", {"u": "bob", "p": "newbob"})
2017-11-01 19:55:35 +00:00
# non-admin change self
2019-06-29 15:25:44 +00:00
self._make_request(
"changePassword",
{"u": "bob", "p": "newbob", "username": "bob", "password": "B0b"},
skip_post=True,
)
self._make_request("ping", {"u": "bob", "p": "newbob"}, error=40)
self._make_request("ping", {"u": "bob", "p": "B0b"})
2017-11-01 19:55:35 +00:00
# non-admin change other
2019-06-29 15:25:44 +00:00
self._make_request(
"changePassword",
{"u": "bob", "p": "B0b", "username": "alice", "password": "newpass"},
skip_post=True,
error=50,
)
self._make_request("ping", {"u": "alice", "p": "newpass"}, error=40)
self._make_request("ping")
2017-11-01 19:55:35 +00:00
# change non existing
2019-06-29 15:25:44 +00:00
self._make_request(
"changePassword", {"username": "nonexsistent", "password": "pass"}, error=70
)
2017-11-01 19:55:35 +00:00
# non ASCII chars
2019-06-29 15:25:44 +00:00
self._make_request(
"changePassword",
{"username": "alice", "password": "новыйпароль"},
skip_post=True,
)
self._make_request("ping", {"u": "alice", "p": "новыйпароль"})
self._make_request(
"changePassword",
{
"username": "alice",
"password": "Alic3",
"u": "alice",
"p": "новыйпароль",
},
skip_post=True,
)
2017-11-01 19:55:35 +00:00
# non ASCII in hex encoded password
2019-06-29 15:25:44 +00:00
self._make_request(
"changePassword",
{"username": "alice", "password": "enc:" + hexlify(u"новыйпароль")},
skip_post=True,
)
self._make_request("ping", {"u": "alice", "p": "новыйпароль"})
2017-11-01 19:55:35 +00:00
# new password starting with 'enc:' followed by non hex chars
2019-06-29 15:25:44 +00:00
self._make_request(
"changePassword",
{
"username": "alice",
"password": "enc:randomstring",
"u": "alice",
"p": "новыйпароль",
},
skip_post=True,
)
self._make_request("ping", {"u": "alice", "p": "enc:randomstring"})
if __name__ == "__main__":
2017-11-01 19:55:35 +00:00
unittest.main()