test_user.py 91KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  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. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1671. uapi = UserApi(
  1672. current_user=admin,
  1673. session=dbsession,
  1674. config=self.app_config,
  1675. )
  1676. user = uapi.get_one(user_id)
  1677. assert not user.validate_password('pass')
  1678. assert user.validate_password('mynewpassword')
  1679. def test_api__set_user_password__err_403__admin_wrong_password(self):
  1680. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1681. admin = dbsession.query(models.User) \
  1682. .filter(models.User.email == 'admin@admin.admin') \
  1683. .one()
  1684. uapi = UserApi(
  1685. current_user=admin,
  1686. session=dbsession,
  1687. config=self.app_config,
  1688. )
  1689. gapi = GroupApi(
  1690. current_user=admin,
  1691. session=dbsession,
  1692. config=self.app_config,
  1693. )
  1694. groups = [gapi.get_one_with_name('users')]
  1695. test_user = uapi.create_user(
  1696. email='test@test.test',
  1697. password='pass',
  1698. name='bob',
  1699. groups=groups,
  1700. timezone='Europe/Paris',
  1701. do_save=True,
  1702. do_notify=False,
  1703. )
  1704. uapi.save(test_user)
  1705. transaction.commit()
  1706. user_id = int(test_user.user_id)
  1707. self.testapp.authorization = (
  1708. 'Basic',
  1709. (
  1710. 'admin@admin.admin',
  1711. 'admin@admin.admin'
  1712. )
  1713. )
  1714. # check before
  1715. user = uapi.get_one(user_id)
  1716. assert user.validate_password('pass')
  1717. assert not user.validate_password('mynewpassword')
  1718. # Set password
  1719. params = {
  1720. 'new_password': 'mynewpassword',
  1721. 'new_password2': 'mynewpassword',
  1722. 'loggedin_user_password': 'wrongpassword',
  1723. }
  1724. self.testapp.put_json(
  1725. '/api/v2/users/{}/password'.format(user_id),
  1726. params=params,
  1727. status=403,
  1728. )
  1729. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1730. uapi = UserApi(
  1731. current_user=admin,
  1732. session=dbsession,
  1733. config=self.app_config,
  1734. )
  1735. # Check After
  1736. user = uapi.get_one(user_id)
  1737. assert user.validate_password('pass')
  1738. assert not user.validate_password('mynewpassword')
  1739. def test_api__set_user_password__err_400__admin_passwords_do_not_match(self): # nopep8
  1740. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1741. admin = dbsession.query(models.User) \
  1742. .filter(models.User.email == 'admin@admin.admin') \
  1743. .one()
  1744. uapi = UserApi(
  1745. current_user=admin,
  1746. session=dbsession,
  1747. config=self.app_config,
  1748. )
  1749. gapi = GroupApi(
  1750. current_user=admin,
  1751. session=dbsession,
  1752. config=self.app_config,
  1753. )
  1754. groups = [gapi.get_one_with_name('users')]
  1755. test_user = uapi.create_user(
  1756. email='test@test.test',
  1757. password='pass',
  1758. name='bob',
  1759. groups=groups,
  1760. timezone='Europe/Paris',
  1761. do_save=True,
  1762. do_notify=False,
  1763. )
  1764. uapi.save(test_user)
  1765. transaction.commit()
  1766. user_id = int(test_user.user_id)
  1767. self.testapp.authorization = (
  1768. 'Basic',
  1769. (
  1770. 'admin@admin.admin',
  1771. 'admin@admin.admin'
  1772. )
  1773. )
  1774. # check before
  1775. user = uapi.get_one(user_id)
  1776. assert user.validate_password('pass')
  1777. assert not user.validate_password('mynewpassword')
  1778. assert not user.validate_password('mynewpassword2')
  1779. # Set password
  1780. params = {
  1781. 'new_password': 'mynewpassword',
  1782. 'new_password2': 'mynewpassword2',
  1783. 'loggedin_user_password': 'admin@admin.admin',
  1784. }
  1785. self.testapp.put_json(
  1786. '/api/v2/users/{}/password'.format(user_id),
  1787. params=params,
  1788. status=400,
  1789. )
  1790. # Check After
  1791. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1792. uapi = UserApi(
  1793. current_user=admin,
  1794. session=dbsession,
  1795. config=self.app_config,
  1796. )
  1797. user = uapi.get_one(user_id)
  1798. assert user.validate_password('pass')
  1799. assert not user.validate_password('mynewpassword')
  1800. assert not user.validate_password('mynewpassword2')
  1801. def test_api__set_user_password__ok_200__user_itself(self):
  1802. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1803. admin = dbsession.query(models.User) \
  1804. .filter(models.User.email == 'admin@admin.admin') \
  1805. .one()
  1806. uapi = UserApi(
  1807. current_user=admin,
  1808. session=dbsession,
  1809. config=self.app_config,
  1810. )
  1811. gapi = GroupApi(
  1812. current_user=admin,
  1813. session=dbsession,
  1814. config=self.app_config,
  1815. )
  1816. groups = [gapi.get_one_with_name('users')]
  1817. test_user = uapi.create_user(
  1818. email='test@test.test',
  1819. password='pass',
  1820. name='bob',
  1821. groups=groups,
  1822. timezone='Europe/Paris',
  1823. do_save=True,
  1824. do_notify=False,
  1825. )
  1826. uapi.save(test_user)
  1827. transaction.commit()
  1828. user_id = int(test_user.user_id)
  1829. self.testapp.authorization = (
  1830. 'Basic',
  1831. (
  1832. 'test@test.test',
  1833. 'pass'
  1834. )
  1835. )
  1836. # check before
  1837. user = uapi.get_one(user_id)
  1838. assert user.validate_password('pass')
  1839. assert not user.validate_password('mynewpassword')
  1840. # Set password
  1841. params = {
  1842. 'new_password': 'mynewpassword',
  1843. 'new_password2': 'mynewpassword',
  1844. 'loggedin_user_password': 'pass',
  1845. }
  1846. self.testapp.put_json(
  1847. '/api/v2/users/{}/password'.format(user_id),
  1848. params=params,
  1849. status=204,
  1850. )
  1851. # Check After
  1852. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1853. uapi = UserApi(
  1854. current_user=admin,
  1855. session=dbsession,
  1856. config=self.app_config,
  1857. )
  1858. user = uapi.get_one(user_id)
  1859. assert not user.validate_password('pass')
  1860. assert user.validate_password('mynewpassword')
  1861. def test_api__set_user_email__err_403__other_normal_user(self):
  1862. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1863. admin = dbsession.query(models.User) \
  1864. .filter(models.User.email == 'admin@admin.admin') \
  1865. .one()
  1866. uapi = UserApi(
  1867. current_user=admin,
  1868. session=dbsession,
  1869. config=self.app_config,
  1870. )
  1871. gapi = GroupApi(
  1872. current_user=admin,
  1873. session=dbsession,
  1874. config=self.app_config,
  1875. )
  1876. groups = [gapi.get_one_with_name('users')]
  1877. test_user = uapi.create_user(
  1878. email='test@test.test',
  1879. password='pass',
  1880. name='bob',
  1881. groups=groups,
  1882. timezone='Europe/Paris',
  1883. do_save=True,
  1884. do_notify=False,
  1885. )
  1886. test_user2 = uapi.create_user(
  1887. email='test2@test2.test2',
  1888. password='pass',
  1889. name='bob2',
  1890. groups=groups,
  1891. timezone='Europe/Paris',
  1892. do_save=True,
  1893. do_notify=False,
  1894. )
  1895. uapi.save(test_user2)
  1896. uapi.save(test_user)
  1897. transaction.commit()
  1898. user_id = int(test_user.user_id)
  1899. self.testapp.authorization = (
  1900. 'Basic',
  1901. (
  1902. 'test@test.test',
  1903. 'pass'
  1904. )
  1905. )
  1906. # Set password
  1907. params = {
  1908. 'email': 'mysuperemail@email.fr',
  1909. 'loggedin_user_password': 'test2@test2.test2',
  1910. }
  1911. self.testapp.put_json(
  1912. '/api/v2/users/{}/email'.format(user_id),
  1913. params=params,
  1914. status=403,
  1915. )
  1916. class TestSetUserInfoEndpoint(FunctionalTest):
  1917. # -*- coding: utf-8 -*-
  1918. """
  1919. Tests for PUT /api/v2/users/{user_id}
  1920. """
  1921. fixtures = [BaseFixture]
  1922. def test_api__set_user_info__ok_200__admin(self):
  1923. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1924. admin = dbsession.query(models.User) \
  1925. .filter(models.User.email == 'admin@admin.admin') \
  1926. .one()
  1927. uapi = UserApi(
  1928. current_user=admin,
  1929. session=dbsession,
  1930. config=self.app_config,
  1931. )
  1932. gapi = GroupApi(
  1933. current_user=admin,
  1934. session=dbsession,
  1935. config=self.app_config,
  1936. )
  1937. groups = [gapi.get_one_with_name('users')]
  1938. test_user = uapi.create_user(
  1939. email='test@test.test',
  1940. password='pass',
  1941. name='bob',
  1942. groups=groups,
  1943. timezone='Europe/Paris',
  1944. do_save=True,
  1945. do_notify=False,
  1946. )
  1947. uapi.save(test_user)
  1948. transaction.commit()
  1949. user_id = int(test_user.user_id)
  1950. self.testapp.authorization = (
  1951. 'Basic',
  1952. (
  1953. 'admin@admin.admin',
  1954. 'admin@admin.admin'
  1955. )
  1956. )
  1957. # check before
  1958. res = self.testapp.get(
  1959. '/api/v2/users/{}'.format(user_id),
  1960. status=200
  1961. )
  1962. res = res.json_body
  1963. assert res['user_id'] == user_id
  1964. assert res['public_name'] == 'bob'
  1965. assert res['timezone'] == 'Europe/Paris'
  1966. # Set params
  1967. params = {
  1968. 'public_name': 'updated',
  1969. 'timezone': 'Europe/London',
  1970. }
  1971. self.testapp.put_json(
  1972. '/api/v2/users/{}'.format(user_id),
  1973. params=params,
  1974. status=200,
  1975. )
  1976. # Check After
  1977. res = self.testapp.get(
  1978. '/api/v2/users/{}'.format(user_id),
  1979. status=200
  1980. )
  1981. res = res.json_body
  1982. assert res['user_id'] == user_id
  1983. assert res['public_name'] == 'updated'
  1984. assert res['timezone'] == 'Europe/London'
  1985. def test_api__set_user_info__ok_200__user_itself(self):
  1986. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1987. admin = dbsession.query(models.User) \
  1988. .filter(models.User.email == 'admin@admin.admin') \
  1989. .one()
  1990. uapi = UserApi(
  1991. current_user=admin,
  1992. session=dbsession,
  1993. config=self.app_config,
  1994. )
  1995. gapi = GroupApi(
  1996. current_user=admin,
  1997. session=dbsession,
  1998. config=self.app_config,
  1999. )
  2000. groups = [gapi.get_one_with_name('users')]
  2001. test_user = uapi.create_user(
  2002. email='test@test.test',
  2003. password='pass',
  2004. name='bob',
  2005. groups=groups,
  2006. timezone='Europe/Paris',
  2007. do_save=True,
  2008. do_notify=False,
  2009. )
  2010. uapi.save(test_user)
  2011. transaction.commit()
  2012. user_id = int(test_user.user_id)
  2013. self.testapp.authorization = (
  2014. 'Basic',
  2015. (
  2016. 'test@test.test',
  2017. 'pass',
  2018. )
  2019. )
  2020. # check before
  2021. res = self.testapp.get(
  2022. '/api/v2/users/{}'.format(user_id),
  2023. status=200
  2024. )
  2025. res = res.json_body
  2026. assert res['user_id'] == user_id
  2027. assert res['public_name'] == 'bob'
  2028. assert res['timezone'] == 'Europe/Paris'
  2029. # Set params
  2030. params = {
  2031. 'public_name': 'updated',
  2032. 'timezone': 'Europe/London',
  2033. }
  2034. self.testapp.put_json(
  2035. '/api/v2/users/{}'.format(user_id),
  2036. params=params,
  2037. status=200,
  2038. )
  2039. # Check After
  2040. res = self.testapp.get(
  2041. '/api/v2/users/{}'.format(user_id),
  2042. status=200
  2043. )
  2044. res = res.json_body
  2045. assert res['user_id'] == user_id
  2046. assert res['public_name'] == 'updated'
  2047. assert res['timezone'] == 'Europe/London'
  2048. def test_api__set_user_email__err_403__other_normal_user(self):
  2049. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2050. admin = dbsession.query(models.User) \
  2051. .filter(models.User.email == 'admin@admin.admin') \
  2052. .one()
  2053. uapi = UserApi(
  2054. current_user=admin,
  2055. session=dbsession,
  2056. config=self.app_config,
  2057. )
  2058. gapi = GroupApi(
  2059. current_user=admin,
  2060. session=dbsession,
  2061. config=self.app_config,
  2062. )
  2063. groups = [gapi.get_one_with_name('users')]
  2064. test_user = uapi.create_user(
  2065. email='test@test.test',
  2066. password='pass',
  2067. name='bob',
  2068. groups=groups,
  2069. timezone='Europe/Paris',
  2070. do_save=True,
  2071. do_notify=False,
  2072. )
  2073. test_user2 = uapi.create_user(
  2074. email='test2@test2.test2',
  2075. password='pass',
  2076. name='test',
  2077. groups=groups,
  2078. timezone='Europe/Paris',
  2079. do_save=True,
  2080. do_notify=False,
  2081. )
  2082. uapi.save(test_user2)
  2083. uapi.save(test_user)
  2084. transaction.commit()
  2085. user_id = int(test_user.user_id)
  2086. self.testapp.authorization = (
  2087. 'Basic',
  2088. (
  2089. 'test2@test2.test2',
  2090. 'pass',
  2091. )
  2092. )
  2093. # Set params
  2094. params = {
  2095. 'public_name': 'updated',
  2096. 'timezone': 'Europe/London',
  2097. }
  2098. self.testapp.put_json(
  2099. '/api/v2/users/{}'.format(user_id),
  2100. params=params,
  2101. status=403,
  2102. )
  2103. class TestSetUserProfilEndpoint(FunctionalTest):
  2104. # -*- coding: utf-8 -*-
  2105. """
  2106. Tests for PUT /api/v2/users/{user_id}/profile
  2107. """
  2108. fixtures = [BaseFixture]
  2109. def test_api__set_user_info__ok_200__admin(self):
  2110. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2111. admin = dbsession.query(models.User) \
  2112. .filter(models.User.email == 'admin@admin.admin') \
  2113. .one()
  2114. uapi = UserApi(
  2115. current_user=admin,
  2116. session=dbsession,
  2117. config=self.app_config,
  2118. )
  2119. gapi = GroupApi(
  2120. current_user=admin,
  2121. session=dbsession,
  2122. config=self.app_config,
  2123. )
  2124. groups = [gapi.get_one_with_name('users')]
  2125. test_user = uapi.create_user(
  2126. email='test@test.test',
  2127. password='pass',
  2128. name='bob',
  2129. groups=groups,
  2130. timezone='Europe/Paris',
  2131. do_save=True,
  2132. do_notify=False,
  2133. )
  2134. uapi.save(test_user)
  2135. transaction.commit()
  2136. user_id = int(test_user.user_id)
  2137. self.testapp.authorization = (
  2138. 'Basic',
  2139. (
  2140. 'admin@admin.admin',
  2141. 'admin@admin.admin'
  2142. )
  2143. )
  2144. # check before
  2145. res = self.testapp.get(
  2146. '/api/v2/users/{}'.format(user_id),
  2147. status=200
  2148. )
  2149. res = res.json_body
  2150. assert res['user_id'] == user_id
  2151. assert res['profile'] == 'users'
  2152. # Set params
  2153. params = {
  2154. 'profile': 'administrators',
  2155. }
  2156. self.testapp.put_json(
  2157. '/api/v2/users/{}/profile'.format(user_id),
  2158. params=params,
  2159. status=204,
  2160. )
  2161. # Check After
  2162. res = self.testapp.get(
  2163. '/api/v2/users/{}'.format(user_id),
  2164. status=200
  2165. )
  2166. res = res.json_body
  2167. assert res['user_id'] == user_id
  2168. assert res['profile'] == 'administrators'
  2169. def test_api__set_user_info__err_403__user_itself(self):
  2170. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2171. admin = dbsession.query(models.User) \
  2172. .filter(models.User.email == 'admin@admin.admin') \
  2173. .one()
  2174. uapi = UserApi(
  2175. current_user=admin,
  2176. session=dbsession,
  2177. config=self.app_config,
  2178. )
  2179. gapi = GroupApi(
  2180. current_user=admin,
  2181. session=dbsession,
  2182. config=self.app_config,
  2183. )
  2184. groups = [gapi.get_one_with_name('users')]
  2185. test_user = uapi.create_user(
  2186. email='test@test.test',
  2187. password='pass',
  2188. name='bob',
  2189. groups=groups,
  2190. timezone='Europe/Paris',
  2191. do_save=True,
  2192. do_notify=False,
  2193. )
  2194. uapi.save(test_user)
  2195. transaction.commit()
  2196. user_id = int(test_user.user_id)
  2197. self.testapp.authorization = (
  2198. 'Basic',
  2199. (
  2200. 'test@test.test',
  2201. 'pass',
  2202. )
  2203. )
  2204. # check before
  2205. res = self.testapp.get(
  2206. '/api/v2/users/{}'.format(user_id),
  2207. status=200
  2208. )
  2209. res = res.json_body
  2210. assert res['user_id'] == user_id
  2211. assert res['profile'] == 'users'
  2212. # Set params
  2213. params = {
  2214. 'profile': 'administrators',
  2215. }
  2216. self.testapp.put_json(
  2217. '/api/v2/users/{}/profile'.format(user_id),
  2218. params=params,
  2219. status=403,
  2220. )
  2221. # Check After
  2222. res = self.testapp.get(
  2223. '/api/v2/users/{}'.format(user_id),
  2224. status=200
  2225. )
  2226. res = res.json_body
  2227. assert res['user_id'] == user_id
  2228. assert res['profile'] == 'users'
  2229. def test_api__set_user_email__err_403__other_normal_user(self):
  2230. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2231. admin = dbsession.query(models.User) \
  2232. .filter(models.User.email == 'admin@admin.admin') \
  2233. .one()
  2234. uapi = UserApi(
  2235. current_user=admin,
  2236. session=dbsession,
  2237. config=self.app_config,
  2238. )
  2239. gapi = GroupApi(
  2240. current_user=admin,
  2241. session=dbsession,
  2242. config=self.app_config,
  2243. )
  2244. groups = [gapi.get_one_with_name('users')]
  2245. test_user = uapi.create_user(
  2246. email='test@test.test',
  2247. password='pass',
  2248. name='bob',
  2249. groups=groups,
  2250. timezone='Europe/Paris',
  2251. do_save=True,
  2252. do_notify=False,
  2253. )
  2254. test_user2 = uapi.create_user(
  2255. email='test2@test2.test2',
  2256. password='pass',
  2257. name='test',
  2258. groups=groups,
  2259. timezone='Europe/Paris',
  2260. do_save=True,
  2261. do_notify=False,
  2262. )
  2263. uapi.save(test_user2)
  2264. uapi.save(test_user)
  2265. transaction.commit()
  2266. user_id = int(test_user.user_id)
  2267. self.testapp.authorization = (
  2268. 'Basic',
  2269. (
  2270. 'test2@test2.test2',
  2271. 'pass',
  2272. )
  2273. )
  2274. # Set params
  2275. params = {
  2276. 'profile': 'administrators',
  2277. }
  2278. self.testapp.put_json(
  2279. '/api/v2/users/{}/profile'.format(user_id),
  2280. params=params,
  2281. status=403,
  2282. )
  2283. class TestSetUserEnableDisableEndpoints(FunctionalTest):
  2284. # -*- coding: utf-8 -*-
  2285. """
  2286. Tests for PUT /api/v2/users/{user_id}/enable
  2287. and PUT /api/v2/users/{user_id}/disable
  2288. """
  2289. fixtures = [BaseFixture]
  2290. def test_api_enable_user__ok_200__admin(self):
  2291. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2292. admin = dbsession.query(models.User) \
  2293. .filter(models.User.email == 'admin@admin.admin') \
  2294. .one()
  2295. uapi = UserApi(
  2296. current_user=admin,
  2297. session=dbsession,
  2298. config=self.app_config,
  2299. )
  2300. gapi = GroupApi(
  2301. current_user=admin,
  2302. session=dbsession,
  2303. config=self.app_config,
  2304. )
  2305. groups = [gapi.get_one_with_name('users')]
  2306. test_user = uapi.create_user(
  2307. email='test@test.test',
  2308. password='pass',
  2309. name='bob',
  2310. groups=groups,
  2311. timezone='Europe/Paris',
  2312. do_save=True,
  2313. do_notify=False,
  2314. )
  2315. uapi.disable(test_user, do_save=True)
  2316. uapi.save(test_user)
  2317. transaction.commit()
  2318. user_id = int(test_user.user_id)
  2319. self.testapp.authorization = (
  2320. 'Basic',
  2321. (
  2322. 'admin@admin.admin',
  2323. 'admin@admin.admin'
  2324. )
  2325. )
  2326. # check before
  2327. res = self.testapp.get(
  2328. '/api/v2/users/{}'.format(user_id),
  2329. status=200
  2330. )
  2331. res = res.json_body
  2332. assert res['user_id'] == user_id
  2333. assert res['is_active'] is False
  2334. self.testapp.put_json(
  2335. '/api/v2/users/{}/enable'.format(user_id),
  2336. status=204,
  2337. )
  2338. # Check After
  2339. res = self.testapp.get(
  2340. '/api/v2/users/{}'.format(user_id),
  2341. status=200
  2342. )
  2343. res = res.json_body
  2344. assert res['user_id'] == user_id
  2345. assert res['is_active'] is True
  2346. def test_api_disable_user__ok_200__admin(self):
  2347. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2348. admin = dbsession.query(models.User) \
  2349. .filter(models.User.email == 'admin@admin.admin') \
  2350. .one()
  2351. uapi = UserApi(
  2352. current_user=admin,
  2353. session=dbsession,
  2354. config=self.app_config,
  2355. )
  2356. gapi = GroupApi(
  2357. current_user=admin,
  2358. session=dbsession,
  2359. config=self.app_config,
  2360. )
  2361. groups = [gapi.get_one_with_name('users')]
  2362. test_user = uapi.create_user(
  2363. email='test@test.test',
  2364. password='pass',
  2365. name='bob',
  2366. groups=groups,
  2367. timezone='Europe/Paris',
  2368. do_save=True,
  2369. do_notify=False,
  2370. )
  2371. uapi.enable(test_user, do_save=True)
  2372. uapi.save(test_user)
  2373. transaction.commit()
  2374. user_id = int(test_user.user_id)
  2375. self.testapp.authorization = (
  2376. 'Basic',
  2377. (
  2378. 'admin@admin.admin',
  2379. 'admin@admin.admin'
  2380. )
  2381. )
  2382. # check before
  2383. res = self.testapp.get(
  2384. '/api/v2/users/{}'.format(user_id),
  2385. status=200
  2386. )
  2387. res = res.json_body
  2388. assert res['user_id'] == user_id
  2389. assert res['is_active'] is True
  2390. self.testapp.put_json(
  2391. '/api/v2/users/{}/disable'.format(user_id),
  2392. status=204,
  2393. )
  2394. # Check After
  2395. res = self.testapp.get(
  2396. '/api/v2/users/{}'.format(user_id),
  2397. status=200
  2398. )
  2399. res = res.json_body
  2400. assert res['user_id'] == user_id
  2401. assert res['is_active'] is False
  2402. def test_api_enable_user__err_403__other_account(self):
  2403. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2404. admin = dbsession.query(models.User) \
  2405. .filter(models.User.email == 'admin@admin.admin') \
  2406. .one()
  2407. uapi = UserApi(
  2408. current_user=admin,
  2409. session=dbsession,
  2410. config=self.app_config,
  2411. )
  2412. gapi = GroupApi(
  2413. current_user=admin,
  2414. session=dbsession,
  2415. config=self.app_config,
  2416. )
  2417. groups = [gapi.get_one_with_name('users')]
  2418. test_user = uapi.create_user(
  2419. email='test@test.test',
  2420. password='pass',
  2421. name='bob',
  2422. groups=groups,
  2423. timezone='Europe/Paris',
  2424. do_save=True,
  2425. do_notify=False,
  2426. )
  2427. test_user2 = uapi.create_user(
  2428. email='test2@test2.test2',
  2429. password='pass',
  2430. name='test2',
  2431. groups=groups,
  2432. timezone='Europe/Paris',
  2433. do_save=True,
  2434. do_notify=False,
  2435. )
  2436. uapi.disable(test_user, do_save=True)
  2437. uapi.save(test_user2)
  2438. uapi.save(test_user)
  2439. transaction.commit()
  2440. user_id = int(test_user.user_id)
  2441. self.testapp.authorization = (
  2442. 'Basic',
  2443. (
  2444. 'test2@test2.test2',
  2445. 'pass'
  2446. )
  2447. )
  2448. self.testapp.put_json(
  2449. '/api/v2/users/{}/enable'.format(user_id),
  2450. status=403,
  2451. )
  2452. def test_api_disable_user__err_403__other_account(self):
  2453. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2454. admin = dbsession.query(models.User) \
  2455. .filter(models.User.email == 'admin@admin.admin') \
  2456. .one()
  2457. uapi = UserApi(
  2458. current_user=admin,
  2459. session=dbsession,
  2460. config=self.app_config,
  2461. )
  2462. gapi = GroupApi(
  2463. current_user=admin,
  2464. session=dbsession,
  2465. config=self.app_config,
  2466. )
  2467. groups = [gapi.get_one_with_name('users')]
  2468. test_user = uapi.create_user(
  2469. email='test@test.test',
  2470. password='pass',
  2471. name='bob',
  2472. groups=groups,
  2473. timezone='Europe/Paris',
  2474. do_save=True,
  2475. do_notify=False,
  2476. )
  2477. test_user2 = uapi.create_user(
  2478. email='test2@test2.test2',
  2479. password='pass',
  2480. name='test2',
  2481. groups=groups,
  2482. timezone='Europe/Paris',
  2483. do_save=True,
  2484. do_notify=False,
  2485. )
  2486. uapi.enable(test_user, do_save=True)
  2487. uapi.save(test_user2)
  2488. uapi.save(test_user)
  2489. transaction.commit()
  2490. user_id = int(test_user.user_id)
  2491. self.testapp.authorization = (
  2492. 'Basic',
  2493. (
  2494. 'test2@test2.test2',
  2495. 'pass'
  2496. )
  2497. )
  2498. self.testapp.put_json(
  2499. '/api/v2/users/{}/disable'.format(user_id),
  2500. status=403,
  2501. )
  2502. def test_api_disable_user__ok_200__user_itself(self):
  2503. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2504. admin = dbsession.query(models.User) \
  2505. .filter(models.User.email == 'admin@admin.admin') \
  2506. .one()
  2507. uapi = UserApi(
  2508. current_user=admin,
  2509. session=dbsession,
  2510. config=self.app_config,
  2511. )
  2512. gapi = GroupApi(
  2513. current_user=admin,
  2514. session=dbsession,
  2515. config=self.app_config,
  2516. )
  2517. groups = [gapi.get_one_with_name('users')]
  2518. test_user = uapi.create_user(
  2519. email='test@test.test',
  2520. password='pass',
  2521. name='bob',
  2522. groups=groups,
  2523. timezone='Europe/Paris',
  2524. do_save=True,
  2525. do_notify=False,
  2526. )
  2527. uapi.enable(test_user, do_save=True)
  2528. uapi.save(test_user)
  2529. transaction.commit()
  2530. user_id = int(test_user.user_id)
  2531. self.testapp.authorization = (
  2532. 'Basic',
  2533. (
  2534. 'test@test.test',
  2535. 'pass'
  2536. )
  2537. )
  2538. # check before
  2539. res = self.testapp.get(
  2540. '/api/v2/users/{}'.format(user_id),
  2541. status=200
  2542. )
  2543. res = res.json_body
  2544. assert res['user_id'] == user_id
  2545. assert res['is_active'] is True
  2546. self.testapp.put_json(
  2547. '/api/v2/users/{}/disable'.format(user_id),
  2548. status=403,
  2549. )
  2550. # Check After
  2551. res = self.testapp.get(
  2552. '/api/v2/users/{}'.format(user_id),
  2553. status=200
  2554. )
  2555. res = res.json_body
  2556. assert res['user_id'] == user_id
  2557. assert res['is_active'] is True