test_user.py 90KB

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