diff --git a/API_Interfaces.txt b/API_Interfaces.txt index cc876a7..7afadc5 100644 --- a/API_Interfaces.txt +++ b/API_Interfaces.txt @@ -32,23 +32,26 @@ UserAPI (api/user) ######################## POST -> Create a user if it not already exists In: - CASid = Login of the user caught from the CAS authentication + CASid = Login of the user caught from the CAS authentication (must be unique) role = Role of the user (can be concatenated with -) 1=secrétaire, 2=resp_formation, 3=tuteur_univ, 4=étudiant Out: 200 -> UID = : The user already exists with the id USER_ID 201 -> UID = : The user has been successfully created with the id USER_ID 400 -> ERROR = "One or more parameters are missing" : Bad request + 405 -> ERROR = "A user with this email already exists !" : A user with this email already exists PUT -> Modify an existing user In: (Suffix = /byuid/) - CASid = Login of the user caught from the CAS authentication + CASid = Login of the user caught from the CAS authentication (must be unique) role = Role of the user (can be concatenated with -) 1=secrétaire, 2=resp_formation, 3=tuteur_univ, 4=étudiant phone = Phone number of the user (00.00.00.00.00) - email = Email of the user + email = Email of the user (must be unique) Out: 200 -> UID = : The user has been modified sucessfully with the id USER_ID 400 -> ERROR = "One or more parameters are missing !" : Bad request 405 -> ERROR = "This user doesn't exists !" : Bad USER_ID provided + 405 -> ERROR = "A user with this CASid (login) already exists !" : A user with this login already exists + 405 -> ERROR = "A user with this email already exists !" : A user with this email already exists GET -> Getting specified user infos In: (Suffixes = /byuid/ | /bylogin/ | /byemail/) diff --git a/backend/app/api/GroupAPI.py b/backend/app/api/GroupAPI.py index 172ce89..6869191 100644 --- a/backend/app/api/GroupAPI.py +++ b/backend/app/api/GroupAPI.py @@ -74,7 +74,7 @@ class GroupAPI(Resource): return {"ERROR": "This group does not exists !"}, 405 group = getGroup(name=name) - if group is None: + if group is not None: return {"ERROR": "A group with this name already exists !"}, 405 user = getUser(uid=resp_id) diff --git a/backend/app/api/UserAPI.py b/backend/app/api/UserAPI.py index 2560302..24369e5 100644 --- a/backend/app/api/UserAPI.py +++ b/backend/app/api/UserAPI.py @@ -22,6 +22,9 @@ class UserAPI(Resource): if user is not None: return {"UID": user["id"]}, 200 + if getUser(email=email) is not None: + return {"ERROR": "A user with this email (" + email + ") already exists !"}, 405 + query = USER.insert().values(login=CASid, email=email, role=role, phone=phone) res = query.execute() return {"UID": res.lastrowid}, 201 @@ -31,13 +34,20 @@ class UserAPI(Resource): if not checkParams(['CASid', 'role', 'email', 'phone'], args): return {"ERROR": "One or more parameters are missing !"}, 400 - if getUser(uid=uid) is None: - return {"ERROR": "This user doesn't exists !"}, 405 - CASid = args['CASid'] role = args['role'] email = args['email'] phone = args['phone'] + + if getUser(uid=uid) is None: + return {"ERROR": "This user doesn't exists !"}, 405 + + if getUser(login=CASid) is not None: + return {"ERROR": "A user with this CASid (login) already exists !"}, 405 + + if getUser(email=email) is not None: + return {"ERROR": "A user with this email already exists !"}, 405 + query = USER.update().values(login=CASid, email=email, role=role, phone=phone).where(USER.c.id == uid) query.execute() return {"UID": uid}, 200 @@ -52,4 +62,4 @@ class UserAPI(Resource): @staticmethod def getEmailFromCAS(CASid): - return "" + return CASid + "@ola.com" diff --git a/backend/tests/api/test_User.py b/backend/tests/api/test_User.py new file mode 100644 index 0000000..fad5c0a --- /dev/null +++ b/backend/tests/api/test_User.py @@ -0,0 +1,94 @@ +import unittest + +from flask import json + +from app.core import app +from app.model import USER, user_class + + +class UserTestCase(unittest.TestCase): + uid = None + gid = None + tid = None + + @classmethod + def tearDownClass(cls): + query = USER.delete().where(user_class.login == "admin") + query.execute() + query = USER.delete().where(user_class.login == "admin2") + query.execute() + + def setUp(self): + self.app = app.test_client() + + def tearDown(self): + pass + + def create_user(self, login, role): + return self.app.post('/api/user', + data=json.dumps( + dict( + CASid=login, + role=role + ) + ), content_type='application/json') + + def getUserByID(self, UID): + return self.app.get('/api/user/byuid/' + str(UID)) + + def getUserByLogin(self, login): + return self.app.get('/api/user/bylogin/' + login) + + def getUserByEmail(self, email): + return self.app.get('/api/user/byemail/' + email) + + def change_user(self, UID, login, role, email, phone): + return self.app.put('/api/user/byuid/' + str(UID), + data=json.dumps( + dict( + CASid=login, + role=role, + email=email, + phone=phone + ) + ), content_type='application/json') + + def test_user(self): + rv = self.create_user('admin', '4') + self.assertEqual(rv.status_code, 201, 'Creating user Failed') + uid = json.loads(rv.data)['UID'] + self.assertIsNotNone(uid) + + rv = self.create_user('admin', '4') + self.assertEqual(rv.status_code, 200, 'User is supposed to already exist') + uid2 = json.loads(rv.data)['UID'] + self.assertEqual(uid, uid2, "The UID must be the same !") + + rv = self.getUserByID(uid) + self.assertEqual(rv.status_code, 200, 'Getting user failed by ID') + user = json.loads(rv.data)['USER'] + self.assertIsNotNone(user) + + rv = self.getUserByLogin("admin") + self.assertEqual(rv.status_code, 200, 'Getting user failed by Login') + user2 = json.loads(rv.data)['USER'] + self.assertEqual(user, user2, "User by login must be the same !") + + rv = self.getUserByEmail("admin@ola.com") + self.assertEqual(rv.status_code, 200, 'Getting user failed by email') + user3 = json.loads(rv.data)['USER'] + self.assertEqual(user, user3, "User by email must be the same !") + + rv = self.change_user(uid, 'adminx', '3', 'adminx@email.com', '11.11.11.11.11') + self.assertEqual(rv.status_code, 200, 'User modification failed !') + uid3 = json.loads(rv.data)['UID'] + self.assertEqual(uid, uid3, "UIDs doesn't match !") + + rv = self.getUserByLogin("adminx") + self.assertEqual(rv.status_code, 200, 'Getting modified user failed by Login') + user4 = json.loads(rv.data)['USER'] + self.assertIsNotNone(user4, "Modified user shouldn't be None !") + + +if __name__ == '__main__': + unittest.main()