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-01-26 12:11:21 +00:00
|
|
|
from .helper import ControllerTestCase
|
2018-01-27 10:55:29 +00:00
|
|
|
from ..tools import Session
|
2018-01-23 22:13:57 +00:00
|
|
|
|
2018-01-26 12:11:21 +00:00
|
|
|
|
|
|
|
class AuthTest(ControllerTestCase):
|
2018-01-26 12:12:16 +00:00
|
|
|
def setUp(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self.reset_session()
|
|
|
|
self._ceph_cmd(['dashboard', 'set-session-expire', '2'])
|
|
|
|
self._ceph_cmd(['dashboard', 'set-login-credentials', 'admin', 'admin'])
|
2018-01-26 12:12:16 +00:00
|
|
|
|
|
|
|
def test_a_set_login_credentials(self):
|
2018-02-08 08:24:01 +00:00
|
|
|
self._ceph_cmd(['dashboard', 'set-login-credentials', 'admin2', 'admin2'])
|
|
|
|
self._post("/api/auth", {'username': 'admin2', 'password': 'admin2'})
|
|
|
|
self.assertStatus(201)
|
|
|
|
self.assertJsonBody({"username": "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)
|
|
|
|
self.assertJsonBody({"username": "admin"})
|
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)
|
|
|
|
self.assertIn(Session.NAME, self.cookies())
|
|
|
|
for cookie in self.cookies():
|
|
|
|
if cookie.name == Session.NAME:
|
|
|
|
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)
|
|
|
|
self.assertIn(Session.NAME, self.cookies())
|
|
|
|
for cookie in self.cookies():
|
|
|
|
if cookie.name == Session.NAME:
|
|
|
|
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'})
|
|
|
|
self.assertStatus(403)
|
|
|
|
self.assertJsonBody({"detail": "Invalid credentials"})
|
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-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-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)
|
2018-02-21 14:46:40 +00:00
|
|
|
self._get("/api")
|
|
|
|
self.assertStatus(401)
|