2018-01-23 22:13:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import time
|
2018-01-25 11:41:24 +00:00
|
|
|
|
2018-02-28 22:14:35 +00:00
|
|
|
from .helper import DashboardTestCase
|
2018-01-23 22:13:57 +00:00
|
|
|
|
2018-01-26 12:11:21 +00:00
|
|
|
|
2018-02-28 22:14:35 +00:00
|
|
|
class AuthTest(DashboardTestCase):
|
2018-04-24 16:32:54 +00:00
|
|
|
|
|
|
|
AUTO_AUTHENTICATE = False
|
|
|
|
|
2018-01-26 12:12:16 +00:00
|
|
|
def setUp(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self.reset_session()
|
2018-01-26 12:12:16 +00:00
|
|
|
|
|
|
|
def test_a_set_login_credentials(self):
|
2018-04-24 16:32:54 +00:00
|
|
|
self.create_user('admin2', 'admin2', ['administrator'])
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin2', 'password': 'admin2'})
|
|
|
|
self.assertStatus(201)
|
2018-06-01 11:15:49 +00:00
|
|
|
# self.assertJsonBody({"username": "admin2"})
|
|
|
|
data = self.jsonBody()
|
|
|
|
self.assertIn('username', data)
|
|
|
|
self.assertEqual(data['username'], "admin2")
|
|
|
|
self.assertIn('permissions', data)
|
|
|
|
for scope, perms in data['permissions'].items():
|
|
|
|
self.assertIsNotNone(scope)
|
|
|
|
self.assertIn('read', perms)
|
|
|
|
self.assertIn('update', perms)
|
|
|
|
self.assertIn('create', perms)
|
|
|
|
self.assertIn('delete', perms)
|
2018-04-24 16:32:54 +00:00
|
|
|
self.delete_user('admin2')
|
2018-01-26 12:12:16 +00:00
|
|
|
|
2018-01-23 22:13:57 +00:00
|
|
|
def test_login_valid(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
|
|
|
|
self.assertStatus(201)
|
2018-06-01 11:15:49 +00:00
|
|
|
data = self.jsonBody()
|
|
|
|
self.assertIn('username', data)
|
|
|
|
self.assertEqual(data['username'], "admin")
|
|
|
|
self.assertIn('permissions', data)
|
|
|
|
for scope, perms in data['permissions'].items():
|
|
|
|
self.assertIsNotNone(scope)
|
|
|
|
self.assertIn('read', perms)
|
|
|
|
self.assertIn('update', perms)
|
|
|
|
self.assertIn('create', perms)
|
|
|
|
self.assertIn('delete', perms)
|
2018-01-27 10:55:29 +00:00
|
|
|
|
|
|
|
def test_login_stay_signed_in(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {
|
|
|
|
'username': 'admin',
|
|
|
|
'password': 'admin',
|
|
|
|
'stay_signed_in': True})
|
|
|
|
self.assertStatus(201)
|
2018-02-28 22:14:35 +00:00
|
|
|
self.assertIn('session_id', self.cookies())
|
2018-02-08 08:24:01 +00:00
|
|
|
for cookie in self.cookies():
|
2018-02-28 22:14:35 +00:00
|
|
|
if cookie.name == 'session_id':
|
2018-02-08 08:24:01 +00:00
|
|
|
self.assertIsNotNone(cookie.expires)
|
2018-01-27 10:55:29 +00:00
|
|
|
|
|
|
|
def test_login_not_stay_signed_in(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {
|
|
|
|
'username': 'admin',
|
|
|
|
'password': 'admin',
|
|
|
|
'stay_signed_in': False})
|
|
|
|
self.assertStatus(201)
|
2018-02-28 22:14:35 +00:00
|
|
|
self.assertIn('session_id', self.cookies())
|
2018-02-08 08:24:01 +00:00
|
|
|
for cookie in self.cookies():
|
2018-02-28 22:14:35 +00:00
|
|
|
if cookie.name == 'session_id':
|
2018-02-08 08:24:01 +00:00
|
|
|
self.assertIsNone(cookie.expires)
|
2018-01-23 22:13:57 +00:00
|
|
|
|
|
|
|
def test_login_invalid(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'inval'})
|
2018-06-04 20:26:44 +00:00
|
|
|
self.assertStatus(400)
|
|
|
|
self.assertJsonBody({
|
|
|
|
"component": "auth",
|
|
|
|
"code": "invalid_credentials",
|
|
|
|
"detail": "Invalid credentials"
|
|
|
|
})
|
2018-01-23 22:13:57 +00:00
|
|
|
|
2018-09-17 11:03:37 +00:00
|
|
|
def test_login_without_password(self):
|
|
|
|
self.create_user('admin2', '', ['administrator'])
|
|
|
|
self._post("/api/auth", {'username': 'admin2', 'password': ''})
|
|
|
|
self.assertStatus(400)
|
|
|
|
self.assertJsonBody({
|
|
|
|
"component": "auth",
|
|
|
|
"code": "invalid_credentials",
|
|
|
|
"detail": "Invalid credentials"
|
|
|
|
})
|
|
|
|
self.delete_user('admin2')
|
|
|
|
|
2018-01-23 22:13:57 +00:00
|
|
|
def test_logout(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
|
|
|
|
self._delete("/api/auth")
|
|
|
|
self.assertStatus(204)
|
|
|
|
self.assertBody('')
|
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|
2018-01-23 22:13:57 +00:00
|
|
|
|
|
|
|
def test_session_expire(self):
|
2018-03-23 10:25:48 +00:00
|
|
|
self._ceph_cmd(['dashboard', 'set-session-expire', '2'])
|
2018-02-08 08:24:01 +00:00
|
|
|
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
|
|
|
|
self.assertStatus(201)
|
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(200)
|
|
|
|
time.sleep(3)
|
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|
2018-03-23 10:25:48 +00:00
|
|
|
self._ceph_cmd(['dashboard', 'set-session-expire', '1200'])
|
2018-01-23 22:13:57 +00:00
|
|
|
|
|
|
|
def test_unauthorized(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._get("/api/host")
|
|
|
|
self.assertStatus(401)
|