TG-60 : TU sur GroupAPI + corrections
This commit is contained in:
parent
2e21619ff5
commit
793544e300
@ -2,7 +2,6 @@ import os
|
||||
|
||||
from flask_restful import Resource, request
|
||||
|
||||
from app.config import Config
|
||||
from app.core import app
|
||||
from app.model import *
|
||||
from app.utils import checkParams
|
||||
@ -25,7 +24,7 @@ class GroupAPI(Resource):
|
||||
department = args['department']
|
||||
resp_id = args['resp_id']
|
||||
sec_id = args['sec_id']
|
||||
res_dir = Config.BASE_RESSOURCES_DIR + name + "/"
|
||||
res_dir = app.config['BASE_RESSOURCES_DIR'] + name + "/"
|
||||
|
||||
group = getGroup(name=name)
|
||||
if group is not None:
|
||||
@ -73,8 +72,8 @@ class GroupAPI(Resource):
|
||||
if group is None:
|
||||
return {"ERROR": "This group does not exists !"}, 405
|
||||
|
||||
group = getGroup(name=name)
|
||||
if group is not None:
|
||||
group2 = getGroup(name=name)
|
||||
if group2 is not None:
|
||||
return {"ERROR": "A group with this name already exists !"}, 405
|
||||
|
||||
user = getUser(uid=resp_id)
|
||||
@ -99,7 +98,10 @@ class GroupAPI(Resource):
|
||||
department=department, resp_id=resp_id, sec_id=sec_id, ressources_dir=res_dir) \
|
||||
.where(GROUP.c.id == gid)
|
||||
res = query.execute()
|
||||
os.mkdir(res_dir)
|
||||
|
||||
if group["ressources_dir"] != res_dir:
|
||||
os.rename(group["ressources_dir"], res_dir)
|
||||
|
||||
return {"GID": gid}, 200
|
||||
|
||||
def get(self, gid=0, name=""):
|
||||
|
@ -27,7 +27,9 @@ def getUser(uid=0, login="", email=""):
|
||||
raise Exception("getUser must be called with one argument !")
|
||||
else:
|
||||
if uid != 0:
|
||||
res = db.session.query(user_class).get(uid)
|
||||
query = USER.select(USER.c.id == uid)
|
||||
rows = query.execute()
|
||||
res = rows.first()
|
||||
|
||||
elif login != "":
|
||||
query = USER.select(USER.c.login == login)
|
||||
@ -59,7 +61,9 @@ def getGroup(gid=0, name=""):
|
||||
raise Exception("getUser must be called with one argument !")
|
||||
else:
|
||||
if gid != 0:
|
||||
res = db.session.query(group_class).get(gid)
|
||||
query = GROUP.select(GROUP.c.id == gid)
|
||||
rows = query.execute()
|
||||
res = rows.first()
|
||||
|
||||
elif name != "":
|
||||
query = GROUP.select(GROUP.c.name == name)
|
||||
|
106
backend/tests/api/test_Group.py
Normal file
106
backend/tests/api/test_Group.py
Normal file
@ -0,0 +1,106 @@
|
||||
import unittest
|
||||
|
||||
from flask import json
|
||||
|
||||
from app.core import app
|
||||
from app.model import USER, user_class, GROUP, group_class
|
||||
|
||||
|
||||
class GroupTestCase(unittest.TestCase):
|
||||
uid = None
|
||||
uid2 = None
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
query = USER.insert().values(login="admin", email="admin@admin.com", role="4", phone="00.00.00.00.00")
|
||||
res = query.execute()
|
||||
cls.uid = res.lastrowid
|
||||
query = USER.insert().values(login="adminx", email="adminx@admin.com", role="3", phone="00.00.00.00.00")
|
||||
res = query.execute()
|
||||
cls.uid2 = res.lastrowid
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
query = GROUP.delete().where(group_class.name == "group_test")
|
||||
query.execute()
|
||||
query = GROUP.delete().where(group_class.name == "group_test2")
|
||||
query.execute()
|
||||
query = USER.delete().where(user_class.login == "admin")
|
||||
query.execute()
|
||||
query = USER.delete().where(user_class.login == "adminx")
|
||||
query.execute()
|
||||
|
||||
def setUp(self):
|
||||
self.app = app.test_client()
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def create_group(self, name, year, class_short, class_long, department, resp_id, sec_id):
|
||||
return self.app.post('/api/group',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
name=name,
|
||||
year=year,
|
||||
class_short=class_short,
|
||||
class_long=class_long,
|
||||
department=department,
|
||||
resp_id=resp_id,
|
||||
sec_id=sec_id
|
||||
)
|
||||
), content_type='application/json')
|
||||
|
||||
def getGroupByID(self, GID):
|
||||
return self.app.get('/api/group/bygid/' + str(GID))
|
||||
|
||||
def getGroupByName(self, name):
|
||||
return self.app.get('/api/group/byname/' + name)
|
||||
|
||||
def change_group(self, GID, name, year, class_short, class_long, department, resp_id, sec_id):
|
||||
return self.app.put('/api/group/bygid/' + str(GID),
|
||||
data=json.dumps(
|
||||
dict(
|
||||
name=name,
|
||||
year=year,
|
||||
class_short=class_short,
|
||||
class_long=class_long,
|
||||
department=department,
|
||||
resp_id=resp_id,
|
||||
sec_id=sec_id
|
||||
)
|
||||
), content_type='application/json')
|
||||
|
||||
def test_group(self):
|
||||
rv = self.create_group('group_test', '2017', 'GT', 'GROUP_TEST', 'TESTING', self.uid, self.uid2)
|
||||
self.assertEqual(rv.status_code, 201, 'Creating group Failed')
|
||||
gid = json.loads(rv.data)['GID']
|
||||
self.assertIsNotNone(gid)
|
||||
|
||||
rv = self.create_group('group_test', '2017', 'GT', 'GROUP_TEST', 'TESTING', self.uid, self.uid2)
|
||||
self.assertEqual(rv.status_code, 200, 'Group is supposed to already exist')
|
||||
gid2 = json.loads(rv.data)['GID']
|
||||
self.assertEqual(gid, gid2, "The GID must be the same !")
|
||||
|
||||
rv = self.getGroupByID(gid)
|
||||
self.assertEqual(rv.status_code, 200, 'Getting group failed by ID')
|
||||
group = json.loads(rv.data)['GROUP']
|
||||
self.assertIsNotNone(group)
|
||||
|
||||
rv = self.getGroupByName("group_test")
|
||||
self.assertEqual(rv.status_code, 200, 'Getting group failed by Name')
|
||||
group2 = json.loads(rv.data)['GROUP']
|
||||
self.assertEqual(group, group2, "Group by name must be the same !")
|
||||
|
||||
rv = self.change_group(gid, 'group_test2', '2018', 'GT2', 'GROUP_TEST2', 'TESTING2', self.uid2, self.uid)
|
||||
self.assertEqual(rv.status_code, 200, 'Group modification failed !')
|
||||
gid3 = json.loads(rv.data)['GID']
|
||||
self.assertEqual(gid, gid3, "GIDs doesn't match !")
|
||||
|
||||
rv = self.getGroupByName('group_test2')
|
||||
self.assertEqual(rv.status_code, 200, 'Getting modified group failed by Name')
|
||||
group4 = json.loads(rv.data)['GROUP']
|
||||
self.assertIsNotNone(group4, "Modified group shouldn't be None !")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -15,7 +15,7 @@ class UserTestCase(unittest.TestCase):
|
||||
def tearDownClass(cls):
|
||||
query = USER.delete().where(user_class.login == "admin")
|
||||
query.execute()
|
||||
query = USER.delete().where(user_class.login == "admin2")
|
||||
query = USER.delete().where(user_class.login == "adminx")
|
||||
query.execute()
|
||||
|
||||
def setUp(self):
|
||||
|
Reference in New Issue
Block a user