This repository has been archived on 2021-09-15. You can view files and clone it, but cannot push or open issues or pull requests.
M2OLA/backend/tests/api/test_Auth.py

88 lines
3.3 KiB
Python
Raw Normal View History

2017-01-24 10:42:19 +01:00
import unittest
from flask import json
from app.core import app
2017-01-24 13:28:05 +01:00
from app.model import USER, getUser, GROUP, TUTORSHIP, tutorship_class, group_class, user_class
2017-01-24 10:42:19 +01:00
class AuthTestCase(unittest.TestCase):
2017-01-24 13:28:05 +01:00
uid = None
gid = None
tid = None
2017-01-24 10:42:19 +01:00
@classmethod
def setUpClass(cls):
2017-01-24 11:45:07 +01:00
if getUser(login="admin") is None:
query = USER.insert().values(login="admin", email="admin@admin.com", role="4", phone="00.00.00.00.00")
2017-01-24 13:28:05 +01:00
res = query.execute()
cls.uid = res.lastrowid
query = GROUP.insert().values(name="test", year="2017", class_long="classe toto", class_short="toto",
2017-01-27 12:05:02 +01:00
department="plop", ressources_dir="/plop/toto", resp_id=cls.uid,
sec_id=cls.uid)
2017-01-24 13:28:05 +01:00
res = query.execute()
cls.gid = res.lastrowid
query = TUTORSHIP.insert().values(student_id=cls.uid, ptutor_id=cls.uid, group_id=cls.gid)
res = query.execute()
cls.tid = res.lastrowid
2017-01-24 10:42:19 +01:00
@classmethod
def tearDownClass(cls):
2017-01-24 13:28:05 +01:00
if cls.uid is not None and cls.gid is not None and cls.tid is not None:
query = TUTORSHIP.delete().where(tutorship_class.id == cls.tid)
query.execute()
query = GROUP.delete().where(group_class.id == cls.gid)
query.execute()
query = USER.delete().where(user_class.id == cls.uid)
query.execute()
2017-01-24 10:42:19 +01:00
def setUp(self):
self.app = app.test_client()
def tearDown(self):
pass
def login(self, email, password):
return self.app.post('/api/login',
data=json.dumps(
dict(
email=email,
2017-01-24 10:42:19 +01:00
password=password
)
), content_type='application/json')
2017-01-26 17:16:23 +01:00
def getUserInfo(self):
return self.app.get('/api/userInfo')
2017-01-24 10:42:19 +01:00
def logout(self):
return self.app.delete('/api/login')
def test_login_logout(self):
rv = self.login('admin@admin.com', 'admin@admin.com')
2017-01-24 10:42:19 +01:00
self.assertEqual(rv.status_code, 200, 'Login as admin Failed')
rv = self.login('admin@admin.com', 'admin@admin.com')
2017-01-26 17:16:23 +01:00
self.assertEqual(rv.status_code, 201, 'Login as admin succeed but should have already been done')
rv = self.getUserInfo()
self.assertEqual(rv.status_code, 200, 'Getting user info failed')
self.assertEqual({"id": getUser(login="admin")["id"], "login": "admin", "email": "admin@admin.com", "role": "4",
2017-01-26 17:16:23 +01:00
"phone": "00.00.00.00.00"}, json.loads(rv.data)['USER'], 'Invalid user info')
2017-01-24 10:42:19 +01:00
rv = self.logout()
self.assertEqual(rv.status_code, 200, 'Logout Failed')
rv = self.login('adminx@admin.com', 'admin@admin.com')
self.assertEqual(rv.status_code, 401, 'Authentication not failed for the invalid user!')
2017-01-24 10:42:19 +01:00
2017-01-26 17:16:23 +01:00
rv = self.getUserInfo()
self.assertEqual(rv.status_code, 200, 'Getting user info failed')
self.assertIsNone(json.loads(rv.data)['USER'], 'User info should be None')
rv = self.login('admin@admin.com', 'admin@admin.comx')
2017-01-24 10:42:19 +01:00
self.assertEqual(rv.status_code, 401,
'Authenticationnot failed for the invalid password !')
2017-01-24 10:42:19 +01:00
if __name__ == '__main__':
unittest.main()