test_user.py 79KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for /api/v2/users subpath endpoints.
  4. """
  5. from time import sleep
  6. import pytest
  7. import transaction
  8. from tracim_backend import models
  9. from tracim_backend.lib.core.content import ContentApi
  10. from tracim_backend.lib.core.user import UserApi
  11. from tracim_backend.lib.core.group import GroupApi
  12. from tracim_backend.lib.core.workspace import WorkspaceApi
  13. from tracim_backend.models import get_tm_session
  14. from tracim_backend.models.contents import ContentTypeLegacy as ContentType
  15. from tracim_backend.models.revision_protection import new_revision
  16. from tracim_backend.tests import FunctionalTest
  17. from tracim_backend.fixtures.content import Content as ContentFixtures
  18. from tracim_backend.fixtures.users_and_groups import Base as BaseFixture
  19. class TestUserRecentlyActiveContentEndpoint(FunctionalTest):
  20. """
  21. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/recently_active # nopep8
  22. """
  23. fixtures = [BaseFixture]
  24. def test_api__get_recently_active_content__ok__200__nominal_case(self):
  25. # init DB
  26. dbsession = get_tm_session(self.session_factory, transaction.manager)
  27. admin = dbsession.query(models.User) \
  28. .filter(models.User.email == 'admin@admin.admin') \
  29. .one()
  30. workspace_api = WorkspaceApi(
  31. current_user=admin,
  32. session=dbsession,
  33. config=self.app_config
  34. )
  35. workspace = WorkspaceApi(
  36. current_user=admin,
  37. session=dbsession,
  38. config=self.app_config,
  39. ).create_workspace(
  40. 'test workspace',
  41. save_now=True
  42. )
  43. workspace2 = WorkspaceApi(
  44. current_user=admin,
  45. session=dbsession,
  46. config=self.app_config,
  47. ).create_workspace(
  48. 'test workspace2',
  49. save_now=True
  50. )
  51. api = ContentApi(
  52. current_user=admin,
  53. session=dbsession,
  54. config=self.app_config,
  55. )
  56. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  57. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  58. # creation order test
  59. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  60. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  61. # update order test
  62. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  63. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  64. with new_revision(
  65. session=dbsession,
  66. tm=transaction.manager,
  67. content=firstly_created_but_recently_updated,
  68. ):
  69. firstly_created_but_recently_updated.description = 'Just an update'
  70. api.save(firstly_created_but_recently_updated)
  71. # comment change order
  72. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  73. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  74. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  75. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  76. dbsession.flush()
  77. transaction.commit()
  78. self.testapp.authorization = (
  79. 'Basic',
  80. (
  81. 'admin@admin.admin',
  82. 'admin@admin.admin'
  83. )
  84. )
  85. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), status=200) # nopep8
  86. res = res.json_body
  87. assert len(res) == 7
  88. for elem in res:
  89. assert isinstance(elem['content_id'], int)
  90. assert isinstance(elem['content_type'], str)
  91. assert elem['content_type'] != 'comments'
  92. assert isinstance(elem['is_archived'], bool)
  93. assert isinstance(elem['is_deleted'], bool)
  94. assert isinstance(elem['label'], str)
  95. assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
  96. assert isinstance(elem['show_in_ui'], bool)
  97. assert isinstance(elem['slug'], str)
  98. assert isinstance(elem['status'], str)
  99. assert isinstance(elem['sub_content_types'], list)
  100. for sub_content_type in elem['sub_content_types']:
  101. assert isinstance(sub_content_type, str)
  102. assert isinstance(elem['workspace_id'], int)
  103. # comment is newest than page2
  104. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  105. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  106. # last updated content is newer than other one despite creation
  107. # of the other is more recent
  108. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  109. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  110. # creation order is inverted here as last created is last active
  111. assert res[4]['content_id'] == secondly_created.content_id
  112. assert res[5]['content_id'] == firstly_created.content_id
  113. # folder subcontent modification does not change folder order
  114. assert res[6]['content_id'] == main_folder.content_id
  115. def test_api__get_recently_active_content__ok__200__limit_2_multiple(self):
  116. # TODO - G.M - 2018-07-20 - Better fix for this test, do not use sleep()
  117. # anymore to fix datetime lack of precision.
  118. # init DB
  119. dbsession = get_tm_session(self.session_factory, transaction.manager)
  120. admin = dbsession.query(models.User) \
  121. .filter(models.User.email == 'admin@admin.admin') \
  122. .one()
  123. workspace_api = WorkspaceApi(
  124. current_user=admin,
  125. session=dbsession,
  126. config=self.app_config
  127. )
  128. workspace = WorkspaceApi(
  129. current_user=admin,
  130. session=dbsession,
  131. config=self.app_config,
  132. ).create_workspace(
  133. 'test workspace',
  134. save_now=True
  135. )
  136. workspace2 = WorkspaceApi(
  137. current_user=admin,
  138. session=dbsession,
  139. config=self.app_config,
  140. ).create_workspace(
  141. 'test workspace2',
  142. save_now=True
  143. )
  144. api = ContentApi(
  145. current_user=admin,
  146. session=dbsession,
  147. config=self.app_config,
  148. )
  149. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  150. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  151. # creation order test
  152. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  153. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  154. # update order test
  155. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  156. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  157. with new_revision(
  158. session=dbsession,
  159. tm=transaction.manager,
  160. content=firstly_created_but_recently_updated,
  161. ):
  162. firstly_created_but_recently_updated.description = 'Just an update'
  163. api.save(firstly_created_but_recently_updated)
  164. # comment change order
  165. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  166. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  167. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  168. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  169. dbsession.flush()
  170. transaction.commit()
  171. self.testapp.authorization = (
  172. 'Basic',
  173. (
  174. 'admin@admin.admin',
  175. 'admin@admin.admin'
  176. )
  177. )
  178. params = {
  179. 'limit': 2,
  180. }
  181. res = self.testapp.get(
  182. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  183. status=200,
  184. params=params
  185. ) # nopep8
  186. res = res.json_body
  187. assert len(res) == 2
  188. for elem in res:
  189. assert isinstance(elem['content_id'], int)
  190. assert isinstance(elem['content_type'], str)
  191. assert elem['content_type'] != 'comments'
  192. assert isinstance(elem['is_archived'], bool)
  193. assert isinstance(elem['is_deleted'], bool)
  194. assert isinstance(elem['label'], str)
  195. assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
  196. assert isinstance(elem['show_in_ui'], bool)
  197. assert isinstance(elem['slug'], str)
  198. assert isinstance(elem['status'], str)
  199. assert isinstance(elem['sub_content_types'], list)
  200. for sub_content_type in elem['sub_content_types']:
  201. assert isinstance(sub_content_type, str)
  202. assert isinstance(elem['workspace_id'], int)
  203. # comment is newest than page2
  204. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  205. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  206. params = {
  207. 'limit': 2,
  208. 'before_content_id': secondly_created_but_not_commented.content_id, # nopep8
  209. }
  210. res = self.testapp.get(
  211. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  212. status=200,
  213. params=params
  214. )
  215. res = res.json_body
  216. assert len(res) == 2
  217. # last updated content is newer than other one despite creation
  218. # of the other is more recent
  219. assert res[0]['content_id'] == firstly_created_but_recently_updated.content_id
  220. assert res[1]['content_id'] == secondly_created_but_not_updated.content_id
  221. class TestUserReadStatusEndpoint(FunctionalTest):
  222. """
  223. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status # nopep8
  224. """
  225. def test_api__get_read_status__ok__200__all(self):
  226. # init DB
  227. dbsession = get_tm_session(self.session_factory, transaction.manager)
  228. admin = dbsession.query(models.User) \
  229. .filter(models.User.email == 'admin@admin.admin') \
  230. .one()
  231. workspace_api = WorkspaceApi(
  232. current_user=admin,
  233. session=dbsession,
  234. config=self.app_config
  235. )
  236. workspace = WorkspaceApi(
  237. current_user=admin,
  238. session=dbsession,
  239. config=self.app_config,
  240. ).create_workspace(
  241. 'test workspace',
  242. save_now=True
  243. )
  244. workspace2 = WorkspaceApi(
  245. current_user=admin,
  246. session=dbsession,
  247. config=self.app_config,
  248. ).create_workspace(
  249. 'test workspace2',
  250. save_now=True
  251. )
  252. api = ContentApi(
  253. current_user=admin,
  254. session=dbsession,
  255. config=self.app_config,
  256. )
  257. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  258. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  259. # creation order test
  260. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  261. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  262. # update order test
  263. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  264. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  265. with new_revision(
  266. session=dbsession,
  267. tm=transaction.manager,
  268. content=firstly_created_but_recently_updated,
  269. ):
  270. firstly_created_but_recently_updated.description = 'Just an update'
  271. api.save(firstly_created_but_recently_updated)
  272. # comment change order
  273. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  274. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  275. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  276. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  277. dbsession.flush()
  278. transaction.commit()
  279. self.testapp.authorization = (
  280. 'Basic',
  281. (
  282. 'admin@admin.admin',
  283. 'admin@admin.admin'
  284. )
  285. )
  286. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  287. res = res.json_body
  288. assert len(res) == 7
  289. for elem in res:
  290. assert isinstance(elem['content_id'], int)
  291. assert isinstance(elem['read_by_user'], bool)
  292. # comment is newest than page2
  293. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  294. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  295. # last updated content is newer than other one despite creation
  296. # of the other is more recent
  297. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  298. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  299. # creation order is inverted here as last created is last active
  300. assert res[4]['content_id'] == secondly_created.content_id
  301. assert res[5]['content_id'] == firstly_created.content_id
  302. # folder subcontent modification does not change folder order
  303. assert res[6]['content_id'] == main_folder.content_id
  304. def test_api__get_read_status__ok__200__nominal_case(self):
  305. # init DB
  306. dbsession = get_tm_session(self.session_factory, transaction.manager)
  307. admin = dbsession.query(models.User) \
  308. .filter(models.User.email == 'admin@admin.admin') \
  309. .one()
  310. workspace_api = WorkspaceApi(
  311. current_user=admin,
  312. session=dbsession,
  313. config=self.app_config
  314. )
  315. workspace = WorkspaceApi(
  316. current_user=admin,
  317. session=dbsession,
  318. config=self.app_config,
  319. ).create_workspace(
  320. 'test workspace',
  321. save_now=True
  322. )
  323. workspace2 = WorkspaceApi(
  324. current_user=admin,
  325. session=dbsession,
  326. config=self.app_config,
  327. ).create_workspace(
  328. 'test workspace2',
  329. save_now=True
  330. )
  331. api = ContentApi(
  332. current_user=admin,
  333. session=dbsession,
  334. config=self.app_config,
  335. )
  336. main_folder_workspace2 = api.create(ContentType.Folder, workspace2, None, 'Hepla', '', True) # nopep8
  337. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  338. # creation order test
  339. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  340. secondly_created = api.create(ContentType.Page, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  341. # update order test
  342. firstly_created_but_recently_updated = api.create(ContentType.Page, workspace, main_folder, 'update_order_test', '', True) # nopep8
  343. secondly_created_but_not_updated = api.create(ContentType.Page, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  344. with new_revision(
  345. session=dbsession,
  346. tm=transaction.manager,
  347. content=firstly_created_but_recently_updated,
  348. ):
  349. firstly_created_but_recently_updated.description = 'Just an update'
  350. api.save(firstly_created_but_recently_updated)
  351. # comment change order
  352. firstly_created_but_recently_commented = api.create(ContentType.Page, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  353. secondly_created_but_not_commented = api.create(ContentType.Page, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  354. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  355. content_workspace_2 = api.create(ContentType.Page, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  356. dbsession.flush()
  357. transaction.commit()
  358. self.testapp.authorization = (
  359. 'Basic',
  360. (
  361. 'admin@admin.admin',
  362. 'admin@admin.admin'
  363. )
  364. )
  365. selected_contents_id = [
  366. firstly_created_but_recently_commented.content_id,
  367. firstly_created_but_recently_updated.content_id,
  368. firstly_created.content_id,
  369. main_folder.content_id,
  370. ]
  371. url = '/api/v2/users/1/workspaces/{workspace_id}/contents/read_status?contents_ids={cid1}&contents_ids={cid2}&contents_ids={cid3}&contents_ids={cid4}'.format( # nopep8
  372. workspace_id=workspace.workspace_id,
  373. cid1=selected_contents_id[0],
  374. cid2=selected_contents_id[1],
  375. cid3=selected_contents_id[2],
  376. cid4=selected_contents_id[3],
  377. )
  378. res = self.testapp.get(
  379. url=url,
  380. status=200,
  381. )
  382. res = res.json_body
  383. assert len(res) == 4
  384. for elem in res:
  385. assert isinstance(elem['content_id'], int)
  386. assert isinstance(elem['read_by_user'], bool)
  387. # comment is newest than page2
  388. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  389. # last updated content is newer than other one despite creation
  390. # of the other is more recent
  391. assert res[1]['content_id'] == firstly_created_but_recently_updated.content_id
  392. # creation order is inverted here as last created is last active
  393. assert res[2]['content_id'] == firstly_created.content_id
  394. # folder subcontent modification does not change folder order
  395. assert res[3]['content_id'] == main_folder.content_id
  396. class TestUserSetContentAsRead(FunctionalTest):
  397. """
  398. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read # nopep8
  399. """
  400. def test_api_set_content_as_read__ok__200__nominal_case(self):
  401. # init DB
  402. dbsession = get_tm_session(self.session_factory, transaction.manager)
  403. admin = dbsession.query(models.User) \
  404. .filter(models.User.email == 'admin@admin.admin') \
  405. .one()
  406. workspace_api = WorkspaceApi(
  407. current_user=admin,
  408. session=dbsession,
  409. config=self.app_config
  410. )
  411. workspace = WorkspaceApi(
  412. current_user=admin,
  413. session=dbsession,
  414. config=self.app_config,
  415. ).create_workspace(
  416. 'test workspace',
  417. save_now=True
  418. )
  419. api = ContentApi(
  420. current_user=admin,
  421. session=dbsession,
  422. config=self.app_config,
  423. )
  424. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  425. # creation order test
  426. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  427. api.mark_unread(firstly_created)
  428. dbsession.flush()
  429. transaction.commit()
  430. self.testapp.authorization = (
  431. 'Basic',
  432. (
  433. 'admin@admin.admin',
  434. 'admin@admin.admin'
  435. )
  436. )
  437. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  438. assert res.json_body[0]['content_id'] == firstly_created.content_id
  439. assert res.json_body[0]['read_by_user'] is False
  440. self.testapp.put(
  441. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  442. workspace_id=workspace.workspace_id,
  443. content_id=firstly_created.content_id,
  444. user_id=admin.user_id,
  445. )
  446. )
  447. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  448. assert res.json_body[0]['content_id'] == firstly_created.content_id
  449. assert res.json_body[0]['read_by_user'] is True
  450. def test_api_set_content_as_read__ok__200__with_comments(self):
  451. # init DB
  452. dbsession = get_tm_session(self.session_factory, transaction.manager)
  453. admin = dbsession.query(models.User) \
  454. .filter(models.User.email == 'admin@admin.admin') \
  455. .one()
  456. workspace_api = WorkspaceApi(
  457. current_user=admin,
  458. session=dbsession,
  459. config=self.app_config
  460. )
  461. workspace = WorkspaceApi(
  462. current_user=admin,
  463. session=dbsession,
  464. config=self.app_config,
  465. ).create_workspace(
  466. 'test workspace',
  467. save_now=True
  468. )
  469. api = ContentApi(
  470. current_user=admin,
  471. session=dbsession,
  472. config=self.app_config,
  473. )
  474. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  475. # creation order test
  476. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  477. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  478. api.mark_unread(firstly_created)
  479. api.mark_unread(comments)
  480. dbsession.flush()
  481. transaction.commit()
  482. self.testapp.authorization = (
  483. 'Basic',
  484. (
  485. 'admin@admin.admin',
  486. 'admin@admin.admin'
  487. )
  488. )
  489. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  490. assert res.json_body[0]['content_id'] == firstly_created.content_id
  491. assert res.json_body[0]['read_by_user'] is False
  492. self.testapp.put(
  493. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  494. workspace_id=workspace.workspace_id,
  495. content_id=firstly_created.content_id,
  496. user_id=admin.user_id,
  497. )
  498. )
  499. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  500. assert res.json_body[0]['content_id'] == firstly_created.content_id
  501. assert res.json_body[0]['read_by_user'] is True
  502. # comment is also set as read
  503. assert comments.has_new_information_for(admin) is False
  504. class TestUserSetContentAsUnread(FunctionalTest):
  505. """
  506. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread # nopep8
  507. """
  508. def test_api_set_content_as_unread__ok__200__nominal_case(self):
  509. # init DB
  510. dbsession = get_tm_session(self.session_factory, transaction.manager)
  511. admin = dbsession.query(models.User) \
  512. .filter(models.User.email == 'admin@admin.admin') \
  513. .one()
  514. workspace_api = WorkspaceApi(
  515. current_user=admin,
  516. session=dbsession,
  517. config=self.app_config
  518. )
  519. workspace = WorkspaceApi(
  520. current_user=admin,
  521. session=dbsession,
  522. config=self.app_config,
  523. ).create_workspace(
  524. 'test workspace',
  525. save_now=True
  526. )
  527. api = ContentApi(
  528. current_user=admin,
  529. session=dbsession,
  530. config=self.app_config,
  531. )
  532. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  533. # creation order test
  534. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  535. api.mark_read(firstly_created)
  536. dbsession.flush()
  537. transaction.commit()
  538. self.testapp.authorization = (
  539. 'Basic',
  540. (
  541. 'admin@admin.admin',
  542. 'admin@admin.admin'
  543. )
  544. )
  545. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  546. assert res.json_body[0]['content_id'] == firstly_created.content_id
  547. assert res.json_body[0]['read_by_user'] is True
  548. self.testapp.put(
  549. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  550. workspace_id=workspace.workspace_id,
  551. content_id=firstly_created.content_id,
  552. user_id=admin.user_id,
  553. )
  554. )
  555. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  556. assert res.json_body[0]['content_id'] == firstly_created.content_id
  557. assert res.json_body[0]['read_by_user'] is False
  558. def test_api_set_content_as_unread__ok__200__with_comments(self):
  559. # init DB
  560. dbsession = get_tm_session(self.session_factory, transaction.manager)
  561. admin = dbsession.query(models.User) \
  562. .filter(models.User.email == 'admin@admin.admin') \
  563. .one()
  564. workspace_api = WorkspaceApi(
  565. current_user=admin,
  566. session=dbsession,
  567. config=self.app_config
  568. )
  569. workspace = WorkspaceApi(
  570. current_user=admin,
  571. session=dbsession,
  572. config=self.app_config,
  573. ).create_workspace(
  574. 'test workspace',
  575. save_now=True
  576. )
  577. api = ContentApi(
  578. current_user=admin,
  579. session=dbsession,
  580. config=self.app_config,
  581. )
  582. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  583. # creation order test
  584. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  585. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  586. api.mark_read(firstly_created)
  587. api.mark_read(comments)
  588. dbsession.flush()
  589. transaction.commit()
  590. self.testapp.authorization = (
  591. 'Basic',
  592. (
  593. 'admin@admin.admin',
  594. 'admin@admin.admin'
  595. )
  596. )
  597. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  598. assert res.json_body[0]['content_id'] == firstly_created.content_id
  599. assert res.json_body[0]['read_by_user'] is True
  600. self.testapp.put(
  601. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  602. workspace_id=workspace.workspace_id,
  603. content_id=firstly_created.content_id,
  604. user_id=admin.user_id,
  605. )
  606. )
  607. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  608. assert res.json_body[0]['content_id'] == firstly_created.content_id
  609. assert res.json_body[0]['read_by_user'] is False
  610. assert comments.has_new_information_for(admin) is True
  611. class TestUserSetWorkspaceAsRead(FunctionalTest):
  612. """
  613. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/read
  614. """
  615. def test_api_set_content_as_read__ok__200__nominal_case(self):
  616. # init DB
  617. dbsession = get_tm_session(self.session_factory, transaction.manager)
  618. admin = dbsession.query(models.User) \
  619. .filter(models.User.email == 'admin@admin.admin') \
  620. .one()
  621. workspace_api = WorkspaceApi(
  622. current_user=admin,
  623. session=dbsession,
  624. config=self.app_config
  625. )
  626. workspace = WorkspaceApi(
  627. current_user=admin,
  628. session=dbsession,
  629. config=self.app_config,
  630. ).create_workspace(
  631. 'test workspace',
  632. save_now=True
  633. )
  634. api = ContentApi(
  635. current_user=admin,
  636. session=dbsession,
  637. config=self.app_config,
  638. )
  639. main_folder = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', '', True) # nopep8
  640. # creation order test
  641. firstly_created = api.create(ContentType.Page, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  642. api.mark_unread(main_folder)
  643. api.mark_unread(firstly_created)
  644. dbsession.flush()
  645. transaction.commit()
  646. self.testapp.authorization = (
  647. 'Basic',
  648. (
  649. 'admin@admin.admin',
  650. 'admin@admin.admin'
  651. )
  652. )
  653. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  654. assert res.json_body[0]['content_id'] == firstly_created.content_id
  655. assert res.json_body[0]['read_by_user'] is False
  656. assert res.json_body[1]['content_id'] == main_folder.content_id
  657. assert res.json_body[1]['read_by_user'] is False
  658. self.testapp.put(
  659. '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format( # nopep8
  660. workspace_id=workspace.workspace_id,
  661. content_id=firstly_created.content_id,
  662. user_id=admin.user_id,
  663. )
  664. )
  665. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  666. assert res.json_body[0]['content_id'] == firstly_created.content_id
  667. assert res.json_body[0]['read_by_user'] is True
  668. assert res.json_body[1]['content_id'] == main_folder.content_id
  669. assert res.json_body[1]['read_by_user'] is True
  670. class TestUserWorkspaceEndpoint(FunctionalTest):
  671. """
  672. Tests for /api/v2/users/{user_id}/workspaces
  673. """
  674. fixtures = [BaseFixture, ContentFixtures]
  675. def test_api__get_user_workspaces__ok_200__nominal_case(self):
  676. """
  677. Check obtain all workspaces reachables for user with user auth.
  678. """
  679. self.testapp.authorization = (
  680. 'Basic',
  681. (
  682. 'admin@admin.admin',
  683. 'admin@admin.admin'
  684. )
  685. )
  686. res = self.testapp.get('/api/v2/users/1/workspaces', status=200)
  687. res = res.json_body
  688. workspace = res[0]
  689. assert workspace['workspace_id'] == 1
  690. assert workspace['label'] == 'Business'
  691. assert workspace['slug'] == 'business'
  692. assert len(workspace['sidebar_entries']) == 7
  693. sidebar_entry = workspace['sidebar_entries'][0]
  694. assert sidebar_entry['slug'] == 'dashboard'
  695. assert sidebar_entry['label'] == 'Dashboard'
  696. assert sidebar_entry['route'] == '/#/workspaces/1/dashboard' # nopep8
  697. assert sidebar_entry['hexcolor'] == "#252525"
  698. assert sidebar_entry['fa_icon'] == "signal"
  699. sidebar_entry = workspace['sidebar_entries'][1]
  700. assert sidebar_entry['slug'] == 'contents/all'
  701. assert sidebar_entry['label'] == 'All Contents'
  702. assert sidebar_entry['route'] == "/#/workspaces/1/contents" # nopep8
  703. assert sidebar_entry['hexcolor'] == "#fdfdfd"
  704. assert sidebar_entry['fa_icon'] == "th"
  705. sidebar_entry = workspace['sidebar_entries'][2]
  706. assert sidebar_entry['slug'] == 'contents/html-documents'
  707. assert sidebar_entry['label'] == 'Text Documents'
  708. assert sidebar_entry['route'] == '/#/workspaces/1/contents?type=html-documents' # nopep8
  709. assert sidebar_entry['hexcolor'] == "#3f52e3"
  710. assert sidebar_entry['fa_icon'] == "file-text-o"
  711. sidebar_entry = workspace['sidebar_entries'][3]
  712. assert sidebar_entry['slug'] == 'contents/markdownpluspage'
  713. assert sidebar_entry['label'] == 'Markdown Plus Documents'
  714. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=markdownpluspage" # nopep8
  715. assert sidebar_entry['hexcolor'] == "#f12d2d"
  716. assert sidebar_entry['fa_icon'] == "file-code-o"
  717. sidebar_entry = workspace['sidebar_entries'][4]
  718. assert sidebar_entry['slug'] == 'contents/files'
  719. assert sidebar_entry['label'] == 'Files'
  720. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=file" # nopep8
  721. assert sidebar_entry['hexcolor'] == "#FF9900"
  722. assert sidebar_entry['fa_icon'] == "paperclip"
  723. sidebar_entry = workspace['sidebar_entries'][5]
  724. assert sidebar_entry['slug'] == 'contents/threads'
  725. assert sidebar_entry['label'] == 'Threads'
  726. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=thread" # nopep8
  727. assert sidebar_entry['hexcolor'] == "#ad4cf9"
  728. assert sidebar_entry['fa_icon'] == "comments-o"
  729. sidebar_entry = workspace['sidebar_entries'][6]
  730. assert sidebar_entry['slug'] == 'calendar'
  731. assert sidebar_entry['label'] == 'Calendar'
  732. assert sidebar_entry['route'] == "/#/workspaces/1/calendar" # nopep8
  733. assert sidebar_entry['hexcolor'] == "#757575"
  734. assert sidebar_entry['fa_icon'] == "calendar"
  735. def test_api__get_user_workspaces__err_403__unallowed_user(self):
  736. """
  737. Check obtain all workspaces reachables for one user
  738. with another non-admin user auth.
  739. """
  740. self.testapp.authorization = (
  741. 'Basic',
  742. (
  743. 'lawrence-not-real-email@fsf.local',
  744. 'foobarbaz'
  745. )
  746. )
  747. res = self.testapp.get('/api/v2/users/1/workspaces', status=403)
  748. assert isinstance(res.json, dict)
  749. assert 'code' in res.json.keys()
  750. assert 'message' in res.json.keys()
  751. assert 'details' in res.json.keys()
  752. def test_api__get_user_workspaces__err_401__unregistered_user(self):
  753. """
  754. Check obtain all workspaces reachables for one user
  755. without correct user auth (user unregistered).
  756. """
  757. self.testapp.authorization = (
  758. 'Basic',
  759. (
  760. 'john@doe.doe',
  761. 'lapin'
  762. )
  763. )
  764. res = self.testapp.get('/api/v2/users/1/workspaces', status=401)
  765. assert isinstance(res.json, dict)
  766. assert 'code' in res.json.keys()
  767. assert 'message' in res.json.keys()
  768. assert 'details' in res.json.keys()
  769. def test_api__get_user_workspaces__err_400__user_does_not_exist(self):
  770. """
  771. Check obtain all workspaces reachables for one user who does
  772. not exist
  773. with a correct user auth.
  774. """
  775. self.testapp.authorization = (
  776. 'Basic',
  777. (
  778. 'admin@admin.admin',
  779. 'admin@admin.admin'
  780. )
  781. )
  782. res = self.testapp.get('/api/v2/users/5/workspaces', status=400)
  783. assert isinstance(res.json, dict)
  784. assert 'code' in res.json.keys()
  785. assert 'message' in res.json.keys()
  786. assert 'details' in res.json.keys()
  787. class TestUserEndpoint(FunctionalTest):
  788. # -*- coding: utf-8 -*-
  789. """
  790. Tests for GET /api/v2/users/{user_id}
  791. """
  792. fixtures = [BaseFixture]
  793. def test_api__get_user__ok_200__admin(self):
  794. dbsession = get_tm_session(self.session_factory, transaction.manager)
  795. admin = dbsession.query(models.User) \
  796. .filter(models.User.email == 'admin@admin.admin') \
  797. .one()
  798. uapi = UserApi(
  799. current_user=admin,
  800. session=dbsession,
  801. config=self.app_config,
  802. )
  803. gapi = GroupApi(
  804. current_user=admin,
  805. session=dbsession,
  806. config=self.app_config,
  807. )
  808. groups = [gapi.get_one_with_name('users')]
  809. test_user = uapi.create_user(
  810. email='test@test.test',
  811. password='pass',
  812. name='bob',
  813. groups=groups,
  814. timezone='Europe/Paris',
  815. do_save=True,
  816. do_notify=False,
  817. )
  818. uapi.save(test_user)
  819. transaction.commit()
  820. user_id = int(test_user.user_id)
  821. self.testapp.authorization = (
  822. 'Basic',
  823. (
  824. 'admin@admin.admin',
  825. 'admin@admin.admin'
  826. )
  827. )
  828. res = self.testapp.get(
  829. '/api/v2/users/{}'.format(user_id),
  830. status=200
  831. )
  832. res = res.json_body
  833. assert res['user_id'] == user_id
  834. assert res['created']
  835. assert res['is_active'] is True
  836. assert res['profile'] == 'users'
  837. assert res['email'] == 'test@test.test'
  838. assert res['public_name'] == 'bob'
  839. assert res['timezone'] == 'Europe/Paris'
  840. def test_api__get_user__ok_200__user_itself(self):
  841. dbsession = get_tm_session(self.session_factory, transaction.manager)
  842. admin = dbsession.query(models.User) \
  843. .filter(models.User.email == 'admin@admin.admin') \
  844. .one()
  845. uapi = UserApi(
  846. current_user=admin,
  847. session=dbsession,
  848. config=self.app_config,
  849. )
  850. gapi = GroupApi(
  851. current_user=admin,
  852. session=dbsession,
  853. config=self.app_config,
  854. )
  855. groups = [gapi.get_one_with_name('users')]
  856. test_user = uapi.create_user(
  857. email='test@test.test',
  858. password='pass',
  859. name='bob',
  860. groups=groups,
  861. timezone='Europe/Paris',
  862. do_save=True,
  863. do_notify=False,
  864. )
  865. uapi.save(test_user)
  866. transaction.commit()
  867. user_id = int(test_user.user_id)
  868. self.testapp.authorization = (
  869. 'Basic',
  870. (
  871. 'test@test.test',
  872. 'pass'
  873. )
  874. )
  875. res = self.testapp.get(
  876. '/api/v2/users/{}'.format(user_id),
  877. status=200
  878. )
  879. res = res.json_body
  880. assert res['user_id'] == user_id
  881. assert res['created']
  882. assert res['is_active'] is True
  883. assert res['profile'] == 'users'
  884. assert res['email'] == 'test@test.test'
  885. assert res['public_name'] == 'bob'
  886. assert res['timezone'] == 'Europe/Paris'
  887. def test_api__get_user__err_403__other_normal_user(self):
  888. dbsession = get_tm_session(self.session_factory, transaction.manager)
  889. admin = dbsession.query(models.User) \
  890. .filter(models.User.email == 'admin@admin.admin') \
  891. .one()
  892. uapi = UserApi(
  893. current_user=admin,
  894. session=dbsession,
  895. config=self.app_config,
  896. )
  897. gapi = GroupApi(
  898. current_user=admin,
  899. session=dbsession,
  900. config=self.app_config,
  901. )
  902. groups = [gapi.get_one_with_name('users')]
  903. test_user = uapi.create_user(
  904. email='test@test.test',
  905. password='pass',
  906. name='bob',
  907. groups=groups,
  908. timezone='Europe/Paris',
  909. do_save=True,
  910. do_notify=False,
  911. )
  912. test_user2 = uapi.create_user(
  913. email='test2@test2.test2',
  914. password='pass',
  915. name='bob2',
  916. groups=groups,
  917. timezone='Europe/Paris',
  918. do_save=True,
  919. do_notify=False,
  920. )
  921. uapi.save(test_user2)
  922. uapi.save(test_user)
  923. transaction.commit()
  924. user_id = int(test_user.user_id)
  925. self.testapp.authorization = (
  926. 'Basic',
  927. (
  928. 'test2@test2.test2',
  929. 'pass'
  930. )
  931. )
  932. self.testapp.get(
  933. '/api/v2/users/{}'.format(user_id),
  934. status=403
  935. )
  936. class TestSetEmailEndpoint(FunctionalTest):
  937. # -*- coding: utf-8 -*-
  938. """
  939. Tests for PUT /api/v2/users/{user_id}/email
  940. """
  941. fixtures = [BaseFixture]
  942. def test_api__set_user_email__ok_200__admin(self):
  943. dbsession = get_tm_session(self.session_factory, transaction.manager)
  944. admin = dbsession.query(models.User) \
  945. .filter(models.User.email == 'admin@admin.admin') \
  946. .one()
  947. uapi = UserApi(
  948. current_user=admin,
  949. session=dbsession,
  950. config=self.app_config,
  951. )
  952. gapi = GroupApi(
  953. current_user=admin,
  954. session=dbsession,
  955. config=self.app_config,
  956. )
  957. groups = [gapi.get_one_with_name('users')]
  958. test_user = uapi.create_user(
  959. email='test@test.test',
  960. password='pass',
  961. name='bob',
  962. groups=groups,
  963. timezone='Europe/Paris',
  964. do_save=True,
  965. do_notify=False,
  966. )
  967. uapi.save(test_user)
  968. transaction.commit()
  969. user_id = int(test_user.user_id)
  970. self.testapp.authorization = (
  971. 'Basic',
  972. (
  973. 'admin@admin.admin',
  974. 'admin@admin.admin'
  975. )
  976. )
  977. # check before
  978. res = self.testapp.get(
  979. '/api/v2/users/{}'.format(user_id),
  980. status=200
  981. )
  982. res = res.json_body
  983. assert res['email'] == 'test@test.test'
  984. # Set password
  985. params = {
  986. 'email': 'mysuperemail@email.fr',
  987. 'loggedin_user_password': 'admin@admin.admin',
  988. }
  989. self.testapp.put_json(
  990. '/api/v2/users/{}/email'.format(user_id),
  991. params=params,
  992. status=200,
  993. )
  994. # Check After
  995. res = self.testapp.get(
  996. '/api/v2/users/{}'.format(user_id),
  997. status=200
  998. )
  999. res = res.json_body
  1000. assert res['email'] == 'mysuperemail@email.fr'
  1001. def test_api__set_user_email__err_403__admin_wrong_password(self):
  1002. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1003. admin = dbsession.query(models.User) \
  1004. .filter(models.User.email == 'admin@admin.admin') \
  1005. .one()
  1006. uapi = UserApi(
  1007. current_user=admin,
  1008. session=dbsession,
  1009. config=self.app_config,
  1010. )
  1011. gapi = GroupApi(
  1012. current_user=admin,
  1013. session=dbsession,
  1014. config=self.app_config,
  1015. )
  1016. groups = [gapi.get_one_with_name('users')]
  1017. test_user = uapi.create_user(
  1018. email='test@test.test',
  1019. password='pass',
  1020. name='bob',
  1021. groups=groups,
  1022. timezone='Europe/Paris',
  1023. do_save=True,
  1024. do_notify=False,
  1025. )
  1026. uapi.save(test_user)
  1027. transaction.commit()
  1028. user_id = int(test_user.user_id)
  1029. self.testapp.authorization = (
  1030. 'Basic',
  1031. (
  1032. 'admin@admin.admin',
  1033. 'admin@admin.admin'
  1034. )
  1035. )
  1036. # check before
  1037. res = self.testapp.get(
  1038. '/api/v2/users/{}'.format(user_id),
  1039. status=200
  1040. )
  1041. res = res.json_body
  1042. assert res['email'] == 'test@test.test'
  1043. # Set password
  1044. params = {
  1045. 'email': 'mysuperemail@email.fr',
  1046. 'loggedin_user_password': 'badpassword',
  1047. }
  1048. self.testapp.put_json(
  1049. '/api/v2/users/{}/email'.format(user_id),
  1050. params=params,
  1051. status=403,
  1052. )
  1053. # Check After
  1054. res = self.testapp.get(
  1055. '/api/v2/users/{}'.format(user_id),
  1056. status=200
  1057. )
  1058. res = res.json_body
  1059. assert res['email'] == 'test@test.test'
  1060. def test_api__set_user_email__err_400__admin_string_is_not_email(self):
  1061. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1062. admin = dbsession.query(models.User) \
  1063. .filter(models.User.email == 'admin@admin.admin') \
  1064. .one()
  1065. uapi = UserApi(
  1066. current_user=admin,
  1067. session=dbsession,
  1068. config=self.app_config,
  1069. )
  1070. gapi = GroupApi(
  1071. current_user=admin,
  1072. session=dbsession,
  1073. config=self.app_config,
  1074. )
  1075. groups = [gapi.get_one_with_name('users')]
  1076. test_user = uapi.create_user(
  1077. email='test@test.test',
  1078. password='pass',
  1079. name='bob',
  1080. groups=groups,
  1081. timezone='Europe/Paris',
  1082. do_save=True,
  1083. do_notify=False,
  1084. )
  1085. uapi.save(test_user)
  1086. transaction.commit()
  1087. user_id = int(test_user.user_id)
  1088. self.testapp.authorization = (
  1089. 'Basic',
  1090. (
  1091. 'admin@admin.admin',
  1092. 'admin@admin.admin'
  1093. )
  1094. )
  1095. # check before
  1096. res = self.testapp.get(
  1097. '/api/v2/users/{}'.format(user_id),
  1098. status=200
  1099. )
  1100. res = res.json_body
  1101. assert res['email'] == 'test@test.test'
  1102. # Set password
  1103. params = {
  1104. 'email': 'thatisnotandemail',
  1105. 'loggedin_user_password': 'admin@admin.admin',
  1106. }
  1107. self.testapp.put_json(
  1108. '/api/v2/users/{}/email'.format(user_id),
  1109. params=params,
  1110. status=400,
  1111. )
  1112. # Check After
  1113. res = self.testapp.get(
  1114. '/api/v2/users/{}'.format(user_id),
  1115. status=200
  1116. )
  1117. res = res.json_body
  1118. assert res['email'] == 'test@test.test'
  1119. def test_api__set_user_email__ok_200__user_itself(self):
  1120. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1121. admin = dbsession.query(models.User) \
  1122. .filter(models.User.email == 'admin@admin.admin') \
  1123. .one()
  1124. uapi = UserApi(
  1125. current_user=admin,
  1126. session=dbsession,
  1127. config=self.app_config,
  1128. )
  1129. gapi = GroupApi(
  1130. current_user=admin,
  1131. session=dbsession,
  1132. config=self.app_config,
  1133. )
  1134. groups = [gapi.get_one_with_name('users')]
  1135. test_user = uapi.create_user(
  1136. email='test@test.test',
  1137. password='pass',
  1138. name='bob',
  1139. groups=groups,
  1140. timezone='Europe/Paris',
  1141. do_save=True,
  1142. do_notify=False,
  1143. )
  1144. uapi.save(test_user)
  1145. transaction.commit()
  1146. user_id = int(test_user.user_id)
  1147. self.testapp.authorization = (
  1148. 'Basic',
  1149. (
  1150. 'test@test.test',
  1151. 'pass'
  1152. )
  1153. )
  1154. # check before
  1155. res = self.testapp.get(
  1156. '/api/v2/users/{}'.format(user_id),
  1157. status=200
  1158. )
  1159. res = res.json_body
  1160. assert res['email'] == 'test@test.test'
  1161. # Set password
  1162. params = {
  1163. 'email': 'mysuperemail@email.fr',
  1164. 'loggedin_user_password': 'pass',
  1165. }
  1166. self.testapp.put_json(
  1167. '/api/v2/users/{}/email'.format(user_id),
  1168. params=params,
  1169. status=200,
  1170. )
  1171. self.testapp.authorization = (
  1172. 'Basic',
  1173. (
  1174. 'mysuperemail@email.fr',
  1175. 'pass'
  1176. )
  1177. )
  1178. # Check After
  1179. res = self.testapp.get(
  1180. '/api/v2/users/{}'.format(user_id),
  1181. status=200
  1182. )
  1183. res = res.json_body
  1184. assert res['email'] == 'mysuperemail@email.fr'
  1185. def test_api__set_user_email__err_403__other_normal_user(self):
  1186. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1187. admin = dbsession.query(models.User) \
  1188. .filter(models.User.email == 'admin@admin.admin') \
  1189. .one()
  1190. uapi = UserApi(
  1191. current_user=admin,
  1192. session=dbsession,
  1193. config=self.app_config,
  1194. )
  1195. gapi = GroupApi(
  1196. current_user=admin,
  1197. session=dbsession,
  1198. config=self.app_config,
  1199. )
  1200. groups = [gapi.get_one_with_name('users')]
  1201. test_user = uapi.create_user(
  1202. email='test@test.test',
  1203. password='pass',
  1204. name='bob',
  1205. groups=groups,
  1206. timezone='Europe/Paris',
  1207. do_save=True,
  1208. do_notify=False,
  1209. )
  1210. test_user2 = uapi.create_user(
  1211. email='test2@test2.test2',
  1212. password='pass',
  1213. name='bob2',
  1214. groups=groups,
  1215. timezone='Europe/Paris',
  1216. do_save=True,
  1217. do_notify=False,
  1218. )
  1219. uapi.save(test_user2)
  1220. uapi.save(test_user)
  1221. transaction.commit()
  1222. user_id = int(test_user.user_id)
  1223. self.testapp.authorization = (
  1224. 'Basic',
  1225. (
  1226. 'test@test.test',
  1227. 'pass'
  1228. )
  1229. )
  1230. # Set password
  1231. params = {
  1232. 'email': 'mysuperemail@email.fr',
  1233. 'loggedin_user_password': 'test2@test2.test2',
  1234. }
  1235. self.testapp.put_json(
  1236. '/api/v2/users/{}/email'.format(user_id),
  1237. params=params,
  1238. status=403,
  1239. )
  1240. class TestSetPasswordEndpoint(FunctionalTest):
  1241. # -*- coding: utf-8 -*-
  1242. """
  1243. Tests for PUT /api/v2/users/{user_id}/password
  1244. """
  1245. fixtures = [BaseFixture]
  1246. def test_api__set_user_password__ok_200__admin(self):
  1247. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1248. admin = dbsession.query(models.User) \
  1249. .filter(models.User.email == 'admin@admin.admin') \
  1250. .one()
  1251. uapi = UserApi(
  1252. current_user=admin,
  1253. session=dbsession,
  1254. config=self.app_config,
  1255. )
  1256. gapi = GroupApi(
  1257. current_user=admin,
  1258. session=dbsession,
  1259. config=self.app_config,
  1260. )
  1261. groups = [gapi.get_one_with_name('users')]
  1262. test_user = uapi.create_user(
  1263. email='test@test.test',
  1264. password='pass',
  1265. name='bob',
  1266. groups=groups,
  1267. timezone='Europe/Paris',
  1268. do_save=True,
  1269. do_notify=False,
  1270. )
  1271. uapi.save(test_user)
  1272. transaction.commit()
  1273. user_id = int(test_user.user_id)
  1274. self.testapp.authorization = (
  1275. 'Basic',
  1276. (
  1277. 'admin@admin.admin',
  1278. 'admin@admin.admin'
  1279. )
  1280. )
  1281. # check before
  1282. user = uapi.get_one(user_id)
  1283. assert user.validate_password('pass')
  1284. assert not user.validate_password('mynewpassword')
  1285. # Set password
  1286. params = {
  1287. 'new_password': 'mynewpassword',
  1288. 'new_password2': 'mynewpassword',
  1289. 'loggedin_user_password': 'admin@admin.admin',
  1290. }
  1291. self.testapp.put_json(
  1292. '/api/v2/users/{}/password'.format(user_id),
  1293. params=params,
  1294. status=204,
  1295. )
  1296. # Check After
  1297. user = uapi.get_one(user_id)
  1298. assert not user.validate_password('pass')
  1299. assert user.validate_password('mynewpassword')
  1300. def test_api__set_user_password__err_403__admin_wrong_password(self):
  1301. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1302. admin = dbsession.query(models.User) \
  1303. .filter(models.User.email == 'admin@admin.admin') \
  1304. .one()
  1305. uapi = UserApi(
  1306. current_user=admin,
  1307. session=dbsession,
  1308. config=self.app_config,
  1309. )
  1310. gapi = GroupApi(
  1311. current_user=admin,
  1312. session=dbsession,
  1313. config=self.app_config,
  1314. )
  1315. groups = [gapi.get_one_with_name('users')]
  1316. test_user = uapi.create_user(
  1317. email='test@test.test',
  1318. password='pass',
  1319. name='bob',
  1320. groups=groups,
  1321. timezone='Europe/Paris',
  1322. do_save=True,
  1323. do_notify=False,
  1324. )
  1325. uapi.save(test_user)
  1326. transaction.commit()
  1327. user_id = int(test_user.user_id)
  1328. self.testapp.authorization = (
  1329. 'Basic',
  1330. (
  1331. 'admin@admin.admin',
  1332. 'admin@admin.admin'
  1333. )
  1334. )
  1335. # check before
  1336. user = uapi.get_one(user_id)
  1337. assert user.validate_password('pass')
  1338. assert not user.validate_password('mynewpassword')
  1339. # Set password
  1340. params = {
  1341. 'new_password': 'mynewpassword',
  1342. 'new_password2': 'mynewpassword',
  1343. 'loggedin_user_password': 'wrongpassword',
  1344. }
  1345. self.testapp.put_json(
  1346. '/api/v2/users/{}/password'.format(user_id),
  1347. params=params,
  1348. status=403,
  1349. )
  1350. # Check After
  1351. user = uapi.get_one(user_id)
  1352. assert user.validate_password('pass')
  1353. assert not user.validate_password('mynewpassword')
  1354. def test_api__set_user_password__err_400__admin_passwords_do_not_match(self): # nopep8
  1355. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1356. admin = dbsession.query(models.User) \
  1357. .filter(models.User.email == 'admin@admin.admin') \
  1358. .one()
  1359. uapi = UserApi(
  1360. current_user=admin,
  1361. session=dbsession,
  1362. config=self.app_config,
  1363. )
  1364. gapi = GroupApi(
  1365. current_user=admin,
  1366. session=dbsession,
  1367. config=self.app_config,
  1368. )
  1369. groups = [gapi.get_one_with_name('users')]
  1370. test_user = uapi.create_user(
  1371. email='test@test.test',
  1372. password='pass',
  1373. name='bob',
  1374. groups=groups,
  1375. timezone='Europe/Paris',
  1376. do_save=True,
  1377. do_notify=False,
  1378. )
  1379. uapi.save(test_user)
  1380. transaction.commit()
  1381. user_id = int(test_user.user_id)
  1382. self.testapp.authorization = (
  1383. 'Basic',
  1384. (
  1385. 'admin@admin.admin',
  1386. 'admin@admin.admin'
  1387. )
  1388. )
  1389. # check before
  1390. user = uapi.get_one(user_id)
  1391. assert user.validate_password('pass')
  1392. assert not user.validate_password('mynewpassword')
  1393. assert not user.validate_password('mynewpassword2')
  1394. # Set password
  1395. params = {
  1396. 'new_password': 'mynewpassword',
  1397. 'new_password2': 'mynewpassword2',
  1398. 'loggedin_user_password': 'admin@admin.admin',
  1399. }
  1400. self.testapp.put_json(
  1401. '/api/v2/users/{}/password'.format(user_id),
  1402. params=params,
  1403. status=400,
  1404. )
  1405. # Check After
  1406. user = uapi.get_one(user_id)
  1407. assert user.validate_password('pass')
  1408. assert not user.validate_password('mynewpassword')
  1409. assert not user.validate_password('mynewpassword2')
  1410. def test_api__set_user_password__ok_200__user_itself(self):
  1411. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1412. admin = dbsession.query(models.User) \
  1413. .filter(models.User.email == 'admin@admin.admin') \
  1414. .one()
  1415. uapi = UserApi(
  1416. current_user=admin,
  1417. session=dbsession,
  1418. config=self.app_config,
  1419. )
  1420. gapi = GroupApi(
  1421. current_user=admin,
  1422. session=dbsession,
  1423. config=self.app_config,
  1424. )
  1425. groups = [gapi.get_one_with_name('users')]
  1426. test_user = uapi.create_user(
  1427. email='test@test.test',
  1428. password='pass',
  1429. name='bob',
  1430. groups=groups,
  1431. timezone='Europe/Paris',
  1432. do_save=True,
  1433. do_notify=False,
  1434. )
  1435. uapi.save(test_user)
  1436. transaction.commit()
  1437. user_id = int(test_user.user_id)
  1438. self.testapp.authorization = (
  1439. 'Basic',
  1440. (
  1441. 'test@test.test',
  1442. 'pass'
  1443. )
  1444. )
  1445. # check before
  1446. user = uapi.get_one(user_id)
  1447. assert user.validate_password('pass')
  1448. assert not user.validate_password('mynewpassword')
  1449. # Set password
  1450. params = {
  1451. 'new_password': 'mynewpassword',
  1452. 'new_password2': 'mynewpassword',
  1453. 'loggedin_user_password': 'pass',
  1454. }
  1455. self.testapp.put_json(
  1456. '/api/v2/users/{}/password'.format(user_id),
  1457. params=params,
  1458. status=204,
  1459. )
  1460. # Check After
  1461. user = uapi.get_one(user_id)
  1462. assert not user.validate_password('pass')
  1463. assert user.validate_password('mynewpassword')
  1464. def test_api__set_user_email__err_403__other_normal_user(self):
  1465. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1466. admin = dbsession.query(models.User) \
  1467. .filter(models.User.email == 'admin@admin.admin') \
  1468. .one()
  1469. uapi = UserApi(
  1470. current_user=admin,
  1471. session=dbsession,
  1472. config=self.app_config,
  1473. )
  1474. gapi = GroupApi(
  1475. current_user=admin,
  1476. session=dbsession,
  1477. config=self.app_config,
  1478. )
  1479. groups = [gapi.get_one_with_name('users')]
  1480. test_user = uapi.create_user(
  1481. email='test@test.test',
  1482. password='pass',
  1483. name='bob',
  1484. groups=groups,
  1485. timezone='Europe/Paris',
  1486. do_save=True,
  1487. do_notify=False,
  1488. )
  1489. test_user2 = uapi.create_user(
  1490. email='test2@test2.test2',
  1491. password='pass',
  1492. name='bob2',
  1493. groups=groups,
  1494. timezone='Europe/Paris',
  1495. do_save=True,
  1496. do_notify=False,
  1497. )
  1498. uapi.save(test_user2)
  1499. uapi.save(test_user)
  1500. transaction.commit()
  1501. user_id = int(test_user.user_id)
  1502. self.testapp.authorization = (
  1503. 'Basic',
  1504. (
  1505. 'test@test.test',
  1506. 'pass'
  1507. )
  1508. )
  1509. # Set password
  1510. params = {
  1511. 'email': 'mysuperemail@email.fr',
  1512. 'loggedin_user_password': 'test2@test2.test2',
  1513. }
  1514. self.testapp.put_json(
  1515. '/api/v2/users/{}/email'.format(user_id),
  1516. params=params,
  1517. status=403,
  1518. )
  1519. class TestSetUserInfoEndpoint(FunctionalTest):
  1520. # -*- coding: utf-8 -*-
  1521. """
  1522. Tests for PUT /api/v2/users/{user_id}
  1523. """
  1524. fixtures = [BaseFixture]
  1525. def test_api__set_user_info__ok_200__admin(self):
  1526. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1527. admin = dbsession.query(models.User) \
  1528. .filter(models.User.email == 'admin@admin.admin') \
  1529. .one()
  1530. uapi = UserApi(
  1531. current_user=admin,
  1532. session=dbsession,
  1533. config=self.app_config,
  1534. )
  1535. gapi = GroupApi(
  1536. current_user=admin,
  1537. session=dbsession,
  1538. config=self.app_config,
  1539. )
  1540. groups = [gapi.get_one_with_name('users')]
  1541. test_user = uapi.create_user(
  1542. email='test@test.test',
  1543. password='pass',
  1544. name='bob',
  1545. groups=groups,
  1546. timezone='Europe/Paris',
  1547. do_save=True,
  1548. do_notify=False,
  1549. )
  1550. uapi.save(test_user)
  1551. transaction.commit()
  1552. user_id = int(test_user.user_id)
  1553. self.testapp.authorization = (
  1554. 'Basic',
  1555. (
  1556. 'admin@admin.admin',
  1557. 'admin@admin.admin'
  1558. )
  1559. )
  1560. # check before
  1561. res = self.testapp.get(
  1562. '/api/v2/users/{}'.format(user_id),
  1563. status=200
  1564. )
  1565. res = res.json_body
  1566. assert res['user_id'] == user_id
  1567. assert res['public_name'] == 'bob'
  1568. assert res['timezone'] == 'Europe/Paris'
  1569. # Set params
  1570. params = {
  1571. 'public_name': 'updated',
  1572. 'timezone': 'Europe/London',
  1573. }
  1574. self.testapp.put_json(
  1575. '/api/v2/users/{}'.format(user_id),
  1576. params=params,
  1577. status=200,
  1578. )
  1579. # Check After
  1580. res = self.testapp.get(
  1581. '/api/v2/users/{}'.format(user_id),
  1582. status=200
  1583. )
  1584. res = res.json_body
  1585. assert res['user_id'] == user_id
  1586. assert res['public_name'] == 'updated'
  1587. assert res['timezone'] == 'Europe/London'
  1588. def test_api__set_user_info__ok_200__user_itself(self):
  1589. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1590. admin = dbsession.query(models.User) \
  1591. .filter(models.User.email == 'admin@admin.admin') \
  1592. .one()
  1593. uapi = UserApi(
  1594. current_user=admin,
  1595. session=dbsession,
  1596. config=self.app_config,
  1597. )
  1598. gapi = GroupApi(
  1599. current_user=admin,
  1600. session=dbsession,
  1601. config=self.app_config,
  1602. )
  1603. groups = [gapi.get_one_with_name('users')]
  1604. test_user = uapi.create_user(
  1605. email='test@test.test',
  1606. password='pass',
  1607. name='bob',
  1608. groups=groups,
  1609. timezone='Europe/Paris',
  1610. do_save=True,
  1611. do_notify=False,
  1612. )
  1613. uapi.save(test_user)
  1614. transaction.commit()
  1615. user_id = int(test_user.user_id)
  1616. self.testapp.authorization = (
  1617. 'Basic',
  1618. (
  1619. 'test@test.test',
  1620. 'pass',
  1621. )
  1622. )
  1623. # check before
  1624. res = self.testapp.get(
  1625. '/api/v2/users/{}'.format(user_id),
  1626. status=200
  1627. )
  1628. res = res.json_body
  1629. assert res['user_id'] == user_id
  1630. assert res['public_name'] == 'bob'
  1631. assert res['timezone'] == 'Europe/Paris'
  1632. # Set params
  1633. params = {
  1634. 'public_name': 'updated',
  1635. 'timezone': 'Europe/London',
  1636. }
  1637. self.testapp.put_json(
  1638. '/api/v2/users/{}'.format(user_id),
  1639. params=params,
  1640. status=200,
  1641. )
  1642. # Check After
  1643. res = self.testapp.get(
  1644. '/api/v2/users/{}'.format(user_id),
  1645. status=200
  1646. )
  1647. res = res.json_body
  1648. assert res['user_id'] == user_id
  1649. assert res['public_name'] == 'updated'
  1650. assert res['timezone'] == 'Europe/London'
  1651. def test_api__set_user_email__err_403__other_normal_user(self):
  1652. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1653. admin = dbsession.query(models.User) \
  1654. .filter(models.User.email == 'admin@admin.admin') \
  1655. .one()
  1656. uapi = UserApi(
  1657. current_user=admin,
  1658. session=dbsession,
  1659. config=self.app_config,
  1660. )
  1661. gapi = GroupApi(
  1662. current_user=admin,
  1663. session=dbsession,
  1664. config=self.app_config,
  1665. )
  1666. groups = [gapi.get_one_with_name('users')]
  1667. test_user = uapi.create_user(
  1668. email='test@test.test',
  1669. password='pass',
  1670. name='bob',
  1671. groups=groups,
  1672. timezone='Europe/Paris',
  1673. do_save=True,
  1674. do_notify=False,
  1675. )
  1676. test_user2 = uapi.create_user(
  1677. email='test2@test2.test2',
  1678. password='pass',
  1679. name='test',
  1680. groups=groups,
  1681. timezone='Europe/Paris',
  1682. do_save=True,
  1683. do_notify=False,
  1684. )
  1685. uapi.save(test_user2)
  1686. uapi.save(test_user)
  1687. transaction.commit()
  1688. user_id = int(test_user.user_id)
  1689. self.testapp.authorization = (
  1690. 'Basic',
  1691. (
  1692. 'test2@test2.test2',
  1693. 'pass',
  1694. )
  1695. )
  1696. # Set params
  1697. params = {
  1698. 'public_name': 'updated',
  1699. 'timezone': 'Europe/London',
  1700. }
  1701. self.testapp.put_json(
  1702. '/api/v2/users/{}'.format(user_id),
  1703. params=params,
  1704. status=403,
  1705. )
  1706. class TestSetUserProfilEndpoint(FunctionalTest):
  1707. # -*- coding: utf-8 -*-
  1708. """
  1709. Tests for PUT /api/v2/users/{user_id}/profile
  1710. """
  1711. fixtures = [BaseFixture]
  1712. def test_api__set_user_info__ok_200__admin(self):
  1713. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1714. admin = dbsession.query(models.User) \
  1715. .filter(models.User.email == 'admin@admin.admin') \
  1716. .one()
  1717. uapi = UserApi(
  1718. current_user=admin,
  1719. session=dbsession,
  1720. config=self.app_config,
  1721. )
  1722. gapi = GroupApi(
  1723. current_user=admin,
  1724. session=dbsession,
  1725. config=self.app_config,
  1726. )
  1727. groups = [gapi.get_one_with_name('users')]
  1728. test_user = uapi.create_user(
  1729. email='test@test.test',
  1730. password='pass',
  1731. name='bob',
  1732. groups=groups,
  1733. timezone='Europe/Paris',
  1734. do_save=True,
  1735. do_notify=False,
  1736. )
  1737. uapi.save(test_user)
  1738. transaction.commit()
  1739. user_id = int(test_user.user_id)
  1740. self.testapp.authorization = (
  1741. 'Basic',
  1742. (
  1743. 'admin@admin.admin',
  1744. 'admin@admin.admin'
  1745. )
  1746. )
  1747. # check before
  1748. res = self.testapp.get(
  1749. '/api/v2/users/{}'.format(user_id),
  1750. status=200
  1751. )
  1752. res = res.json_body
  1753. assert res['user_id'] == user_id
  1754. assert res['profile'] == 'users'
  1755. # Set params
  1756. params = {
  1757. 'profile': 'administrators',
  1758. }
  1759. self.testapp.put_json(
  1760. '/api/v2/users/{}/profile'.format(user_id),
  1761. params=params,
  1762. status=204,
  1763. )
  1764. # Check After
  1765. res = self.testapp.get(
  1766. '/api/v2/users/{}'.format(user_id),
  1767. status=200
  1768. )
  1769. res = res.json_body
  1770. assert res['user_id'] == user_id
  1771. assert res['profile'] == 'administrators'
  1772. def test_api__set_user_info__err_403__user_itself(self):
  1773. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1774. admin = dbsession.query(models.User) \
  1775. .filter(models.User.email == 'admin@admin.admin') \
  1776. .one()
  1777. uapi = UserApi(
  1778. current_user=admin,
  1779. session=dbsession,
  1780. config=self.app_config,
  1781. )
  1782. gapi = GroupApi(
  1783. current_user=admin,
  1784. session=dbsession,
  1785. config=self.app_config,
  1786. )
  1787. groups = [gapi.get_one_with_name('users')]
  1788. test_user = uapi.create_user(
  1789. email='test@test.test',
  1790. password='pass',
  1791. name='bob',
  1792. groups=groups,
  1793. timezone='Europe/Paris',
  1794. do_save=True,
  1795. do_notify=False,
  1796. )
  1797. uapi.save(test_user)
  1798. transaction.commit()
  1799. user_id = int(test_user.user_id)
  1800. self.testapp.authorization = (
  1801. 'Basic',
  1802. (
  1803. 'test@test.test',
  1804. 'pass',
  1805. )
  1806. )
  1807. # check before
  1808. res = self.testapp.get(
  1809. '/api/v2/users/{}'.format(user_id),
  1810. status=200
  1811. )
  1812. res = res.json_body
  1813. assert res['user_id'] == user_id
  1814. assert res['profile'] == 'users'
  1815. # Set params
  1816. params = {
  1817. 'profile': 'administrators',
  1818. }
  1819. self.testapp.put_json(
  1820. '/api/v2/users/{}/profile'.format(user_id),
  1821. params=params,
  1822. status=403,
  1823. )
  1824. # Check After
  1825. res = self.testapp.get(
  1826. '/api/v2/users/{}'.format(user_id),
  1827. status=200
  1828. )
  1829. res = res.json_body
  1830. assert res['user_id'] == user_id
  1831. assert res['profile'] == 'users'
  1832. def test_api__set_user_email__err_403__other_normal_user(self):
  1833. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1834. admin = dbsession.query(models.User) \
  1835. .filter(models.User.email == 'admin@admin.admin') \
  1836. .one()
  1837. uapi = UserApi(
  1838. current_user=admin,
  1839. session=dbsession,
  1840. config=self.app_config,
  1841. )
  1842. gapi = GroupApi(
  1843. current_user=admin,
  1844. session=dbsession,
  1845. config=self.app_config,
  1846. )
  1847. groups = [gapi.get_one_with_name('users')]
  1848. test_user = uapi.create_user(
  1849. email='test@test.test',
  1850. password='pass',
  1851. name='bob',
  1852. groups=groups,
  1853. timezone='Europe/Paris',
  1854. do_save=True,
  1855. do_notify=False,
  1856. )
  1857. test_user2 = uapi.create_user(
  1858. email='test2@test2.test2',
  1859. password='pass',
  1860. name='test',
  1861. groups=groups,
  1862. timezone='Europe/Paris',
  1863. do_save=True,
  1864. do_notify=False,
  1865. )
  1866. uapi.save(test_user2)
  1867. uapi.save(test_user)
  1868. transaction.commit()
  1869. user_id = int(test_user.user_id)
  1870. self.testapp.authorization = (
  1871. 'Basic',
  1872. (
  1873. 'test2@test2.test2',
  1874. 'pass',
  1875. )
  1876. )
  1877. # Set params
  1878. params = {
  1879. 'profile': 'administrators',
  1880. }
  1881. self.testapp.put_json(
  1882. '/api/v2/users/{}/profile'.format(user_id),
  1883. params=params,
  1884. status=403,
  1885. )
  1886. class TestSetUserEnableDisableEndpoints(FunctionalTest):
  1887. # -*- coding: utf-8 -*-
  1888. """
  1889. Tests for PUT /api/v2/users/{user_id}/enable
  1890. and PUT /api/v2/users/{user_id}/disable
  1891. """
  1892. fixtures = [BaseFixture]
  1893. def test_api_enable_user__ok_200__admin(self):
  1894. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1895. admin = dbsession.query(models.User) \
  1896. .filter(models.User.email == 'admin@admin.admin') \
  1897. .one()
  1898. uapi = UserApi(
  1899. current_user=admin,
  1900. session=dbsession,
  1901. config=self.app_config,
  1902. )
  1903. gapi = GroupApi(
  1904. current_user=admin,
  1905. session=dbsession,
  1906. config=self.app_config,
  1907. )
  1908. groups = [gapi.get_one_with_name('users')]
  1909. test_user = uapi.create_user(
  1910. email='test@test.test',
  1911. password='pass',
  1912. name='bob',
  1913. groups=groups,
  1914. timezone='Europe/Paris',
  1915. do_save=True,
  1916. do_notify=False,
  1917. )
  1918. uapi.disable(test_user, do_save=True)
  1919. uapi.save(test_user)
  1920. transaction.commit()
  1921. user_id = int(test_user.user_id)
  1922. self.testapp.authorization = (
  1923. 'Basic',
  1924. (
  1925. 'admin@admin.admin',
  1926. 'admin@admin.admin'
  1927. )
  1928. )
  1929. # check before
  1930. res = self.testapp.get(
  1931. '/api/v2/users/{}'.format(user_id),
  1932. status=200
  1933. )
  1934. res = res.json_body
  1935. assert res['user_id'] == user_id
  1936. assert res['is_active'] is False
  1937. self.testapp.put_json(
  1938. '/api/v2/users/{}/enable'.format(user_id),
  1939. status=204,
  1940. )
  1941. # Check After
  1942. res = self.testapp.get(
  1943. '/api/v2/users/{}'.format(user_id),
  1944. status=200
  1945. )
  1946. res = res.json_body
  1947. assert res['user_id'] == user_id
  1948. assert res['is_active'] is True
  1949. def test_api_disable_user__ok_200__admin(self):
  1950. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1951. admin = dbsession.query(models.User) \
  1952. .filter(models.User.email == 'admin@admin.admin') \
  1953. .one()
  1954. uapi = UserApi(
  1955. current_user=admin,
  1956. session=dbsession,
  1957. config=self.app_config,
  1958. )
  1959. gapi = GroupApi(
  1960. current_user=admin,
  1961. session=dbsession,
  1962. config=self.app_config,
  1963. )
  1964. groups = [gapi.get_one_with_name('users')]
  1965. test_user = uapi.create_user(
  1966. email='test@test.test',
  1967. password='pass',
  1968. name='bob',
  1969. groups=groups,
  1970. timezone='Europe/Paris',
  1971. do_save=True,
  1972. do_notify=False,
  1973. )
  1974. uapi.enable(test_user, do_save=True)
  1975. uapi.save(test_user)
  1976. transaction.commit()
  1977. user_id = int(test_user.user_id)
  1978. self.testapp.authorization = (
  1979. 'Basic',
  1980. (
  1981. 'admin@admin.admin',
  1982. 'admin@admin.admin'
  1983. )
  1984. )
  1985. # check before
  1986. res = self.testapp.get(
  1987. '/api/v2/users/{}'.format(user_id),
  1988. status=200
  1989. )
  1990. res = res.json_body
  1991. assert res['user_id'] == user_id
  1992. assert res['is_active'] is True
  1993. self.testapp.put_json(
  1994. '/api/v2/users/{}/disable'.format(user_id),
  1995. status=204,
  1996. )
  1997. # Check After
  1998. res = self.testapp.get(
  1999. '/api/v2/users/{}'.format(user_id),
  2000. status=200
  2001. )
  2002. res = res.json_body
  2003. assert res['user_id'] == user_id
  2004. assert res['is_active'] is False
  2005. def test_api_enable_user__err_403__other_account(self):
  2006. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2007. admin = dbsession.query(models.User) \
  2008. .filter(models.User.email == 'admin@admin.admin') \
  2009. .one()
  2010. uapi = UserApi(
  2011. current_user=admin,
  2012. session=dbsession,
  2013. config=self.app_config,
  2014. )
  2015. gapi = GroupApi(
  2016. current_user=admin,
  2017. session=dbsession,
  2018. config=self.app_config,
  2019. )
  2020. groups = [gapi.get_one_with_name('users')]
  2021. test_user = uapi.create_user(
  2022. email='test@test.test',
  2023. password='pass',
  2024. name='bob',
  2025. groups=groups,
  2026. timezone='Europe/Paris',
  2027. do_save=True,
  2028. do_notify=False,
  2029. )
  2030. test_user2 = uapi.create_user(
  2031. email='test2@test2.test2',
  2032. password='pass',
  2033. name='test2',
  2034. groups=groups,
  2035. timezone='Europe/Paris',
  2036. do_save=True,
  2037. do_notify=False,
  2038. )
  2039. uapi.disable(test_user, do_save=True)
  2040. uapi.save(test_user2)
  2041. uapi.save(test_user)
  2042. transaction.commit()
  2043. user_id = int(test_user.user_id)
  2044. self.testapp.authorization = (
  2045. 'Basic',
  2046. (
  2047. 'test2@test2.test2',
  2048. 'pass'
  2049. )
  2050. )
  2051. self.testapp.put_json(
  2052. '/api/v2/users/{}/enable'.format(user_id),
  2053. status=403,
  2054. )
  2055. def test_api_disable_user__err_403__other_account(self):
  2056. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2057. admin = dbsession.query(models.User) \
  2058. .filter(models.User.email == 'admin@admin.admin') \
  2059. .one()
  2060. uapi = UserApi(
  2061. current_user=admin,
  2062. session=dbsession,
  2063. config=self.app_config,
  2064. )
  2065. gapi = GroupApi(
  2066. current_user=admin,
  2067. session=dbsession,
  2068. config=self.app_config,
  2069. )
  2070. groups = [gapi.get_one_with_name('users')]
  2071. test_user = uapi.create_user(
  2072. email='test@test.test',
  2073. password='pass',
  2074. name='bob',
  2075. groups=groups,
  2076. timezone='Europe/Paris',
  2077. do_save=True,
  2078. do_notify=False,
  2079. )
  2080. test_user2 = uapi.create_user(
  2081. email='test2@test2.test2',
  2082. password='pass',
  2083. name='test2',
  2084. groups=groups,
  2085. timezone='Europe/Paris',
  2086. do_save=True,
  2087. do_notify=False,
  2088. )
  2089. uapi.enable(test_user, do_save=True)
  2090. uapi.save(test_user2)
  2091. uapi.save(test_user)
  2092. transaction.commit()
  2093. user_id = int(test_user.user_id)
  2094. self.testapp.authorization = (
  2095. 'Basic',
  2096. (
  2097. 'test2@test2.test2',
  2098. 'pass'
  2099. )
  2100. )
  2101. self.testapp.put_json(
  2102. '/api/v2/users/{}/disable'.format(user_id),
  2103. status=403,
  2104. )
  2105. def test_api_disable_user__ok_200__user_itself(self):
  2106. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2107. admin = dbsession.query(models.User) \
  2108. .filter(models.User.email == 'admin@admin.admin') \
  2109. .one()
  2110. uapi = UserApi(
  2111. current_user=admin,
  2112. session=dbsession,
  2113. config=self.app_config,
  2114. )
  2115. gapi = GroupApi(
  2116. current_user=admin,
  2117. session=dbsession,
  2118. config=self.app_config,
  2119. )
  2120. groups = [gapi.get_one_with_name('users')]
  2121. test_user = uapi.create_user(
  2122. email='test@test.test',
  2123. password='pass',
  2124. name='bob',
  2125. groups=groups,
  2126. timezone='Europe/Paris',
  2127. do_save=True,
  2128. do_notify=False,
  2129. )
  2130. uapi.enable(test_user, do_save=True)
  2131. uapi.save(test_user)
  2132. transaction.commit()
  2133. user_id = int(test_user.user_id)
  2134. self.testapp.authorization = (
  2135. 'Basic',
  2136. (
  2137. 'test@test.test',
  2138. 'pass'
  2139. )
  2140. )
  2141. # check before
  2142. res = self.testapp.get(
  2143. '/api/v2/users/{}'.format(user_id),
  2144. status=200
  2145. )
  2146. res = res.json_body
  2147. assert res['user_id'] == user_id
  2148. assert res['is_active'] is True
  2149. self.testapp.put_json(
  2150. '/api/v2/users/{}/disable'.format(user_id),
  2151. status=403,
  2152. )
  2153. # Check After
  2154. res = self.testapp.get(
  2155. '/api/v2/users/{}'.format(user_id),
  2156. status=200
  2157. )
  2158. res = res.json_body
  2159. assert res['user_id'] == user_id
  2160. assert res['is_active'] is True