test_user.py 150KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327
  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 CONTENT_TYPES
  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__admin(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. uapi = UserApi(
  54. current_user=admin,
  55. session=dbsession,
  56. config=self.app_config,
  57. )
  58. gapi = GroupApi(
  59. current_user=admin,
  60. session=dbsession,
  61. config=self.app_config,
  62. )
  63. groups = [gapi.get_one_with_name('users')]
  64. test_user = uapi.create_user(
  65. email='test@test.test',
  66. password='pass',
  67. name='bob',
  68. groups=groups,
  69. timezone='Europe/Paris',
  70. do_save=True,
  71. do_notify=False,
  72. )
  73. rapi = RoleApi(
  74. current_user=admin,
  75. session=dbsession,
  76. config=self.app_config,
  77. )
  78. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  79. api = ContentApi(
  80. current_user=admin,
  81. session=dbsession,
  82. config=self.app_config,
  83. )
  84. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  85. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  86. # creation order test
  87. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  88. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  89. # update order test
  90. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  91. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  92. with new_revision(
  93. session=dbsession,
  94. tm=transaction.manager,
  95. content=firstly_created_but_recently_updated,
  96. ):
  97. firstly_created_but_recently_updated.description = 'Just an update'
  98. api.save(firstly_created_but_recently_updated)
  99. # comment change order
  100. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  101. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  102. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  103. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  104. dbsession.flush()
  105. transaction.commit()
  106. self.testapp.authorization = (
  107. 'Basic',
  108. (
  109. 'admin@admin.admin',
  110. 'admin@admin.admin'
  111. )
  112. )
  113. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/recently_active'.format( # nopep8
  114. user_id=test_user.user_id,
  115. workspace_id=workspace.workspace_id
  116. ), status=200)
  117. res = res.json_body
  118. assert len(res) == 7
  119. for elem in res:
  120. assert isinstance(elem['content_id'], int)
  121. assert isinstance(elem['content_type'], str)
  122. assert elem['content_type'] != 'comments'
  123. assert isinstance(elem['is_archived'], bool)
  124. assert isinstance(elem['is_deleted'], bool)
  125. assert isinstance(elem['label'], str)
  126. assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
  127. assert isinstance(elem['show_in_ui'], bool)
  128. assert isinstance(elem['slug'], str)
  129. assert isinstance(elem['status'], str)
  130. assert isinstance(elem['sub_content_types'], list)
  131. for sub_content_type in elem['sub_content_types']:
  132. assert isinstance(sub_content_type, str)
  133. assert isinstance(elem['workspace_id'], int)
  134. # comment is newest than page2
  135. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  136. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  137. # last updated content is newer than other one despite creation
  138. # of the other is more recent
  139. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  140. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  141. # creation order is inverted here as last created is last active
  142. assert res[4]['content_id'] == secondly_created.content_id
  143. assert res[5]['content_id'] == firstly_created.content_id
  144. # folder subcontent modification does not change folder order
  145. assert res[6]['content_id'] == main_folder.content_id
  146. def test_api__get_recently_active_content__err__400__no_access_to_workspace(self):
  147. # init DB
  148. dbsession = get_tm_session(self.session_factory, transaction.manager)
  149. admin = dbsession.query(models.User) \
  150. .filter(models.User.email == 'admin@admin.admin') \
  151. .one()
  152. workspace_api = WorkspaceApi(
  153. current_user=admin,
  154. session=dbsession,
  155. config=self.app_config
  156. )
  157. workspace = WorkspaceApi(
  158. current_user=admin,
  159. session=dbsession,
  160. config=self.app_config,
  161. ).create_workspace(
  162. 'test workspace',
  163. save_now=True
  164. )
  165. workspace2 = WorkspaceApi(
  166. current_user=admin,
  167. session=dbsession,
  168. config=self.app_config,
  169. ).create_workspace(
  170. 'test workspace2',
  171. save_now=True
  172. )
  173. uapi = UserApi(
  174. current_user=admin,
  175. session=dbsession,
  176. config=self.app_config,
  177. )
  178. gapi = GroupApi(
  179. current_user=admin,
  180. session=dbsession,
  181. config=self.app_config,
  182. )
  183. groups = [gapi.get_one_with_name('users')]
  184. test_user = uapi.create_user(
  185. email='test@test.test',
  186. password='pass',
  187. name='bob',
  188. groups=groups,
  189. timezone='Europe/Paris',
  190. do_save=True,
  191. do_notify=False,
  192. )
  193. api = ContentApi(
  194. current_user=admin,
  195. session=dbsession,
  196. config=self.app_config,
  197. )
  198. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  199. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  200. # creation order test
  201. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  202. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  203. # update order test
  204. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  205. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  206. with new_revision(
  207. session=dbsession,
  208. tm=transaction.manager,
  209. content=firstly_created_but_recently_updated,
  210. ):
  211. firstly_created_but_recently_updated.description = 'Just an update'
  212. api.save(firstly_created_but_recently_updated)
  213. # comment change order
  214. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  215. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  216. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  217. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  218. dbsession.flush()
  219. transaction.commit()
  220. self.testapp.authorization = (
  221. 'Basic',
  222. (
  223. 'admin@admin.admin',
  224. 'admin@admin.admin'
  225. )
  226. )
  227. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/recently_active'.format( # nopep8
  228. user_id=test_user.user_id,
  229. workspace_id=workspace.workspace_id
  230. ), status=400)
  231. def test_api__get_recently_active_content__ok__200__user_itself(self):
  232. # init DB
  233. dbsession = get_tm_session(self.session_factory, transaction.manager)
  234. admin = dbsession.query(models.User) \
  235. .filter(models.User.email == 'admin@admin.admin') \
  236. .one()
  237. workspace_api = WorkspaceApi(
  238. current_user=admin,
  239. session=dbsession,
  240. config=self.app_config
  241. )
  242. workspace = WorkspaceApi(
  243. current_user=admin,
  244. session=dbsession,
  245. config=self.app_config,
  246. ).create_workspace(
  247. 'test workspace',
  248. save_now=True
  249. )
  250. workspace2 = WorkspaceApi(
  251. current_user=admin,
  252. session=dbsession,
  253. config=self.app_config,
  254. ).create_workspace(
  255. 'test workspace2',
  256. save_now=True
  257. )
  258. uapi = UserApi(
  259. current_user=admin,
  260. session=dbsession,
  261. config=self.app_config,
  262. )
  263. gapi = GroupApi(
  264. current_user=admin,
  265. session=dbsession,
  266. config=self.app_config,
  267. )
  268. groups = [gapi.get_one_with_name('users')]
  269. test_user = uapi.create_user(
  270. email='test@test.test',
  271. password='pass',
  272. name='bob',
  273. groups=groups,
  274. timezone='Europe/Paris',
  275. do_save=True,
  276. do_notify=False,
  277. )
  278. rapi = RoleApi(
  279. current_user=admin,
  280. session=dbsession,
  281. config=self.app_config,
  282. )
  283. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  284. api = ContentApi(
  285. current_user=admin,
  286. session=dbsession,
  287. config=self.app_config,
  288. )
  289. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  290. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  291. # creation order test
  292. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  293. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  294. # update order test
  295. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  296. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  297. with new_revision(
  298. session=dbsession,
  299. tm=transaction.manager,
  300. content=firstly_created_but_recently_updated,
  301. ):
  302. firstly_created_but_recently_updated.description = 'Just an update'
  303. api.save(firstly_created_but_recently_updated)
  304. # comment change order
  305. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  306. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  307. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  308. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  309. dbsession.flush()
  310. transaction.commit()
  311. self.testapp.authorization = (
  312. 'Basic',
  313. (
  314. 'test@test.test',
  315. 'pass'
  316. )
  317. )
  318. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/recently_active'.format( # nopep8
  319. user_id=test_user.user_id,
  320. workspace_id=workspace.workspace_id
  321. ), status=200)
  322. res = res.json_body
  323. assert len(res) == 7
  324. for elem in res:
  325. assert isinstance(elem['content_id'], int)
  326. assert isinstance(elem['content_type'], str)
  327. assert elem['content_type'] != 'comments'
  328. assert isinstance(elem['is_archived'], bool)
  329. assert isinstance(elem['is_deleted'], bool)
  330. assert isinstance(elem['label'], str)
  331. assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
  332. assert isinstance(elem['show_in_ui'], bool)
  333. assert isinstance(elem['slug'], str)
  334. assert isinstance(elem['status'], str)
  335. assert isinstance(elem['sub_content_types'], list)
  336. for sub_content_type in elem['sub_content_types']:
  337. assert isinstance(sub_content_type, str)
  338. assert isinstance(elem['workspace_id'], int)
  339. # comment is newest than page2
  340. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  341. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  342. # last updated content is newer than other one despite creation
  343. # of the other is more recent
  344. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  345. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  346. # creation order is inverted here as last created is last active
  347. assert res[4]['content_id'] == secondly_created.content_id
  348. assert res[5]['content_id'] == firstly_created.content_id
  349. # folder subcontent modification does not change folder order
  350. assert res[6]['content_id'] == main_folder.content_id
  351. def test_api__get_recently_active_content__ok__200__other_user(self):
  352. # init DB
  353. dbsession = get_tm_session(self.session_factory, transaction.manager)
  354. admin = dbsession.query(models.User) \
  355. .filter(models.User.email == 'admin@admin.admin') \
  356. .one()
  357. workspace_api = WorkspaceApi(
  358. current_user=admin,
  359. session=dbsession,
  360. config=self.app_config
  361. )
  362. workspace = WorkspaceApi(
  363. current_user=admin,
  364. session=dbsession,
  365. config=self.app_config,
  366. ).create_workspace(
  367. 'test workspace',
  368. save_now=True
  369. )
  370. workspace2 = WorkspaceApi(
  371. current_user=admin,
  372. session=dbsession,
  373. config=self.app_config,
  374. ).create_workspace(
  375. 'test workspace2',
  376. save_now=True
  377. )
  378. uapi = UserApi(
  379. current_user=admin,
  380. session=dbsession,
  381. config=self.app_config,
  382. )
  383. gapi = GroupApi(
  384. current_user=admin,
  385. session=dbsession,
  386. config=self.app_config,
  387. )
  388. groups = [gapi.get_one_with_name('users')]
  389. test_user = uapi.create_user(
  390. email='test@test.test',
  391. password='pass',
  392. name='bob',
  393. groups=groups,
  394. timezone='Europe/Paris',
  395. do_save=True,
  396. do_notify=False,
  397. )
  398. rapi = RoleApi(
  399. current_user=admin,
  400. session=dbsession,
  401. config=self.app_config,
  402. )
  403. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  404. api = ContentApi(
  405. current_user=admin,
  406. session=dbsession,
  407. config=self.app_config,
  408. )
  409. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  410. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  411. # creation order test
  412. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  413. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  414. # update order test
  415. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  416. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  417. with new_revision(
  418. session=dbsession,
  419. tm=transaction.manager,
  420. content=firstly_created_but_recently_updated,
  421. ):
  422. firstly_created_but_recently_updated.description = 'Just an update'
  423. api.save(firstly_created_but_recently_updated)
  424. # comment change order
  425. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  426. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  427. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  428. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  429. dbsession.flush()
  430. transaction.commit()
  431. self.testapp.authorization = (
  432. 'Basic',
  433. (
  434. 'test@test.test',
  435. 'pass'
  436. )
  437. )
  438. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/recently_active'.format( # nopep8
  439. user_id=admin.user_id,
  440. workspace_id=workspace.workspace_id
  441. ), status=403)
  442. def test_api__get_recently_active_content__ok__200__limit_2_multiple(self):
  443. # TODO - G.M - 2018-07-20 - Better fix for this test, do not use sleep()
  444. # anymore to fix datetime lack of precision.
  445. # init DB
  446. dbsession = get_tm_session(self.session_factory, transaction.manager)
  447. admin = dbsession.query(models.User) \
  448. .filter(models.User.email == 'admin@admin.admin') \
  449. .one()
  450. workspace_api = WorkspaceApi(
  451. current_user=admin,
  452. session=dbsession,
  453. config=self.app_config
  454. )
  455. workspace = WorkspaceApi(
  456. current_user=admin,
  457. session=dbsession,
  458. config=self.app_config,
  459. ).create_workspace(
  460. 'test workspace',
  461. save_now=True
  462. )
  463. workspace2 = WorkspaceApi(
  464. current_user=admin,
  465. session=dbsession,
  466. config=self.app_config,
  467. ).create_workspace(
  468. 'test workspace2',
  469. save_now=True
  470. )
  471. api = ContentApi(
  472. current_user=admin,
  473. session=dbsession,
  474. config=self.app_config,
  475. )
  476. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  477. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  478. # creation order test
  479. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  480. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  481. # update order test
  482. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  483. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  484. with new_revision(
  485. session=dbsession,
  486. tm=transaction.manager,
  487. content=firstly_created_but_recently_updated,
  488. ):
  489. firstly_created_but_recently_updated.description = 'Just an update'
  490. api.save(firstly_created_but_recently_updated)
  491. # comment change order
  492. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  493. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  494. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  495. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  496. dbsession.flush()
  497. transaction.commit()
  498. self.testapp.authorization = (
  499. 'Basic',
  500. (
  501. 'admin@admin.admin',
  502. 'admin@admin.admin'
  503. )
  504. )
  505. params = {
  506. 'limit': 2,
  507. }
  508. res = self.testapp.get(
  509. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  510. status=200,
  511. params=params
  512. ) # nopep8
  513. res = res.json_body
  514. assert len(res) == 2
  515. for elem in res:
  516. assert isinstance(elem['content_id'], int)
  517. assert isinstance(elem['content_type'], str)
  518. assert elem['content_type'] != 'comments'
  519. assert isinstance(elem['is_archived'], bool)
  520. assert isinstance(elem['is_deleted'], bool)
  521. assert isinstance(elem['label'], str)
  522. assert isinstance(elem['parent_id'], int) or elem['parent_id'] is None
  523. assert isinstance(elem['show_in_ui'], bool)
  524. assert isinstance(elem['slug'], str)
  525. assert isinstance(elem['status'], str)
  526. assert isinstance(elem['sub_content_types'], list)
  527. for sub_content_type in elem['sub_content_types']:
  528. assert isinstance(sub_content_type, str)
  529. assert isinstance(elem['workspace_id'], int)
  530. # comment is newest than page2
  531. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  532. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  533. params = {
  534. 'limit': 2,
  535. 'before_content_id': secondly_created_but_not_commented.content_id, # nopep8
  536. }
  537. res = self.testapp.get(
  538. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  539. status=200,
  540. params=params
  541. )
  542. res = res.json_body
  543. assert len(res) == 2
  544. # last updated content is newer than other one despite creation
  545. # of the other is more recent
  546. assert res[0]['content_id'] == firstly_created_but_recently_updated.content_id
  547. assert res[1]['content_id'] == secondly_created_but_not_updated.content_id
  548. def test_api__get_recently_active_content__ok__200__bad_before_content_id_doesnt_exist(self): # nopep8
  549. # TODO - G.M - 2018-07-20 - Better fix for this test, do not use sleep()
  550. # anymore to fix datetime lack of precision.
  551. # init DB
  552. dbsession = get_tm_session(self.session_factory, transaction.manager)
  553. admin = dbsession.query(models.User) \
  554. .filter(models.User.email == 'admin@admin.admin') \
  555. .one()
  556. workspace_api = WorkspaceApi(
  557. current_user=admin,
  558. session=dbsession,
  559. config=self.app_config
  560. )
  561. workspace = WorkspaceApi(
  562. current_user=admin,
  563. session=dbsession,
  564. config=self.app_config,
  565. ).create_workspace(
  566. 'test workspace',
  567. save_now=True
  568. )
  569. workspace2 = WorkspaceApi(
  570. current_user=admin,
  571. session=dbsession,
  572. config=self.app_config,
  573. ).create_workspace(
  574. 'test workspace2',
  575. save_now=True
  576. )
  577. api = ContentApi(
  578. current_user=admin,
  579. session=dbsession,
  580. config=self.app_config,
  581. )
  582. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  583. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  584. # creation order test
  585. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  586. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  587. # update order test
  588. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  589. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  590. with new_revision(
  591. session=dbsession,
  592. tm=transaction.manager,
  593. content=firstly_created_but_recently_updated,
  594. ):
  595. firstly_created_but_recently_updated.description = 'Just an update'
  596. api.save(firstly_created_but_recently_updated)
  597. # comment change order
  598. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  599. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  600. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  601. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  602. dbsession.flush()
  603. transaction.commit()
  604. self.testapp.authorization = (
  605. 'Basic',
  606. (
  607. 'admin@admin.admin',
  608. 'admin@admin.admin'
  609. )
  610. )
  611. params = {
  612. 'before_content_id': 4000
  613. }
  614. res = self.testapp.get(
  615. '/api/v2/users/1/workspaces/{}/contents/recently_active'.format(workspace.workspace_id), # nopep8
  616. status=400,
  617. params=params
  618. )
  619. class TestUserReadStatusEndpoint(FunctionalTest):
  620. """
  621. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status # nopep8
  622. """
  623. def test_api__get_read_status__ok__200__admin(self):
  624. # init DB
  625. dbsession = get_tm_session(self.session_factory, transaction.manager)
  626. admin = dbsession.query(models.User) \
  627. .filter(models.User.email == 'admin@admin.admin') \
  628. .one()
  629. workspace_api = WorkspaceApi(
  630. current_user=admin,
  631. session=dbsession,
  632. config=self.app_config
  633. )
  634. workspace = WorkspaceApi(
  635. current_user=admin,
  636. session=dbsession,
  637. config=self.app_config,
  638. ).create_workspace(
  639. 'test workspace',
  640. save_now=True
  641. )
  642. workspace2 = WorkspaceApi(
  643. current_user=admin,
  644. session=dbsession,
  645. config=self.app_config,
  646. ).create_workspace(
  647. 'test workspace2',
  648. save_now=True
  649. )
  650. uapi = UserApi(
  651. current_user=admin,
  652. session=dbsession,
  653. config=self.app_config,
  654. )
  655. gapi = GroupApi(
  656. current_user=admin,
  657. session=dbsession,
  658. config=self.app_config,
  659. )
  660. groups = [gapi.get_one_with_name('users')]
  661. test_user = uapi.create_user(
  662. email='test@test.test',
  663. password='pass',
  664. name='bob',
  665. groups=groups,
  666. timezone='Europe/Paris',
  667. do_save=True,
  668. do_notify=False,
  669. )
  670. rapi = RoleApi(
  671. current_user=admin,
  672. session=dbsession,
  673. config=self.app_config,
  674. )
  675. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  676. api = ContentApi(
  677. current_user=admin,
  678. session=dbsession,
  679. config=self.app_config,
  680. )
  681. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  682. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  683. # creation order test
  684. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  685. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  686. # update order test
  687. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  688. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  689. with new_revision(
  690. session=dbsession,
  691. tm=transaction.manager,
  692. content=firstly_created_but_recently_updated,
  693. ):
  694. firstly_created_but_recently_updated.description = 'Just an update'
  695. api.save(firstly_created_but_recently_updated)
  696. # comment change order
  697. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  698. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  699. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  700. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  701. dbsession.flush()
  702. transaction.commit()
  703. self.testapp.authorization = (
  704. 'Basic',
  705. (
  706. 'admin@admin.admin',
  707. 'admin@admin.admin'
  708. )
  709. )
  710. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  711. user_id=admin.user_id,
  712. workspace_id=workspace.workspace_id
  713. ), status=200)
  714. res = res.json_body
  715. assert len(res) == 7
  716. for elem in res:
  717. assert isinstance(elem['content_id'], int)
  718. assert isinstance(elem['read_by_user'], bool)
  719. # comment is newest than page2
  720. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  721. assert res[1]['content_id'] == secondly_created_but_not_commented.content_id
  722. # last updated content is newer than other one despite creation
  723. # of the other is more recent
  724. assert res[2]['content_id'] == firstly_created_but_recently_updated.content_id
  725. assert res[3]['content_id'] == secondly_created_but_not_updated.content_id
  726. # creation order is inverted here as last created is last active
  727. assert res[4]['content_id'] == secondly_created.content_id
  728. assert res[5]['content_id'] == firstly_created.content_id
  729. # folder subcontent modification does not change folder order
  730. assert res[6]['content_id'] == main_folder.content_id
  731. def test_api__get_read_status__ok__200__user_itself(self):
  732. # init DB
  733. dbsession = get_tm_session(self.session_factory, transaction.manager)
  734. admin = dbsession.query(models.User) \
  735. .filter(models.User.email == 'admin@admin.admin') \
  736. .one()
  737. workspace_api = WorkspaceApi(
  738. current_user=admin,
  739. session=dbsession,
  740. config=self.app_config
  741. )
  742. workspace = WorkspaceApi(
  743. current_user=admin,
  744. session=dbsession,
  745. config=self.app_config,
  746. ).create_workspace(
  747. 'test workspace',
  748. save_now=True
  749. )
  750. workspace2 = WorkspaceApi(
  751. current_user=admin,
  752. session=dbsession,
  753. config=self.app_config,
  754. ).create_workspace(
  755. 'test workspace2',
  756. save_now=True
  757. )
  758. uapi = UserApi(
  759. current_user=admin,
  760. session=dbsession,
  761. config=self.app_config,
  762. )
  763. gapi = GroupApi(
  764. current_user=admin,
  765. session=dbsession,
  766. config=self.app_config,
  767. )
  768. groups = [gapi.get_one_with_name('users')]
  769. test_user = uapi.create_user(
  770. email='test@test.test',
  771. password='pass',
  772. name='bob',
  773. groups=groups,
  774. timezone='Europe/Paris',
  775. do_save=True,
  776. do_notify=False,
  777. )
  778. rapi = RoleApi(
  779. current_user=admin,
  780. session=dbsession,
  781. config=self.app_config,
  782. )
  783. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  784. api = ContentApi(
  785. current_user=admin,
  786. session=dbsession,
  787. config=self.app_config,
  788. )
  789. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  790. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  791. # creation order test
  792. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  793. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  794. # update order test
  795. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  796. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  797. with new_revision(
  798. session=dbsession,
  799. tm=transaction.manager,
  800. content=firstly_created_but_recently_updated,
  801. ):
  802. firstly_created_but_recently_updated.description = 'Just an update'
  803. api.save(firstly_created_but_recently_updated)
  804. # comment change order
  805. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  806. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  807. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  808. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  809. dbsession.flush()
  810. transaction.commit()
  811. self.testapp.authorization = (
  812. 'Basic',
  813. (
  814. 'test@test.test',
  815. 'pass'
  816. )
  817. )
  818. selected_contents_id = [
  819. firstly_created_but_recently_commented.content_id,
  820. firstly_created_but_recently_updated.content_id,
  821. firstly_created.content_id,
  822. main_folder.content_id,
  823. ]
  824. url = '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status?contents_ids={cid1}&contents_ids={cid2}&contents_ids={cid3}&contents_ids={cid4}'.format( # nopep8
  825. workspace_id=workspace.workspace_id,
  826. cid1=selected_contents_id[0],
  827. cid2=selected_contents_id[1],
  828. cid3=selected_contents_id[2],
  829. cid4=selected_contents_id[3],
  830. user_id=test_user.user_id,
  831. )
  832. res = self.testapp.get(
  833. url=url,
  834. status=200,
  835. )
  836. res = res.json_body
  837. assert len(res) == 4
  838. for elem in res:
  839. assert isinstance(elem['content_id'], int)
  840. assert isinstance(elem['read_by_user'], bool)
  841. # comment is newest than page2
  842. assert res[0]['content_id'] == firstly_created_but_recently_commented.content_id
  843. # last updated content is newer than other one despite creation
  844. # of the other is more recent
  845. assert res[1]['content_id'] == firstly_created_but_recently_updated.content_id
  846. # creation order is inverted here as last created is last active
  847. assert res[2]['content_id'] == firstly_created.content_id
  848. # folder subcontent modification does not change folder order
  849. assert res[3]['content_id'] == main_folder.content_id
  850. def test_api__get_read_status__ok__200__other_user(self):
  851. # init DB
  852. dbsession = get_tm_session(self.session_factory, transaction.manager)
  853. admin = dbsession.query(models.User) \
  854. .filter(models.User.email == 'admin@admin.admin') \
  855. .one()
  856. workspace_api = WorkspaceApi(
  857. current_user=admin,
  858. session=dbsession,
  859. config=self.app_config
  860. )
  861. workspace = WorkspaceApi(
  862. current_user=admin,
  863. session=dbsession,
  864. config=self.app_config,
  865. ).create_workspace(
  866. 'test workspace',
  867. save_now=True
  868. )
  869. workspace2 = WorkspaceApi(
  870. current_user=admin,
  871. session=dbsession,
  872. config=self.app_config,
  873. ).create_workspace(
  874. 'test workspace2',
  875. save_now=True
  876. )
  877. uapi = UserApi(
  878. current_user=admin,
  879. session=dbsession,
  880. config=self.app_config,
  881. )
  882. gapi = GroupApi(
  883. current_user=admin,
  884. session=dbsession,
  885. config=self.app_config,
  886. )
  887. groups = [gapi.get_one_with_name('users')]
  888. test_user = uapi.create_user(
  889. email='test@test.test',
  890. password='pass',
  891. name='bob',
  892. groups=groups,
  893. timezone='Europe/Paris',
  894. do_save=True,
  895. do_notify=False,
  896. )
  897. rapi = RoleApi(
  898. current_user=admin,
  899. session=dbsession,
  900. config=self.app_config,
  901. )
  902. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  903. api = ContentApi(
  904. current_user=admin,
  905. session=dbsession,
  906. config=self.app_config,
  907. )
  908. main_folder_workspace2 = api.create(CONTENT_TYPES.Folder.slug, workspace2, None, 'Hepla', '', True) # nopep8
  909. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  910. # creation order test
  911. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  912. secondly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another creation_order_test', '', True) # nopep8
  913. # update order test
  914. firstly_created_but_recently_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'update_order_test', '', True) # nopep8
  915. secondly_created_but_not_updated = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'another update_order_test', '', True) # nopep8
  916. with new_revision(
  917. session=dbsession,
  918. tm=transaction.manager,
  919. content=firstly_created_but_recently_updated,
  920. ):
  921. firstly_created_but_recently_updated.description = 'Just an update'
  922. api.save(firstly_created_but_recently_updated)
  923. # comment change order
  924. firstly_created_but_recently_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is randomized label content', '', True) # nopep8
  925. secondly_created_but_not_commented = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'this is another randomized label content', '', True) # nopep8
  926. comments = api.create_comment(workspace, firstly_created_but_recently_commented, 'juste a super comment', True) # nopep8
  927. content_workspace_2 = api.create(CONTENT_TYPES.Page.slug, workspace2,main_folder_workspace2, 'content_workspace_2', '',True) # nopep8
  928. dbsession.flush()
  929. transaction.commit()
  930. self.testapp.authorization = (
  931. 'Basic',
  932. (
  933. 'test@test.test',
  934. 'pass'
  935. )
  936. )
  937. selected_contents_id = [
  938. firstly_created_but_recently_commented.content_id,
  939. firstly_created_but_recently_updated.content_id,
  940. firstly_created.content_id,
  941. main_folder.content_id,
  942. ]
  943. url = '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status?contents_ids={cid1}&contents_ids={cid2}&contents_ids={cid3}&contents_ids={cid4}'.format( # nopep8
  944. workspace_id=workspace.workspace_id,
  945. cid1=selected_contents_id[0],
  946. cid2=selected_contents_id[1],
  947. cid3=selected_contents_id[2],
  948. cid4=selected_contents_id[3],
  949. user_id=admin.user_id,
  950. )
  951. res = self.testapp.get(
  952. url=url,
  953. status=403,
  954. )
  955. class TestUserSetContentAsRead(FunctionalTest):
  956. """
  957. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read # nopep8
  958. """
  959. def test_api_set_content_as_read__ok__200__admin(self):
  960. # init DB
  961. dbsession = get_tm_session(self.session_factory, transaction.manager)
  962. admin = dbsession.query(models.User) \
  963. .filter(models.User.email == 'admin@admin.admin') \
  964. .one()
  965. workspace_api = WorkspaceApi(
  966. current_user=admin,
  967. session=dbsession,
  968. config=self.app_config
  969. )
  970. workspace = WorkspaceApi(
  971. current_user=admin,
  972. session=dbsession,
  973. config=self.app_config,
  974. ).create_workspace(
  975. 'test workspace',
  976. save_now=True
  977. )
  978. uapi = UserApi(
  979. current_user=admin,
  980. session=dbsession,
  981. config=self.app_config,
  982. )
  983. gapi = GroupApi(
  984. current_user=admin,
  985. session=dbsession,
  986. config=self.app_config,
  987. )
  988. groups = [gapi.get_one_with_name('users')]
  989. test_user = uapi.create_user(
  990. email='test@test.test',
  991. password='pass',
  992. name='bob',
  993. groups=groups,
  994. timezone='Europe/Paris',
  995. do_save=True,
  996. do_notify=False,
  997. )
  998. rapi = RoleApi(
  999. current_user=admin,
  1000. session=dbsession,
  1001. config=self.app_config,
  1002. )
  1003. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1004. api = ContentApi(
  1005. current_user=admin,
  1006. session=dbsession,
  1007. config=self.app_config,
  1008. )
  1009. api2 = ContentApi(
  1010. current_user=test_user,
  1011. session=dbsession,
  1012. config=self.app_config,
  1013. )
  1014. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1015. # creation order test
  1016. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1017. api.mark_unread(firstly_created)
  1018. api2.mark_unread(firstly_created)
  1019. dbsession.flush()
  1020. transaction.commit()
  1021. self.testapp.authorization = (
  1022. 'Basic',
  1023. (
  1024. 'admin@admin.admin',
  1025. 'admin@admin.admin'
  1026. )
  1027. )
  1028. # before
  1029. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1030. user_id=test_user.user_id,
  1031. workspace_id=workspace.workspace_id
  1032. ), status=200)
  1033. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1034. assert res.json_body[0]['read_by_user'] is False
  1035. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1036. user_id=admin.user_id,
  1037. workspace_id=workspace.workspace_id
  1038. ), status=200)
  1039. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1040. assert res.json_body[0]['read_by_user'] is False
  1041. # read
  1042. self.testapp.put(
  1043. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  1044. workspace_id=workspace.workspace_id,
  1045. content_id=firstly_created.content_id,
  1046. user_id=test_user.user_id,
  1047. )
  1048. )
  1049. # after
  1050. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1051. user_id=test_user.user_id,
  1052. workspace_id=workspace.workspace_id
  1053. ), status=200) # nopep8
  1054. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1055. assert res.json_body[0]['read_by_user'] is True
  1056. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1057. user_id=admin.user_id,
  1058. workspace_id=workspace.workspace_id
  1059. ), status=200) # nopep8
  1060. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1061. assert res.json_body[0]['read_by_user'] is False
  1062. def test_api_set_content_as_read__ok__200__admin_workspace_do_not_exist(self):
  1063. # init DB
  1064. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1065. admin = dbsession.query(models.User) \
  1066. .filter(models.User.email == 'admin@admin.admin') \
  1067. .one()
  1068. workspace_api = WorkspaceApi(
  1069. current_user=admin,
  1070. session=dbsession,
  1071. config=self.app_config
  1072. )
  1073. workspace = WorkspaceApi(
  1074. current_user=admin,
  1075. session=dbsession,
  1076. config=self.app_config,
  1077. ).create_workspace(
  1078. 'test workspace',
  1079. save_now=True
  1080. )
  1081. uapi = UserApi(
  1082. current_user=admin,
  1083. session=dbsession,
  1084. config=self.app_config,
  1085. )
  1086. gapi = GroupApi(
  1087. current_user=admin,
  1088. session=dbsession,
  1089. config=self.app_config,
  1090. )
  1091. groups = [gapi.get_one_with_name('users')]
  1092. test_user = uapi.create_user(
  1093. email='test@test.test',
  1094. password='pass',
  1095. name='bob',
  1096. groups=groups,
  1097. timezone='Europe/Paris',
  1098. do_save=True,
  1099. do_notify=False,
  1100. )
  1101. rapi = RoleApi(
  1102. current_user=admin,
  1103. session=dbsession,
  1104. config=self.app_config,
  1105. )
  1106. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1107. api = ContentApi(
  1108. current_user=admin,
  1109. session=dbsession,
  1110. config=self.app_config,
  1111. )
  1112. api2 = ContentApi(
  1113. current_user=test_user,
  1114. session=dbsession,
  1115. config=self.app_config,
  1116. )
  1117. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1118. # creation order test
  1119. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1120. api.mark_unread(firstly_created)
  1121. api2.mark_unread(firstly_created)
  1122. dbsession.flush()
  1123. transaction.commit()
  1124. self.testapp.authorization = (
  1125. 'Basic',
  1126. (
  1127. 'admin@admin.admin',
  1128. 'admin@admin.admin'
  1129. )
  1130. )
  1131. # read
  1132. self.testapp.put(
  1133. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  1134. workspace_id=4000,
  1135. content_id=firstly_created.content_id,
  1136. user_id=test_user.user_id,
  1137. ),
  1138. status=400,
  1139. )
  1140. def test_api_set_content_as_read__ok__200__admin_content_do_not_exist(self):
  1141. # init DB
  1142. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1143. admin = dbsession.query(models.User) \
  1144. .filter(models.User.email == 'admin@admin.admin') \
  1145. .one()
  1146. workspace_api = WorkspaceApi(
  1147. current_user=admin,
  1148. session=dbsession,
  1149. config=self.app_config
  1150. )
  1151. workspace = WorkspaceApi(
  1152. current_user=admin,
  1153. session=dbsession,
  1154. config=self.app_config,
  1155. ).create_workspace(
  1156. 'test workspace',
  1157. save_now=True
  1158. )
  1159. uapi = UserApi(
  1160. current_user=admin,
  1161. session=dbsession,
  1162. config=self.app_config,
  1163. )
  1164. gapi = GroupApi(
  1165. current_user=admin,
  1166. session=dbsession,
  1167. config=self.app_config,
  1168. )
  1169. groups = [gapi.get_one_with_name('users')]
  1170. test_user = uapi.create_user(
  1171. email='test@test.test',
  1172. password='pass',
  1173. name='bob',
  1174. groups=groups,
  1175. timezone='Europe/Paris',
  1176. do_save=True,
  1177. do_notify=False,
  1178. )
  1179. rapi = RoleApi(
  1180. current_user=admin,
  1181. session=dbsession,
  1182. config=self.app_config,
  1183. )
  1184. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1185. api = ContentApi(
  1186. current_user=admin,
  1187. session=dbsession,
  1188. config=self.app_config,
  1189. )
  1190. api2 = ContentApi(
  1191. current_user=test_user,
  1192. session=dbsession,
  1193. config=self.app_config,
  1194. )
  1195. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1196. # creation order test
  1197. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1198. api.mark_unread(firstly_created)
  1199. api2.mark_unread(firstly_created)
  1200. dbsession.flush()
  1201. transaction.commit()
  1202. self.testapp.authorization = (
  1203. 'Basic',
  1204. (
  1205. 'admin@admin.admin',
  1206. 'admin@admin.admin'
  1207. )
  1208. )
  1209. # read
  1210. self.testapp.put(
  1211. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  1212. workspace_id=workspace.workspace_id,
  1213. content_id=4000,
  1214. user_id=test_user.user_id,
  1215. ),
  1216. status=400,
  1217. )
  1218. def test_api_set_content_as_read__ok__200__user_itself(self):
  1219. # init DB
  1220. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1221. admin = dbsession.query(models.User) \
  1222. .filter(models.User.email == 'admin@admin.admin') \
  1223. .one()
  1224. workspace_api = WorkspaceApi(
  1225. current_user=admin,
  1226. session=dbsession,
  1227. config=self.app_config
  1228. )
  1229. workspace = WorkspaceApi(
  1230. current_user=admin,
  1231. session=dbsession,
  1232. config=self.app_config,
  1233. ).create_workspace(
  1234. 'test workspace',
  1235. save_now=True
  1236. )
  1237. uapi = UserApi(
  1238. current_user=admin,
  1239. session=dbsession,
  1240. config=self.app_config,
  1241. )
  1242. gapi = GroupApi(
  1243. current_user=admin,
  1244. session=dbsession,
  1245. config=self.app_config,
  1246. )
  1247. groups = [gapi.get_one_with_name('users')]
  1248. test_user = uapi.create_user(
  1249. email='test@test.test',
  1250. password='pass',
  1251. name='bob',
  1252. groups=groups,
  1253. timezone='Europe/Paris',
  1254. do_save=True,
  1255. do_notify=False,
  1256. )
  1257. rapi = RoleApi(
  1258. current_user=admin,
  1259. session=dbsession,
  1260. config=self.app_config,
  1261. )
  1262. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1263. api = ContentApi(
  1264. current_user=admin,
  1265. session=dbsession,
  1266. config=self.app_config,
  1267. )
  1268. api2 = ContentApi(
  1269. current_user=test_user,
  1270. session=dbsession,
  1271. config=self.app_config,
  1272. )
  1273. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1274. # creation order test
  1275. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1276. api.mark_unread(firstly_created)
  1277. api2.mark_unread(firstly_created)
  1278. dbsession.flush()
  1279. transaction.commit()
  1280. self.testapp.authorization = (
  1281. 'Basic',
  1282. (
  1283. 'test@test.test',
  1284. 'pass'
  1285. )
  1286. )
  1287. # before
  1288. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1289. user_id=test_user.user_id,
  1290. workspace_id=workspace.workspace_id
  1291. ),
  1292. status=200
  1293. )
  1294. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1295. assert res.json_body[0]['read_by_user'] is False
  1296. # read
  1297. self.testapp.put(
  1298. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  1299. workspace_id=workspace.workspace_id,
  1300. content_id=firstly_created.content_id,
  1301. user_id=test_user.user_id,
  1302. )
  1303. )
  1304. # after
  1305. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1306. user_id=test_user.user_id,
  1307. workspace_id=workspace.workspace_id
  1308. ),
  1309. status=200
  1310. )
  1311. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1312. assert res.json_body[0]['read_by_user'] is True
  1313. def test_api_set_content_as_read__ok__403__other_user(self):
  1314. # init DB
  1315. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1316. admin = dbsession.query(models.User) \
  1317. .filter(models.User.email == 'admin@admin.admin') \
  1318. .one()
  1319. workspace_api = WorkspaceApi(
  1320. current_user=admin,
  1321. session=dbsession,
  1322. config=self.app_config
  1323. )
  1324. workspace = WorkspaceApi(
  1325. current_user=admin,
  1326. session=dbsession,
  1327. config=self.app_config,
  1328. ).create_workspace(
  1329. 'test workspace',
  1330. save_now=True
  1331. )
  1332. uapi = UserApi(
  1333. current_user=admin,
  1334. session=dbsession,
  1335. config=self.app_config,
  1336. )
  1337. gapi = GroupApi(
  1338. current_user=admin,
  1339. session=dbsession,
  1340. config=self.app_config,
  1341. )
  1342. groups = [gapi.get_one_with_name('users')]
  1343. test_user = uapi.create_user(
  1344. email='test@test.test',
  1345. password='pass',
  1346. name='bob',
  1347. groups=groups,
  1348. timezone='Europe/Paris',
  1349. do_save=True,
  1350. do_notify=False,
  1351. )
  1352. rapi = RoleApi(
  1353. current_user=admin,
  1354. session=dbsession,
  1355. config=self.app_config,
  1356. )
  1357. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1358. api = ContentApi(
  1359. current_user=admin,
  1360. session=dbsession,
  1361. config=self.app_config,
  1362. )
  1363. api2 = ContentApi(
  1364. current_user=test_user,
  1365. session=dbsession,
  1366. config=self.app_config,
  1367. )
  1368. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1369. # creation order test
  1370. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1371. api.mark_unread(firstly_created)
  1372. api2.mark_unread(firstly_created)
  1373. dbsession.flush()
  1374. transaction.commit()
  1375. self.testapp.authorization = (
  1376. 'Basic',
  1377. (
  1378. 'test@test.test',
  1379. 'pass'
  1380. )
  1381. )
  1382. # read
  1383. self.testapp.put(
  1384. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  1385. workspace_id=workspace.workspace_id,
  1386. content_id=firstly_created.content_id,
  1387. user_id=admin.user_id,
  1388. ),
  1389. status=403,
  1390. )
  1391. def test_api_set_content_as_read__ok__200__admin_with_comments(self):
  1392. # init DB
  1393. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1394. admin = dbsession.query(models.User) \
  1395. .filter(models.User.email == 'admin@admin.admin') \
  1396. .one()
  1397. workspace_api = WorkspaceApi(
  1398. current_user=admin,
  1399. session=dbsession,
  1400. config=self.app_config
  1401. )
  1402. workspace = WorkspaceApi(
  1403. current_user=admin,
  1404. session=dbsession,
  1405. config=self.app_config,
  1406. ).create_workspace(
  1407. 'test workspace',
  1408. save_now=True
  1409. )
  1410. uapi = UserApi(
  1411. current_user=admin,
  1412. session=dbsession,
  1413. config=self.app_config,
  1414. )
  1415. gapi = GroupApi(
  1416. current_user=admin,
  1417. session=dbsession,
  1418. config=self.app_config,
  1419. )
  1420. groups = [gapi.get_one_with_name('users')]
  1421. test_user = uapi.create_user(
  1422. email='test@test.test',
  1423. password='pass',
  1424. name='bob',
  1425. groups=groups,
  1426. timezone='Europe/Paris',
  1427. do_save=True,
  1428. do_notify=False,
  1429. )
  1430. rapi = RoleApi(
  1431. current_user=admin,
  1432. session=dbsession,
  1433. config=self.app_config,
  1434. )
  1435. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1436. api = ContentApi(
  1437. current_user=admin,
  1438. session=dbsession,
  1439. config=self.app_config,
  1440. )
  1441. api2 = ContentApi(
  1442. current_user=test_user,
  1443. session=dbsession,
  1444. config=self.app_config,
  1445. )
  1446. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1447. # creation order test
  1448. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1449. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  1450. api.mark_unread(firstly_created)
  1451. api.mark_unread(comments)
  1452. dbsession.flush()
  1453. transaction.commit()
  1454. self.testapp.authorization = (
  1455. 'Basic',
  1456. (
  1457. 'admin@admin.admin',
  1458. 'admin@admin.admin'
  1459. )
  1460. )
  1461. # before
  1462. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1463. user_id=test_user.user_id,
  1464. workspace_id=workspace.workspace_id
  1465. ), status=200) # nopep8
  1466. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1467. assert res.json_body[0]['read_by_user'] is False
  1468. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1469. user_id=admin.user_id,
  1470. workspace_id=workspace.workspace_id
  1471. ), status=200) # nopep8
  1472. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1473. assert res.json_body[0]['read_by_user'] is False
  1474. self.testapp.put(
  1475. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/read'.format( # nopep8
  1476. workspace_id=workspace.workspace_id,
  1477. content_id=firstly_created.content_id,
  1478. user_id=test_user.user_id,
  1479. )
  1480. )
  1481. # after
  1482. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1483. user_id=test_user.user_id,
  1484. workspace_id=workspace.workspace_id
  1485. ), status=200) # nopep8
  1486. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1487. assert res.json_body[0]['read_by_user'] is True
  1488. # comment is also set as read
  1489. assert comments.has_new_information_for(test_user) is False
  1490. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1491. user_id=admin.user_id,
  1492. workspace_id=workspace.workspace_id
  1493. ), status=200) # nopep8
  1494. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1495. assert res.json_body[0]['read_by_user'] is False
  1496. # comment is also set as read
  1497. assert comments.has_new_information_for(admin) is True
  1498. class TestUserSetContentAsUnread(FunctionalTest):
  1499. """
  1500. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread # nopep8
  1501. """
  1502. def test_api_set_content_as_unread__ok__200__admin(self):
  1503. # init DB
  1504. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1505. admin = dbsession.query(models.User) \
  1506. .filter(models.User.email == 'admin@admin.admin') \
  1507. .one()
  1508. workspace_api = WorkspaceApi(
  1509. current_user=admin,
  1510. session=dbsession,
  1511. config=self.app_config
  1512. )
  1513. workspace = WorkspaceApi(
  1514. current_user=admin,
  1515. session=dbsession,
  1516. config=self.app_config,
  1517. ).create_workspace(
  1518. 'test workspace',
  1519. save_now=True
  1520. )
  1521. uapi = UserApi(
  1522. current_user=admin,
  1523. session=dbsession,
  1524. config=self.app_config,
  1525. )
  1526. gapi = GroupApi(
  1527. current_user=admin,
  1528. session=dbsession,
  1529. config=self.app_config,
  1530. )
  1531. groups = [gapi.get_one_with_name('users')]
  1532. test_user = uapi.create_user(
  1533. email='test@test.test',
  1534. password='pass',
  1535. name='bob',
  1536. groups=groups,
  1537. timezone='Europe/Paris',
  1538. do_save=True,
  1539. do_notify=False,
  1540. )
  1541. rapi = RoleApi(
  1542. current_user=admin,
  1543. session=dbsession,
  1544. config=self.app_config,
  1545. )
  1546. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1547. api = ContentApi(
  1548. current_user=admin,
  1549. session=dbsession,
  1550. config=self.app_config,
  1551. )
  1552. api2 = ContentApi(
  1553. current_user=test_user,
  1554. session=dbsession,
  1555. config=self.app_config,
  1556. )
  1557. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1558. # creation order test
  1559. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1560. api.mark_read(firstly_created)
  1561. api2.mark_read(firstly_created)
  1562. dbsession.flush()
  1563. transaction.commit()
  1564. self.testapp.authorization = (
  1565. 'Basic',
  1566. (
  1567. 'admin@admin.admin',
  1568. 'admin@admin.admin'
  1569. )
  1570. )
  1571. # before
  1572. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1573. user_id=test_user.user_id,
  1574. workspace_id=workspace.workspace_id
  1575. ), status=200)
  1576. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1577. assert res.json_body[0]['read_by_user'] is True
  1578. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1579. user_id=admin.user_id,
  1580. workspace_id=workspace.workspace_id
  1581. ), status=200)
  1582. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1583. assert res.json_body[0]['read_by_user'] is True
  1584. # unread
  1585. self.testapp.put(
  1586. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  1587. workspace_id=workspace.workspace_id,
  1588. content_id=firstly_created.content_id,
  1589. user_id=test_user.user_id,
  1590. )
  1591. )
  1592. # after
  1593. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1594. user_id=test_user.user_id,
  1595. workspace_id=workspace.workspace_id
  1596. ), status=200)
  1597. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1598. assert res.json_body[0]['read_by_user'] is False
  1599. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1600. user_id=admin.user_id,
  1601. workspace_id=workspace.workspace_id
  1602. ), status=200)
  1603. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1604. assert res.json_body[0]['read_by_user'] is True
  1605. def test_api_set_content_as_unread__err__400__admin_workspace_do_not_exist(self):
  1606. # init DB
  1607. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1608. admin = dbsession.query(models.User) \
  1609. .filter(models.User.email == 'admin@admin.admin') \
  1610. .one()
  1611. workspace_api = WorkspaceApi(
  1612. current_user=admin,
  1613. session=dbsession,
  1614. config=self.app_config
  1615. )
  1616. workspace = WorkspaceApi(
  1617. current_user=admin,
  1618. session=dbsession,
  1619. config=self.app_config,
  1620. ).create_workspace(
  1621. 'test workspace',
  1622. save_now=True
  1623. )
  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. rapi = RoleApi(
  1645. current_user=admin,
  1646. session=dbsession,
  1647. config=self.app_config,
  1648. )
  1649. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1650. api = ContentApi(
  1651. current_user=admin,
  1652. session=dbsession,
  1653. config=self.app_config,
  1654. )
  1655. api2 = ContentApi(
  1656. current_user=test_user,
  1657. session=dbsession,
  1658. config=self.app_config,
  1659. )
  1660. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1661. # creation order test
  1662. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1663. api.mark_read(firstly_created)
  1664. api2.mark_read(firstly_created)
  1665. dbsession.flush()
  1666. transaction.commit()
  1667. self.testapp.authorization = (
  1668. 'Basic',
  1669. (
  1670. 'admin@admin.admin',
  1671. 'admin@admin.admin'
  1672. )
  1673. )
  1674. # unread
  1675. self.testapp.put(
  1676. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  1677. workspace_id=4000,
  1678. content_id=firstly_created.content_id,
  1679. user_id=test_user.user_id,
  1680. ),
  1681. status=400,
  1682. )
  1683. def test_api_set_content_as_unread__err__400__admin_content_do_not_exist(self):
  1684. # init DB
  1685. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1686. admin = dbsession.query(models.User) \
  1687. .filter(models.User.email == 'admin@admin.admin') \
  1688. .one()
  1689. workspace_api = WorkspaceApi(
  1690. current_user=admin,
  1691. session=dbsession,
  1692. config=self.app_config
  1693. )
  1694. workspace = WorkspaceApi(
  1695. current_user=admin,
  1696. session=dbsession,
  1697. config=self.app_config,
  1698. ).create_workspace(
  1699. 'test workspace',
  1700. save_now=True
  1701. )
  1702. uapi = UserApi(
  1703. current_user=admin,
  1704. session=dbsession,
  1705. config=self.app_config,
  1706. )
  1707. gapi = GroupApi(
  1708. current_user=admin,
  1709. session=dbsession,
  1710. config=self.app_config,
  1711. )
  1712. groups = [gapi.get_one_with_name('users')]
  1713. test_user = uapi.create_user(
  1714. email='test@test.test',
  1715. password='pass',
  1716. name='bob',
  1717. groups=groups,
  1718. timezone='Europe/Paris',
  1719. do_save=True,
  1720. do_notify=False,
  1721. )
  1722. rapi = RoleApi(
  1723. current_user=admin,
  1724. session=dbsession,
  1725. config=self.app_config,
  1726. )
  1727. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1728. api = ContentApi(
  1729. current_user=admin,
  1730. session=dbsession,
  1731. config=self.app_config,
  1732. )
  1733. api2 = ContentApi(
  1734. current_user=test_user,
  1735. session=dbsession,
  1736. config=self.app_config,
  1737. )
  1738. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1739. # creation order test
  1740. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1741. api.mark_read(firstly_created)
  1742. api2.mark_read(firstly_created)
  1743. dbsession.flush()
  1744. transaction.commit()
  1745. self.testapp.authorization = (
  1746. 'Basic',
  1747. (
  1748. 'admin@admin.admin',
  1749. 'admin@admin.admin'
  1750. )
  1751. )
  1752. # unread
  1753. self.testapp.put(
  1754. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  1755. workspace_id=workspace.workspace_id,
  1756. content_id=4000,
  1757. user_id=test_user.user_id,
  1758. ),
  1759. status=400,
  1760. )
  1761. def test_api_set_content_as_unread__ok__200__user_itself(self):
  1762. # init DB
  1763. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1764. admin = dbsession.query(models.User) \
  1765. .filter(models.User.email == 'admin@admin.admin') \
  1766. .one()
  1767. workspace_api = WorkspaceApi(
  1768. current_user=admin,
  1769. session=dbsession,
  1770. config=self.app_config
  1771. )
  1772. workspace = WorkspaceApi(
  1773. current_user=admin,
  1774. session=dbsession,
  1775. config=self.app_config,
  1776. ).create_workspace(
  1777. 'test workspace',
  1778. save_now=True
  1779. )
  1780. uapi = UserApi(
  1781. current_user=admin,
  1782. session=dbsession,
  1783. config=self.app_config,
  1784. )
  1785. gapi = GroupApi(
  1786. current_user=admin,
  1787. session=dbsession,
  1788. config=self.app_config,
  1789. )
  1790. groups = [gapi.get_one_with_name('users')]
  1791. test_user = uapi.create_user(
  1792. email='test@test.test',
  1793. password='pass',
  1794. name='bob',
  1795. groups=groups,
  1796. timezone='Europe/Paris',
  1797. do_save=True,
  1798. do_notify=False,
  1799. )
  1800. rapi = RoleApi(
  1801. current_user=admin,
  1802. session=dbsession,
  1803. config=self.app_config,
  1804. )
  1805. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1806. api = ContentApi(
  1807. current_user=admin,
  1808. session=dbsession,
  1809. config=self.app_config,
  1810. )
  1811. api2 = ContentApi(
  1812. current_user=test_user,
  1813. session=dbsession,
  1814. config=self.app_config,
  1815. )
  1816. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1817. # creation order test
  1818. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1819. api.mark_read(firstly_created)
  1820. api2.mark_read(firstly_created)
  1821. dbsession.flush()
  1822. transaction.commit()
  1823. self.testapp.authorization = (
  1824. 'Basic',
  1825. (
  1826. 'test@test.test',
  1827. 'pass'
  1828. )
  1829. )
  1830. # before
  1831. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1832. user_id=test_user.user_id,
  1833. workspace_id=workspace.workspace_id
  1834. ), status=200)
  1835. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1836. assert res.json_body[0]['read_by_user'] is True
  1837. # unread
  1838. self.testapp.put(
  1839. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  1840. workspace_id=workspace.workspace_id,
  1841. content_id=firstly_created.content_id,
  1842. user_id=test_user.user_id,
  1843. )
  1844. )
  1845. # after
  1846. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  1847. user_id=test_user.user_id,
  1848. workspace_id=workspace.workspace_id
  1849. ), status=200)
  1850. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1851. assert res.json_body[0]['read_by_user'] is False
  1852. def test_api_set_content_as_unread__err__403__other_user(self):
  1853. # init DB
  1854. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1855. admin = dbsession.query(models.User) \
  1856. .filter(models.User.email == 'admin@admin.admin') \
  1857. .one()
  1858. workspace_api = WorkspaceApi(
  1859. current_user=admin,
  1860. session=dbsession,
  1861. config=self.app_config
  1862. )
  1863. workspace = WorkspaceApi(
  1864. current_user=admin,
  1865. session=dbsession,
  1866. config=self.app_config,
  1867. ).create_workspace(
  1868. 'test workspace',
  1869. save_now=True
  1870. )
  1871. uapi = UserApi(
  1872. current_user=admin,
  1873. session=dbsession,
  1874. config=self.app_config,
  1875. )
  1876. gapi = GroupApi(
  1877. current_user=admin,
  1878. session=dbsession,
  1879. config=self.app_config,
  1880. )
  1881. groups = [gapi.get_one_with_name('users')]
  1882. test_user = uapi.create_user(
  1883. email='test@test.test',
  1884. password='pass',
  1885. name='bob',
  1886. groups=groups,
  1887. timezone='Europe/Paris',
  1888. do_save=True,
  1889. do_notify=False,
  1890. )
  1891. rapi = RoleApi(
  1892. current_user=admin,
  1893. session=dbsession,
  1894. config=self.app_config,
  1895. )
  1896. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  1897. api = ContentApi(
  1898. current_user=admin,
  1899. session=dbsession,
  1900. config=self.app_config,
  1901. )
  1902. api2 = ContentApi(
  1903. current_user=test_user,
  1904. session=dbsession,
  1905. config=self.app_config,
  1906. )
  1907. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1908. # creation order test
  1909. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1910. api.mark_read(firstly_created)
  1911. api2.mark_read(firstly_created)
  1912. dbsession.flush()
  1913. transaction.commit()
  1914. self.testapp.authorization = (
  1915. 'Basic',
  1916. (
  1917. 'test@test.test',
  1918. 'pass'
  1919. )
  1920. )
  1921. # unread
  1922. self.testapp.put(
  1923. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  1924. workspace_id=workspace.workspace_id,
  1925. content_id=firstly_created.content_id,
  1926. user_id=admin.user_id,
  1927. ),
  1928. status=403,
  1929. )
  1930. def test_api_set_content_as_unread__ok__200__with_comments(self):
  1931. # init DB
  1932. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1933. admin = dbsession.query(models.User) \
  1934. .filter(models.User.email == 'admin@admin.admin') \
  1935. .one()
  1936. workspace_api = WorkspaceApi(
  1937. current_user=admin,
  1938. session=dbsession,
  1939. config=self.app_config
  1940. )
  1941. workspace = WorkspaceApi(
  1942. current_user=admin,
  1943. session=dbsession,
  1944. config=self.app_config,
  1945. ).create_workspace(
  1946. 'test workspace',
  1947. save_now=True
  1948. )
  1949. api = ContentApi(
  1950. current_user=admin,
  1951. session=dbsession,
  1952. config=self.app_config,
  1953. )
  1954. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  1955. # creation order test
  1956. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  1957. comments = api.create_comment(workspace, firstly_created, 'juste a super comment', True) # nopep8
  1958. api.mark_read(firstly_created)
  1959. api.mark_read(comments)
  1960. dbsession.flush()
  1961. transaction.commit()
  1962. self.testapp.authorization = (
  1963. 'Basic',
  1964. (
  1965. 'admin@admin.admin',
  1966. 'admin@admin.admin'
  1967. )
  1968. )
  1969. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  1970. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1971. assert res.json_body[0]['read_by_user'] is True
  1972. self.testapp.put(
  1973. '/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/{content_id}/unread'.format( # nopep8
  1974. workspace_id=workspace.workspace_id,
  1975. content_id=firstly_created.content_id,
  1976. user_id=admin.user_id,
  1977. )
  1978. )
  1979. res = self.testapp.get('/api/v2/users/1/workspaces/{}/contents/read_status'.format(workspace.workspace_id), status=200) # nopep8
  1980. assert res.json_body[0]['content_id'] == firstly_created.content_id
  1981. assert res.json_body[0]['read_by_user'] is False
  1982. assert comments.has_new_information_for(admin) is True
  1983. class TestUserSetWorkspaceAsRead(FunctionalTest):
  1984. """
  1985. Tests for /api/v2/users/{user_id}/workspaces/{workspace_id}/read
  1986. """
  1987. def test_api_set_content_as_read__ok__200__admin(self):
  1988. # init DB
  1989. dbsession = get_tm_session(self.session_factory, transaction.manager)
  1990. admin = dbsession.query(models.User) \
  1991. .filter(models.User.email == 'admin@admin.admin') \
  1992. .one()
  1993. workspace_api = WorkspaceApi(
  1994. current_user=admin,
  1995. session=dbsession,
  1996. config=self.app_config
  1997. )
  1998. workspace = WorkspaceApi(
  1999. current_user=admin,
  2000. session=dbsession,
  2001. config=self.app_config,
  2002. ).create_workspace(
  2003. 'test workspace',
  2004. save_now=True
  2005. )
  2006. uapi = UserApi(
  2007. current_user=admin,
  2008. session=dbsession,
  2009. config=self.app_config,
  2010. )
  2011. gapi = GroupApi(
  2012. current_user=admin,
  2013. session=dbsession,
  2014. config=self.app_config,
  2015. )
  2016. groups = [gapi.get_one_with_name('users')]
  2017. test_user = uapi.create_user(
  2018. email='test@test.test',
  2019. password='pass',
  2020. name='bob',
  2021. groups=groups,
  2022. timezone='Europe/Paris',
  2023. do_save=True,
  2024. do_notify=False,
  2025. )
  2026. rapi = RoleApi(
  2027. current_user=admin,
  2028. session=dbsession,
  2029. config=self.app_config,
  2030. )
  2031. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  2032. api = ContentApi(
  2033. current_user=admin,
  2034. session=dbsession,
  2035. config=self.app_config,
  2036. )
  2037. api2 = ContentApi(
  2038. current_user=test_user,
  2039. session=dbsession,
  2040. config=self.app_config,
  2041. )
  2042. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  2043. # creation order test
  2044. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  2045. api.mark_unread(main_folder)
  2046. api.mark_unread(firstly_created)
  2047. api2.mark_unread(main_folder)
  2048. api2.mark_unread(firstly_created)
  2049. dbsession.flush()
  2050. transaction.commit()
  2051. self.testapp.authorization = (
  2052. 'Basic',
  2053. (
  2054. 'admin@admin.admin',
  2055. 'admin@admin.admin'
  2056. )
  2057. )
  2058. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  2059. user_id=test_user.user_id,
  2060. workspace_id=workspace.workspace_id
  2061. ), status=200)
  2062. assert res.json_body[0]['content_id'] == firstly_created.content_id
  2063. assert res.json_body[0]['read_by_user'] is False
  2064. assert res.json_body[1]['content_id'] == main_folder.content_id
  2065. assert res.json_body[1]['read_by_user'] is False
  2066. self.testapp.put(
  2067. '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format( # nopep8
  2068. workspace_id=workspace.workspace_id,
  2069. content_id=firstly_created.content_id,
  2070. user_id=test_user.user_id,
  2071. )
  2072. )
  2073. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  2074. user_id=test_user.user_id,
  2075. workspace_id=workspace.workspace_id
  2076. ), status=200)
  2077. assert res.json_body[0]['content_id'] == firstly_created.content_id
  2078. assert res.json_body[0]['read_by_user'] is True
  2079. assert res.json_body[1]['content_id'] == main_folder.content_id
  2080. assert res.json_body[1]['read_by_user'] is True
  2081. def test_api_set_content_as_read__ok__200__user_itself(self):
  2082. # init DB
  2083. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2084. admin = dbsession.query(models.User) \
  2085. .filter(models.User.email == 'admin@admin.admin') \
  2086. .one()
  2087. workspace_api = WorkspaceApi(
  2088. current_user=admin,
  2089. session=dbsession,
  2090. config=self.app_config
  2091. )
  2092. workspace = WorkspaceApi(
  2093. current_user=admin,
  2094. session=dbsession,
  2095. config=self.app_config,
  2096. ).create_workspace(
  2097. 'test workspace',
  2098. save_now=True
  2099. )
  2100. uapi = UserApi(
  2101. current_user=admin,
  2102. session=dbsession,
  2103. config=self.app_config,
  2104. )
  2105. gapi = GroupApi(
  2106. current_user=admin,
  2107. session=dbsession,
  2108. config=self.app_config,
  2109. )
  2110. groups = [gapi.get_one_with_name('users')]
  2111. test_user = uapi.create_user(
  2112. email='test@test.test',
  2113. password='pass',
  2114. name='bob',
  2115. groups=groups,
  2116. timezone='Europe/Paris',
  2117. do_save=True,
  2118. do_notify=False,
  2119. )
  2120. rapi = RoleApi(
  2121. current_user=admin,
  2122. session=dbsession,
  2123. config=self.app_config,
  2124. )
  2125. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  2126. api = ContentApi(
  2127. current_user=admin,
  2128. session=dbsession,
  2129. config=self.app_config,
  2130. )
  2131. api2 = ContentApi(
  2132. current_user=test_user,
  2133. session=dbsession,
  2134. config=self.app_config,
  2135. )
  2136. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  2137. # creation order test
  2138. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  2139. api.mark_unread(main_folder)
  2140. api.mark_unread(firstly_created)
  2141. api2.mark_unread(main_folder)
  2142. api2.mark_unread(firstly_created)
  2143. dbsession.flush()
  2144. transaction.commit()
  2145. self.testapp.authorization = (
  2146. 'Basic',
  2147. (
  2148. 'test@test.test',
  2149. 'pass'
  2150. )
  2151. )
  2152. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  2153. user_id=test_user.user_id,
  2154. workspace_id=workspace.workspace_id
  2155. ), status=200)
  2156. assert res.json_body[0]['content_id'] == firstly_created.content_id
  2157. assert res.json_body[0]['read_by_user'] is False
  2158. assert res.json_body[1]['content_id'] == main_folder.content_id
  2159. assert res.json_body[1]['read_by_user'] is False
  2160. self.testapp.put(
  2161. '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format( # nopep8
  2162. workspace_id=workspace.workspace_id,
  2163. content_id=firstly_created.content_id,
  2164. user_id=test_user.user_id,
  2165. )
  2166. )
  2167. res = self.testapp.get('/api/v2/users/{user_id}/workspaces/{workspace_id}/contents/read_status'.format( # nopep8
  2168. user_id=test_user.user_id,
  2169. workspace_id=workspace.workspace_id
  2170. ), status=200)
  2171. assert res.json_body[0]['content_id'] == firstly_created.content_id
  2172. assert res.json_body[0]['read_by_user'] is True
  2173. assert res.json_body[1]['content_id'] == main_folder.content_id
  2174. assert res.json_body[1]['read_by_user'] is True
  2175. def test_api_set_content_as_read__err__403__other_user(self):
  2176. # init DB
  2177. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2178. admin = dbsession.query(models.User) \
  2179. .filter(models.User.email == 'admin@admin.admin') \
  2180. .one()
  2181. workspace_api = WorkspaceApi(
  2182. current_user=admin,
  2183. session=dbsession,
  2184. config=self.app_config
  2185. )
  2186. workspace = WorkspaceApi(
  2187. current_user=admin,
  2188. session=dbsession,
  2189. config=self.app_config,
  2190. ).create_workspace(
  2191. 'test workspace',
  2192. save_now=True
  2193. )
  2194. uapi = UserApi(
  2195. current_user=admin,
  2196. session=dbsession,
  2197. config=self.app_config,
  2198. )
  2199. gapi = GroupApi(
  2200. current_user=admin,
  2201. session=dbsession,
  2202. config=self.app_config,
  2203. )
  2204. groups = [gapi.get_one_with_name('users')]
  2205. test_user = uapi.create_user(
  2206. email='test@test.test',
  2207. password='pass',
  2208. name='bob',
  2209. groups=groups,
  2210. timezone='Europe/Paris',
  2211. do_save=True,
  2212. do_notify=False,
  2213. )
  2214. rapi = RoleApi(
  2215. current_user=admin,
  2216. session=dbsession,
  2217. config=self.app_config,
  2218. )
  2219. rapi.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  2220. api = ContentApi(
  2221. current_user=admin,
  2222. session=dbsession,
  2223. config=self.app_config,
  2224. )
  2225. api2 = ContentApi(
  2226. current_user=test_user,
  2227. session=dbsession,
  2228. config=self.app_config,
  2229. )
  2230. main_folder = api.create(CONTENT_TYPES.Folder.slug, workspace, None, 'this is randomized folder', '', True) # nopep8
  2231. # creation order test
  2232. firstly_created = api.create(CONTENT_TYPES.Page.slug, workspace, main_folder, 'creation_order_test', '', True) # nopep8
  2233. api.mark_unread(main_folder)
  2234. api.mark_unread(firstly_created)
  2235. api2.mark_unread(main_folder)
  2236. api2.mark_unread(firstly_created)
  2237. dbsession.flush()
  2238. transaction.commit()
  2239. self.testapp.authorization = (
  2240. 'Basic',
  2241. (
  2242. 'test@test.test',
  2243. 'pass'
  2244. )
  2245. )
  2246. self.testapp.put(
  2247. '/api/v2/users/{user_id}/workspaces/{workspace_id}/read'.format( # nopep8
  2248. workspace_id=workspace.workspace_id,
  2249. content_id=firstly_created.content_id,
  2250. user_id=admin.user_id,
  2251. ),
  2252. status=403,
  2253. )
  2254. class TestUserWorkspaceEndpoint(FunctionalTest):
  2255. """
  2256. Tests for /api/v2/users/{user_id}/workspaces
  2257. """
  2258. fixtures = [BaseFixture, ContentFixtures]
  2259. def test_api__get_user_workspaces__ok_200__nominal_case(self):
  2260. """
  2261. Check obtain all workspaces reachables for user with user auth.
  2262. """
  2263. self.testapp.authorization = (
  2264. 'Basic',
  2265. (
  2266. 'admin@admin.admin',
  2267. 'admin@admin.admin'
  2268. )
  2269. )
  2270. res = self.testapp.get('/api/v2/users/1/workspaces', status=200)
  2271. res = res.json_body
  2272. workspace = res[0]
  2273. assert workspace['workspace_id'] == 1
  2274. assert workspace['label'] == 'Business'
  2275. assert workspace['slug'] == 'business'
  2276. assert len(workspace['sidebar_entries']) == 5
  2277. # TODO - G.M - 2018-08-02 - Better test for sidebar entry, make it
  2278. # not fixed on active application/content-file
  2279. sidebar_entry = workspace['sidebar_entries'][0]
  2280. assert sidebar_entry['slug'] == 'dashboard'
  2281. assert sidebar_entry['label'] == 'Dashboard'
  2282. assert sidebar_entry['route'] == '/#/workspaces/1/dashboard' # nopep8
  2283. assert sidebar_entry['hexcolor'] == "#252525"
  2284. assert sidebar_entry['fa_icon'] == "signal"
  2285. sidebar_entry = workspace['sidebar_entries'][1]
  2286. assert sidebar_entry['slug'] == 'contents/all'
  2287. assert sidebar_entry['label'] == 'All Contents'
  2288. assert sidebar_entry['route'] == "/#/workspaces/1/contents" # nopep8
  2289. assert sidebar_entry['hexcolor'] == "#fdfdfd"
  2290. assert sidebar_entry['fa_icon'] == "th"
  2291. sidebar_entry = workspace['sidebar_entries'][2]
  2292. assert sidebar_entry['slug'] == 'contents/html-document'
  2293. assert sidebar_entry['label'] == 'Text Documents'
  2294. assert sidebar_entry['route'] == '/#/workspaces/1/contents?type=html-document' # nopep8
  2295. assert sidebar_entry['hexcolor'] == "#3f52e3"
  2296. assert sidebar_entry['fa_icon'] == "file-text-o"
  2297. sidebar_entry = workspace['sidebar_entries'][3]
  2298. assert sidebar_entry['slug'] == 'contents/file'
  2299. assert sidebar_entry['label'] == 'Files'
  2300. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=file" # nopep8
  2301. assert sidebar_entry['hexcolor'] == "#FF9900"
  2302. assert sidebar_entry['fa_icon'] == "paperclip"
  2303. sidebar_entry = workspace['sidebar_entries'][4]
  2304. assert sidebar_entry['slug'] == 'contents/thread'
  2305. assert sidebar_entry['label'] == 'Threads'
  2306. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=thread" # nopep8
  2307. assert sidebar_entry['hexcolor'] == "#ad4cf9"
  2308. assert sidebar_entry['fa_icon'] == "comments-o"
  2309. def test_api__get_user_workspaces__err_403__unallowed_user(self):
  2310. """
  2311. Check obtain all workspaces reachables for one user
  2312. with another non-admin user auth.
  2313. """
  2314. self.testapp.authorization = (
  2315. 'Basic',
  2316. (
  2317. 'lawrence-not-real-email@fsf.local',
  2318. 'foobarbaz'
  2319. )
  2320. )
  2321. res = self.testapp.get('/api/v2/users/1/workspaces', status=403)
  2322. assert isinstance(res.json, dict)
  2323. assert 'code' in res.json.keys()
  2324. assert 'message' in res.json.keys()
  2325. assert 'details' in res.json.keys()
  2326. def test_api__get_user_workspaces__err_401__unregistered_user(self):
  2327. """
  2328. Check obtain all workspaces reachables for one user
  2329. without correct user auth (user unregistered).
  2330. """
  2331. self.testapp.authorization = (
  2332. 'Basic',
  2333. (
  2334. 'john@doe.doe',
  2335. 'lapin'
  2336. )
  2337. )
  2338. res = self.testapp.get('/api/v2/users/1/workspaces', status=401)
  2339. assert isinstance(res.json, dict)
  2340. assert 'code' in res.json.keys()
  2341. assert 'message' in res.json.keys()
  2342. assert 'details' in res.json.keys()
  2343. def test_api__get_user_workspaces__err_400__user_does_not_exist(self):
  2344. """
  2345. Check obtain all workspaces reachables for one user who does
  2346. not exist
  2347. with a correct user auth.
  2348. """
  2349. self.testapp.authorization = (
  2350. 'Basic',
  2351. (
  2352. 'admin@admin.admin',
  2353. 'admin@admin.admin'
  2354. )
  2355. )
  2356. res = self.testapp.get('/api/v2/users/5/workspaces', status=400)
  2357. assert isinstance(res.json, dict)
  2358. assert 'code' in res.json.keys()
  2359. assert 'message' in res.json.keys()
  2360. assert 'details' in res.json.keys()
  2361. class TestUserEndpoint(FunctionalTest):
  2362. # -*- coding: utf-8 -*-
  2363. """
  2364. Tests for GET /api/v2/users/{user_id}
  2365. """
  2366. fixtures = [BaseFixture]
  2367. def test_api__get_user__ok_200__admin(self):
  2368. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2369. admin = dbsession.query(models.User) \
  2370. .filter(models.User.email == 'admin@admin.admin') \
  2371. .one()
  2372. uapi = UserApi(
  2373. current_user=admin,
  2374. session=dbsession,
  2375. config=self.app_config,
  2376. )
  2377. gapi = GroupApi(
  2378. current_user=admin,
  2379. session=dbsession,
  2380. config=self.app_config,
  2381. )
  2382. groups = [gapi.get_one_with_name('users')]
  2383. test_user = uapi.create_user(
  2384. email='test@test.test',
  2385. password='pass',
  2386. name='bob',
  2387. groups=groups,
  2388. timezone='Europe/Paris',
  2389. do_save=True,
  2390. do_notify=False,
  2391. )
  2392. uapi.save(test_user)
  2393. transaction.commit()
  2394. user_id = int(test_user.user_id)
  2395. self.testapp.authorization = (
  2396. 'Basic',
  2397. (
  2398. 'admin@admin.admin',
  2399. 'admin@admin.admin'
  2400. )
  2401. )
  2402. res = self.testapp.get(
  2403. '/api/v2/users/{}'.format(user_id),
  2404. status=200
  2405. )
  2406. res = res.json_body
  2407. assert res['user_id'] == user_id
  2408. assert res['created']
  2409. assert res['is_active'] is True
  2410. assert res['profile'] == 'users'
  2411. assert res['email'] == 'test@test.test'
  2412. assert res['public_name'] == 'bob'
  2413. assert res['timezone'] == 'Europe/Paris'
  2414. assert res['is_deleted'] is False
  2415. def test_api__get_user__ok_200__user_itself(self):
  2416. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2417. admin = dbsession.query(models.User) \
  2418. .filter(models.User.email == 'admin@admin.admin') \
  2419. .one()
  2420. uapi = UserApi(
  2421. current_user=admin,
  2422. session=dbsession,
  2423. config=self.app_config,
  2424. )
  2425. gapi = GroupApi(
  2426. current_user=admin,
  2427. session=dbsession,
  2428. config=self.app_config,
  2429. )
  2430. groups = [gapi.get_one_with_name('users')]
  2431. test_user = uapi.create_user(
  2432. email='test@test.test',
  2433. password='pass',
  2434. name='bob',
  2435. groups=groups,
  2436. timezone='Europe/Paris',
  2437. do_save=True,
  2438. do_notify=False,
  2439. )
  2440. uapi.save(test_user)
  2441. transaction.commit()
  2442. user_id = int(test_user.user_id)
  2443. self.testapp.authorization = (
  2444. 'Basic',
  2445. (
  2446. 'test@test.test',
  2447. 'pass'
  2448. )
  2449. )
  2450. res = self.testapp.get(
  2451. '/api/v2/users/{}'.format(user_id),
  2452. status=200
  2453. )
  2454. res = res.json_body
  2455. assert res['user_id'] == user_id
  2456. assert res['created']
  2457. assert res['is_active'] is True
  2458. assert res['profile'] == 'users'
  2459. assert res['email'] == 'test@test.test'
  2460. assert res['public_name'] == 'bob'
  2461. assert res['timezone'] == 'Europe/Paris'
  2462. assert res['is_deleted'] is False
  2463. def test_api__get_user__err_403__other_normal_user(self):
  2464. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2465. admin = dbsession.query(models.User) \
  2466. .filter(models.User.email == 'admin@admin.admin') \
  2467. .one()
  2468. uapi = UserApi(
  2469. current_user=admin,
  2470. session=dbsession,
  2471. config=self.app_config,
  2472. )
  2473. gapi = GroupApi(
  2474. current_user=admin,
  2475. session=dbsession,
  2476. config=self.app_config,
  2477. )
  2478. groups = [gapi.get_one_with_name('users')]
  2479. test_user = uapi.create_user(
  2480. email='test@test.test',
  2481. password='pass',
  2482. name='bob',
  2483. groups=groups,
  2484. timezone='Europe/Paris',
  2485. do_save=True,
  2486. do_notify=False,
  2487. )
  2488. test_user2 = uapi.create_user(
  2489. email='test2@test2.test2',
  2490. password='pass',
  2491. name='bob2',
  2492. groups=groups,
  2493. timezone='Europe/Paris',
  2494. do_save=True,
  2495. do_notify=False,
  2496. )
  2497. uapi.save(test_user2)
  2498. uapi.save(test_user)
  2499. transaction.commit()
  2500. user_id = int(test_user.user_id)
  2501. self.testapp.authorization = (
  2502. 'Basic',
  2503. (
  2504. 'test2@test2.test2',
  2505. 'pass'
  2506. )
  2507. )
  2508. self.testapp.get(
  2509. '/api/v2/users/{}'.format(user_id),
  2510. status=403
  2511. )
  2512. class TestUsersEndpoint(FunctionalTest):
  2513. # -*- coding: utf-8 -*-
  2514. """
  2515. Tests for GET /api/v2/users/{user_id}
  2516. """
  2517. fixtures = [BaseFixture]
  2518. def test_api__get_user__ok_200__admin(self):
  2519. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2520. admin = dbsession.query(models.User) \
  2521. .filter(models.User.email == 'admin@admin.admin') \
  2522. .one()
  2523. uapi = UserApi(
  2524. current_user=admin,
  2525. session=dbsession,
  2526. config=self.app_config,
  2527. )
  2528. gapi = GroupApi(
  2529. current_user=admin,
  2530. session=dbsession,
  2531. config=self.app_config,
  2532. )
  2533. groups = [gapi.get_one_with_name('users')]
  2534. test_user = uapi.create_user(
  2535. email='test@test.test',
  2536. password='pass',
  2537. name='bob',
  2538. groups=groups,
  2539. timezone='Europe/Paris',
  2540. do_save=True,
  2541. do_notify=False,
  2542. )
  2543. uapi.save(test_user)
  2544. transaction.commit()
  2545. user_id = int(test_user.user_id)
  2546. self.testapp.authorization = (
  2547. 'Basic',
  2548. (
  2549. 'admin@admin.admin',
  2550. 'admin@admin.admin'
  2551. )
  2552. )
  2553. res = self.testapp.get(
  2554. '/api/v2/users',
  2555. status=200
  2556. )
  2557. res = res.json_body
  2558. assert len(res) == 2
  2559. assert res[0]['user_id'] == admin.user_id
  2560. assert res[0]['public_name'] == admin.display_name
  2561. assert res[0]['avatar_url'] is None
  2562. assert res[1]['user_id'] == test_user.user_id
  2563. assert res[1]['public_name'] == test_user.display_name
  2564. assert res[1]['avatar_url'] is None
  2565. def test_api__get_user__err_403__normal_user(self):
  2566. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2567. admin = dbsession.query(models.User) \
  2568. .filter(models.User.email == 'admin@admin.admin') \
  2569. .one()
  2570. uapi = UserApi(
  2571. current_user=admin,
  2572. session=dbsession,
  2573. config=self.app_config,
  2574. )
  2575. gapi = GroupApi(
  2576. current_user=admin,
  2577. session=dbsession,
  2578. config=self.app_config,
  2579. )
  2580. groups = [gapi.get_one_with_name('users')]
  2581. test_user = uapi.create_user(
  2582. email='test@test.test',
  2583. password='pass',
  2584. name='bob',
  2585. groups=groups,
  2586. timezone='Europe/Paris',
  2587. do_save=True,
  2588. do_notify=False,
  2589. )
  2590. uapi.save(test_user)
  2591. transaction.commit()
  2592. user_id = int(test_user.user_id)
  2593. self.testapp.authorization = (
  2594. 'Basic',
  2595. (
  2596. 'test@test.test',
  2597. 'pass'
  2598. )
  2599. )
  2600. self.testapp.get(
  2601. '/api/v2/users',
  2602. status=403
  2603. )
  2604. class TestKnownMembersEndpoint(FunctionalTest):
  2605. # -*- coding: utf-8 -*-
  2606. """
  2607. Tests for GET /api/v2/users/{user_id}
  2608. """
  2609. fixtures = [BaseFixture]
  2610. def test_api__get_user__ok_200__admin__by_name(self):
  2611. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2612. admin = dbsession.query(models.User) \
  2613. .filter(models.User.email == 'admin@admin.admin') \
  2614. .one()
  2615. uapi = UserApi(
  2616. current_user=admin,
  2617. session=dbsession,
  2618. config=self.app_config,
  2619. )
  2620. gapi = GroupApi(
  2621. current_user=admin,
  2622. session=dbsession,
  2623. config=self.app_config,
  2624. )
  2625. groups = [gapi.get_one_with_name('users')]
  2626. test_user = uapi.create_user(
  2627. email='test@test.test',
  2628. password='pass',
  2629. name='bob',
  2630. groups=groups,
  2631. timezone='Europe/Paris',
  2632. do_save=True,
  2633. do_notify=False,
  2634. )
  2635. test_user2 = uapi.create_user(
  2636. email='test2@test2.test2',
  2637. password='pass',
  2638. name='bob2',
  2639. groups=groups,
  2640. timezone='Europe/Paris',
  2641. do_save=True,
  2642. do_notify=False,
  2643. )
  2644. uapi.save(test_user)
  2645. uapi.save(test_user2)
  2646. transaction.commit()
  2647. user_id = int(admin.user_id)
  2648. self.testapp.authorization = (
  2649. 'Basic',
  2650. (
  2651. 'admin@admin.admin',
  2652. 'admin@admin.admin'
  2653. )
  2654. )
  2655. params = {
  2656. 'acp': 'bob',
  2657. }
  2658. res = self.testapp.get(
  2659. '/api/v2/users/{user_id}/known_members'.format(user_id=user_id),
  2660. status=200,
  2661. params=params,
  2662. )
  2663. res = res.json_body
  2664. assert len(res) == 2
  2665. assert res[0]['user_id'] == test_user.user_id
  2666. assert res[0]['public_name'] == test_user.display_name
  2667. assert res[0]['avatar_url'] is None
  2668. assert res[1]['user_id'] == test_user2.user_id
  2669. assert res[1]['public_name'] == test_user2.display_name
  2670. assert res[1]['avatar_url'] is None
  2671. def test_api__get_user__ok_200__admin__by_email(self):
  2672. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2673. admin = dbsession.query(models.User) \
  2674. .filter(models.User.email == 'admin@admin.admin') \
  2675. .one()
  2676. uapi = UserApi(
  2677. current_user=admin,
  2678. session=dbsession,
  2679. config=self.app_config,
  2680. )
  2681. gapi = GroupApi(
  2682. current_user=admin,
  2683. session=dbsession,
  2684. config=self.app_config,
  2685. )
  2686. groups = [gapi.get_one_with_name('users')]
  2687. test_user = uapi.create_user(
  2688. email='test@test.test',
  2689. password='pass',
  2690. name='bob',
  2691. groups=groups,
  2692. timezone='Europe/Paris',
  2693. do_save=True,
  2694. do_notify=False,
  2695. )
  2696. test_user2 = uapi.create_user(
  2697. email='test2@test2.test2',
  2698. password='pass',
  2699. name='bob2',
  2700. groups=groups,
  2701. timezone='Europe/Paris',
  2702. do_save=True,
  2703. do_notify=False,
  2704. )
  2705. uapi.save(test_user)
  2706. uapi.save(test_user2)
  2707. transaction.commit()
  2708. user_id = int(admin.user_id)
  2709. self.testapp.authorization = (
  2710. 'Basic',
  2711. (
  2712. 'admin@admin.admin',
  2713. 'admin@admin.admin'
  2714. )
  2715. )
  2716. params = {
  2717. 'acp': 'test',
  2718. }
  2719. res = self.testapp.get(
  2720. '/api/v2/users/{user_id}/known_members'.format(user_id=user_id),
  2721. status=200,
  2722. params=params,
  2723. )
  2724. res = res.json_body
  2725. assert len(res) == 2
  2726. assert res[0]['user_id'] == test_user.user_id
  2727. assert res[0]['public_name'] == test_user.display_name
  2728. assert res[0]['avatar_url'] is None
  2729. assert res[1]['user_id'] == test_user2.user_id
  2730. assert res[1]['public_name'] == test_user2.display_name
  2731. assert res[1]['avatar_url'] is None
  2732. def test_api__get_user__err_403__admin__too_small_acp(self):
  2733. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2734. admin = dbsession.query(models.User) \
  2735. .filter(models.User.email == 'admin@admin.admin') \
  2736. .one()
  2737. uapi = UserApi(
  2738. current_user=admin,
  2739. session=dbsession,
  2740. config=self.app_config,
  2741. )
  2742. gapi = GroupApi(
  2743. current_user=admin,
  2744. session=dbsession,
  2745. config=self.app_config,
  2746. )
  2747. groups = [gapi.get_one_with_name('users')]
  2748. test_user = uapi.create_user(
  2749. email='test@test.test',
  2750. password='pass',
  2751. name='bob',
  2752. groups=groups,
  2753. timezone='Europe/Paris',
  2754. do_save=True,
  2755. do_notify=False,
  2756. )
  2757. test_user2 = uapi.create_user(
  2758. email='test2@test2.test2',
  2759. password='pass',
  2760. name='bob2',
  2761. groups=groups,
  2762. timezone='Europe/Paris',
  2763. do_save=True,
  2764. do_notify=False,
  2765. )
  2766. uapi.save(test_user)
  2767. transaction.commit()
  2768. user_id = int(admin.user_id)
  2769. self.testapp.authorization = (
  2770. 'Basic',
  2771. (
  2772. 'admin@admin.admin',
  2773. 'admin@admin.admin'
  2774. )
  2775. )
  2776. params = {
  2777. 'acp': 't',
  2778. }
  2779. res = self.testapp.get(
  2780. '/api/v2/users/{user_id}/known_members'.format(user_id=user_id),
  2781. status=400,
  2782. params=params
  2783. )
  2784. def test_api__get_user__ok_200__normal_user_by_email(self):
  2785. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2786. admin = dbsession.query(models.User) \
  2787. .filter(models.User.email == 'admin@admin.admin') \
  2788. .one()
  2789. uapi = UserApi(
  2790. current_user=admin,
  2791. session=dbsession,
  2792. config=self.app_config,
  2793. )
  2794. gapi = GroupApi(
  2795. current_user=admin,
  2796. session=dbsession,
  2797. config=self.app_config,
  2798. )
  2799. groups = [gapi.get_one_with_name('users')]
  2800. test_user = uapi.create_user(
  2801. email='test@test.test',
  2802. password='pass',
  2803. name='bob',
  2804. groups=groups,
  2805. timezone='Europe/Paris',
  2806. do_save=True,
  2807. do_notify=False,
  2808. )
  2809. test_user2 = uapi.create_user(
  2810. email='test2@test2.test2',
  2811. password='pass',
  2812. name='bob2',
  2813. groups=groups,
  2814. timezone='Europe/Paris',
  2815. do_save=True,
  2816. do_notify=False,
  2817. )
  2818. test_user3 = uapi.create_user(
  2819. email='test3@test3.test3',
  2820. password='pass',
  2821. name='bob3',
  2822. groups=groups,
  2823. timezone='Europe/Paris',
  2824. do_save=True,
  2825. do_notify=False,
  2826. )
  2827. uapi.save(test_user)
  2828. uapi.save(test_user2)
  2829. uapi.save(test_user3)
  2830. workspace_api = WorkspaceApi(
  2831. current_user=admin,
  2832. session=dbsession,
  2833. config=self.app_config
  2834. )
  2835. workspace = WorkspaceApi(
  2836. current_user=admin,
  2837. session=dbsession,
  2838. config=self.app_config,
  2839. ).create_workspace(
  2840. 'test workspace',
  2841. save_now=True
  2842. )
  2843. role_api = RoleApi(
  2844. current_user=admin,
  2845. session=dbsession,
  2846. config=self.app_config,
  2847. )
  2848. role_api.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  2849. role_api.create_one(test_user2, workspace, UserRoleInWorkspace.READER, False)
  2850. transaction.commit()
  2851. user_id = int(test_user.user_id)
  2852. self.testapp.authorization = (
  2853. 'Basic',
  2854. (
  2855. 'test@test.test',
  2856. 'pass'
  2857. )
  2858. )
  2859. params = {
  2860. 'acp': 'test',
  2861. }
  2862. res = self.testapp.get(
  2863. '/api/v2/users/{user_id}/known_members'.format(user_id=user_id),
  2864. status=200,
  2865. params=params
  2866. )
  2867. res = res.json_body
  2868. assert len(res) == 2
  2869. assert res[0]['user_id'] == test_user.user_id
  2870. assert res[0]['public_name'] == test_user.display_name
  2871. assert res[0]['avatar_url'] is None
  2872. assert res[1]['user_id'] == test_user2.user_id
  2873. assert res[1]['public_name'] == test_user2.display_name
  2874. assert res[1]['avatar_url'] is None
  2875. class TestSetEmailEndpoint(FunctionalTest):
  2876. # -*- coding: utf-8 -*-
  2877. """
  2878. Tests for PUT /api/v2/users/{user_id}/email
  2879. """
  2880. fixtures = [BaseFixture]
  2881. def test_api__set_user_email__ok_200__admin(self):
  2882. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2883. admin = dbsession.query(models.User) \
  2884. .filter(models.User.email == 'admin@admin.admin') \
  2885. .one()
  2886. uapi = UserApi(
  2887. current_user=admin,
  2888. session=dbsession,
  2889. config=self.app_config,
  2890. )
  2891. gapi = GroupApi(
  2892. current_user=admin,
  2893. session=dbsession,
  2894. config=self.app_config,
  2895. )
  2896. groups = [gapi.get_one_with_name('users')]
  2897. test_user = uapi.create_user(
  2898. email='test@test.test',
  2899. password='pass',
  2900. name='bob',
  2901. groups=groups,
  2902. timezone='Europe/Paris',
  2903. do_save=True,
  2904. do_notify=False,
  2905. )
  2906. uapi.save(test_user)
  2907. transaction.commit()
  2908. user_id = int(test_user.user_id)
  2909. self.testapp.authorization = (
  2910. 'Basic',
  2911. (
  2912. 'admin@admin.admin',
  2913. 'admin@admin.admin'
  2914. )
  2915. )
  2916. # check before
  2917. res = self.testapp.get(
  2918. '/api/v2/users/{}'.format(user_id),
  2919. status=200
  2920. )
  2921. res = res.json_body
  2922. assert res['email'] == 'test@test.test'
  2923. # Set password
  2924. params = {
  2925. 'email': 'mysuperemail@email.fr',
  2926. 'loggedin_user_password': 'admin@admin.admin',
  2927. }
  2928. self.testapp.put_json(
  2929. '/api/v2/users/{}/email'.format(user_id),
  2930. params=params,
  2931. status=200,
  2932. )
  2933. # Check After
  2934. res = self.testapp.get(
  2935. '/api/v2/users/{}'.format(user_id),
  2936. status=200
  2937. )
  2938. res = res.json_body
  2939. assert res['email'] == 'mysuperemail@email.fr'
  2940. def test_api__set_user_email__err_403__admin_wrong_password(self):
  2941. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2942. admin = dbsession.query(models.User) \
  2943. .filter(models.User.email == 'admin@admin.admin') \
  2944. .one()
  2945. uapi = UserApi(
  2946. current_user=admin,
  2947. session=dbsession,
  2948. config=self.app_config,
  2949. )
  2950. gapi = GroupApi(
  2951. current_user=admin,
  2952. session=dbsession,
  2953. config=self.app_config,
  2954. )
  2955. groups = [gapi.get_one_with_name('users')]
  2956. test_user = uapi.create_user(
  2957. email='test@test.test',
  2958. password='pass',
  2959. name='bob',
  2960. groups=groups,
  2961. timezone='Europe/Paris',
  2962. do_save=True,
  2963. do_notify=False,
  2964. )
  2965. uapi.save(test_user)
  2966. transaction.commit()
  2967. user_id = int(test_user.user_id)
  2968. self.testapp.authorization = (
  2969. 'Basic',
  2970. (
  2971. 'admin@admin.admin',
  2972. 'admin@admin.admin'
  2973. )
  2974. )
  2975. # check before
  2976. res = self.testapp.get(
  2977. '/api/v2/users/{}'.format(user_id),
  2978. status=200
  2979. )
  2980. res = res.json_body
  2981. assert res['email'] == 'test@test.test'
  2982. # Set password
  2983. params = {
  2984. 'email': 'mysuperemail@email.fr',
  2985. 'loggedin_user_password': 'badpassword',
  2986. }
  2987. self.testapp.put_json(
  2988. '/api/v2/users/{}/email'.format(user_id),
  2989. params=params,
  2990. status=403,
  2991. )
  2992. # Check After
  2993. res = self.testapp.get(
  2994. '/api/v2/users/{}'.format(user_id),
  2995. status=200
  2996. )
  2997. res = res.json_body
  2998. assert res['email'] == 'test@test.test'
  2999. def test_api__set_user_email__err_400__admin_string_is_not_email(self):
  3000. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3001. admin = dbsession.query(models.User) \
  3002. .filter(models.User.email == 'admin@admin.admin') \
  3003. .one()
  3004. uapi = UserApi(
  3005. current_user=admin,
  3006. session=dbsession,
  3007. config=self.app_config,
  3008. )
  3009. gapi = GroupApi(
  3010. current_user=admin,
  3011. session=dbsession,
  3012. config=self.app_config,
  3013. )
  3014. groups = [gapi.get_one_with_name('users')]
  3015. test_user = uapi.create_user(
  3016. email='test@test.test',
  3017. password='pass',
  3018. name='bob',
  3019. groups=groups,
  3020. timezone='Europe/Paris',
  3021. do_save=True,
  3022. do_notify=False,
  3023. )
  3024. uapi.save(test_user)
  3025. transaction.commit()
  3026. user_id = int(test_user.user_id)
  3027. self.testapp.authorization = (
  3028. 'Basic',
  3029. (
  3030. 'admin@admin.admin',
  3031. 'admin@admin.admin'
  3032. )
  3033. )
  3034. # check before
  3035. res = self.testapp.get(
  3036. '/api/v2/users/{}'.format(user_id),
  3037. status=200
  3038. )
  3039. res = res.json_body
  3040. assert res['email'] == 'test@test.test'
  3041. # Set password
  3042. params = {
  3043. 'email': 'thatisnotandemail',
  3044. 'loggedin_user_password': 'admin@admin.admin',
  3045. }
  3046. self.testapp.put_json(
  3047. '/api/v2/users/{}/email'.format(user_id),
  3048. params=params,
  3049. status=400,
  3050. )
  3051. # Check After
  3052. res = self.testapp.get(
  3053. '/api/v2/users/{}'.format(user_id),
  3054. status=200
  3055. )
  3056. res = res.json_body
  3057. assert res['email'] == 'test@test.test'
  3058. def test_api__set_user_email__ok_200__user_itself(self):
  3059. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3060. admin = dbsession.query(models.User) \
  3061. .filter(models.User.email == 'admin@admin.admin') \
  3062. .one()
  3063. uapi = UserApi(
  3064. current_user=admin,
  3065. session=dbsession,
  3066. config=self.app_config,
  3067. )
  3068. gapi = GroupApi(
  3069. current_user=admin,
  3070. session=dbsession,
  3071. config=self.app_config,
  3072. )
  3073. groups = [gapi.get_one_with_name('users')]
  3074. test_user = uapi.create_user(
  3075. email='test@test.test',
  3076. password='pass',
  3077. name='bob',
  3078. groups=groups,
  3079. timezone='Europe/Paris',
  3080. do_save=True,
  3081. do_notify=False,
  3082. )
  3083. uapi.save(test_user)
  3084. transaction.commit()
  3085. user_id = int(test_user.user_id)
  3086. self.testapp.authorization = (
  3087. 'Basic',
  3088. (
  3089. 'test@test.test',
  3090. 'pass'
  3091. )
  3092. )
  3093. # check before
  3094. res = self.testapp.get(
  3095. '/api/v2/users/{}'.format(user_id),
  3096. status=200
  3097. )
  3098. res = res.json_body
  3099. assert res['email'] == 'test@test.test'
  3100. # Set password
  3101. params = {
  3102. 'email': 'mysuperemail@email.fr',
  3103. 'loggedin_user_password': 'pass',
  3104. }
  3105. self.testapp.put_json(
  3106. '/api/v2/users/{}/email'.format(user_id),
  3107. params=params,
  3108. status=200,
  3109. )
  3110. self.testapp.authorization = (
  3111. 'Basic',
  3112. (
  3113. 'mysuperemail@email.fr',
  3114. 'pass'
  3115. )
  3116. )
  3117. # Check After
  3118. res = self.testapp.get(
  3119. '/api/v2/users/{}'.format(user_id),
  3120. status=200
  3121. )
  3122. res = res.json_body
  3123. assert res['email'] == 'mysuperemail@email.fr'
  3124. def test_api__set_user_email__err_403__other_normal_user(self):
  3125. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3126. admin = dbsession.query(models.User) \
  3127. .filter(models.User.email == 'admin@admin.admin') \
  3128. .one()
  3129. uapi = UserApi(
  3130. current_user=admin,
  3131. session=dbsession,
  3132. config=self.app_config,
  3133. )
  3134. gapi = GroupApi(
  3135. current_user=admin,
  3136. session=dbsession,
  3137. config=self.app_config,
  3138. )
  3139. groups = [gapi.get_one_with_name('users')]
  3140. test_user = uapi.create_user(
  3141. email='test@test.test',
  3142. password='pass',
  3143. name='bob',
  3144. groups=groups,
  3145. timezone='Europe/Paris',
  3146. do_save=True,
  3147. do_notify=False,
  3148. )
  3149. test_user2 = uapi.create_user(
  3150. email='test2@test2.test2',
  3151. password='pass',
  3152. name='bob2',
  3153. groups=groups,
  3154. timezone='Europe/Paris',
  3155. do_save=True,
  3156. do_notify=False,
  3157. )
  3158. uapi.save(test_user2)
  3159. uapi.save(test_user)
  3160. transaction.commit()
  3161. user_id = int(test_user.user_id)
  3162. self.testapp.authorization = (
  3163. 'Basic',
  3164. (
  3165. 'test@test.test',
  3166. 'pass'
  3167. )
  3168. )
  3169. # Set password
  3170. params = {
  3171. 'email': 'mysuperemail@email.fr',
  3172. 'loggedin_user_password': 'test2@test2.test2',
  3173. }
  3174. self.testapp.put_json(
  3175. '/api/v2/users/{}/email'.format(user_id),
  3176. params=params,
  3177. status=403,
  3178. )
  3179. class TestSetPasswordEndpoint(FunctionalTest):
  3180. # -*- coding: utf-8 -*-
  3181. """
  3182. Tests for PUT /api/v2/users/{user_id}/password
  3183. """
  3184. fixtures = [BaseFixture]
  3185. def test_api__set_user_password__ok_200__admin(self):
  3186. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3187. admin = dbsession.query(models.User) \
  3188. .filter(models.User.email == 'admin@admin.admin') \
  3189. .one()
  3190. uapi = UserApi(
  3191. current_user=admin,
  3192. session=dbsession,
  3193. config=self.app_config,
  3194. )
  3195. gapi = GroupApi(
  3196. current_user=admin,
  3197. session=dbsession,
  3198. config=self.app_config,
  3199. )
  3200. groups = [gapi.get_one_with_name('users')]
  3201. test_user = uapi.create_user(
  3202. email='test@test.test',
  3203. password='pass',
  3204. name='bob',
  3205. groups=groups,
  3206. timezone='Europe/Paris',
  3207. do_save=True,
  3208. do_notify=False,
  3209. )
  3210. uapi.save(test_user)
  3211. transaction.commit()
  3212. user_id = int(test_user.user_id)
  3213. self.testapp.authorization = (
  3214. 'Basic',
  3215. (
  3216. 'admin@admin.admin',
  3217. 'admin@admin.admin'
  3218. )
  3219. )
  3220. # check before
  3221. user = uapi.get_one(user_id)
  3222. assert user.validate_password('pass')
  3223. assert not user.validate_password('mynewpassword')
  3224. # Set password
  3225. params = {
  3226. 'new_password': 'mynewpassword',
  3227. 'new_password2': 'mynewpassword',
  3228. 'loggedin_user_password': 'admin@admin.admin',
  3229. }
  3230. self.testapp.put_json(
  3231. '/api/v2/users/{}/password'.format(user_id),
  3232. params=params,
  3233. status=204,
  3234. )
  3235. # Check After
  3236. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3237. uapi = UserApi(
  3238. current_user=admin,
  3239. session=dbsession,
  3240. config=self.app_config,
  3241. )
  3242. user = uapi.get_one(user_id)
  3243. assert not user.validate_password('pass')
  3244. assert user.validate_password('mynewpassword')
  3245. def test_api__set_user_password__err_403__admin_wrong_password(self):
  3246. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3247. admin = dbsession.query(models.User) \
  3248. .filter(models.User.email == 'admin@admin.admin') \
  3249. .one()
  3250. uapi = UserApi(
  3251. current_user=admin,
  3252. session=dbsession,
  3253. config=self.app_config,
  3254. )
  3255. gapi = GroupApi(
  3256. current_user=admin,
  3257. session=dbsession,
  3258. config=self.app_config,
  3259. )
  3260. groups = [gapi.get_one_with_name('users')]
  3261. test_user = uapi.create_user(
  3262. email='test@test.test',
  3263. password='pass',
  3264. name='bob',
  3265. groups=groups,
  3266. timezone='Europe/Paris',
  3267. do_save=True,
  3268. do_notify=False,
  3269. )
  3270. uapi.save(test_user)
  3271. transaction.commit()
  3272. user_id = int(test_user.user_id)
  3273. self.testapp.authorization = (
  3274. 'Basic',
  3275. (
  3276. 'admin@admin.admin',
  3277. 'admin@admin.admin'
  3278. )
  3279. )
  3280. # check before
  3281. user = uapi.get_one(user_id)
  3282. assert user.validate_password('pass')
  3283. assert not user.validate_password('mynewpassword')
  3284. # Set password
  3285. params = {
  3286. 'new_password': 'mynewpassword',
  3287. 'new_password2': 'mynewpassword',
  3288. 'loggedin_user_password': 'wrongpassword',
  3289. }
  3290. self.testapp.put_json(
  3291. '/api/v2/users/{}/password'.format(user_id),
  3292. params=params,
  3293. status=403,
  3294. )
  3295. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3296. uapi = UserApi(
  3297. current_user=admin,
  3298. session=dbsession,
  3299. config=self.app_config,
  3300. )
  3301. # Check After
  3302. user = uapi.get_one(user_id)
  3303. assert user.validate_password('pass')
  3304. assert not user.validate_password('mynewpassword')
  3305. def test_api__set_user_password__err_400__admin_passwords_do_not_match(self): # nopep8
  3306. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3307. admin = dbsession.query(models.User) \
  3308. .filter(models.User.email == 'admin@admin.admin') \
  3309. .one()
  3310. uapi = UserApi(
  3311. current_user=admin,
  3312. session=dbsession,
  3313. config=self.app_config,
  3314. )
  3315. gapi = GroupApi(
  3316. current_user=admin,
  3317. session=dbsession,
  3318. config=self.app_config,
  3319. )
  3320. groups = [gapi.get_one_with_name('users')]
  3321. test_user = uapi.create_user(
  3322. email='test@test.test',
  3323. password='pass',
  3324. name='bob',
  3325. groups=groups,
  3326. timezone='Europe/Paris',
  3327. do_save=True,
  3328. do_notify=False,
  3329. )
  3330. uapi.save(test_user)
  3331. transaction.commit()
  3332. user_id = int(test_user.user_id)
  3333. self.testapp.authorization = (
  3334. 'Basic',
  3335. (
  3336. 'admin@admin.admin',
  3337. 'admin@admin.admin'
  3338. )
  3339. )
  3340. # check before
  3341. user = uapi.get_one(user_id)
  3342. assert user.validate_password('pass')
  3343. assert not user.validate_password('mynewpassword')
  3344. assert not user.validate_password('mynewpassword2')
  3345. # Set password
  3346. params = {
  3347. 'new_password': 'mynewpassword',
  3348. 'new_password2': 'mynewpassword2',
  3349. 'loggedin_user_password': 'admin@admin.admin',
  3350. }
  3351. self.testapp.put_json(
  3352. '/api/v2/users/{}/password'.format(user_id),
  3353. params=params,
  3354. status=400,
  3355. )
  3356. # Check After
  3357. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3358. uapi = UserApi(
  3359. current_user=admin,
  3360. session=dbsession,
  3361. config=self.app_config,
  3362. )
  3363. user = uapi.get_one(user_id)
  3364. assert user.validate_password('pass')
  3365. assert not user.validate_password('mynewpassword')
  3366. assert not user.validate_password('mynewpassword2')
  3367. def test_api__set_user_password__ok_200__user_itself(self):
  3368. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3369. admin = dbsession.query(models.User) \
  3370. .filter(models.User.email == 'admin@admin.admin') \
  3371. .one()
  3372. uapi = UserApi(
  3373. current_user=admin,
  3374. session=dbsession,
  3375. config=self.app_config,
  3376. )
  3377. gapi = GroupApi(
  3378. current_user=admin,
  3379. session=dbsession,
  3380. config=self.app_config,
  3381. )
  3382. groups = [gapi.get_one_with_name('users')]
  3383. test_user = uapi.create_user(
  3384. email='test@test.test',
  3385. password='pass',
  3386. name='bob',
  3387. groups=groups,
  3388. timezone='Europe/Paris',
  3389. do_save=True,
  3390. do_notify=False,
  3391. )
  3392. uapi.save(test_user)
  3393. transaction.commit()
  3394. user_id = int(test_user.user_id)
  3395. self.testapp.authorization = (
  3396. 'Basic',
  3397. (
  3398. 'test@test.test',
  3399. 'pass'
  3400. )
  3401. )
  3402. # check before
  3403. user = uapi.get_one(user_id)
  3404. assert user.validate_password('pass')
  3405. assert not user.validate_password('mynewpassword')
  3406. # Set password
  3407. params = {
  3408. 'new_password': 'mynewpassword',
  3409. 'new_password2': 'mynewpassword',
  3410. 'loggedin_user_password': 'pass',
  3411. }
  3412. self.testapp.put_json(
  3413. '/api/v2/users/{}/password'.format(user_id),
  3414. params=params,
  3415. status=204,
  3416. )
  3417. # Check After
  3418. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3419. uapi = UserApi(
  3420. current_user=admin,
  3421. session=dbsession,
  3422. config=self.app_config,
  3423. )
  3424. user = uapi.get_one(user_id)
  3425. assert not user.validate_password('pass')
  3426. assert user.validate_password('mynewpassword')
  3427. def test_api__set_user_email__err_403__other_normal_user(self):
  3428. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3429. admin = dbsession.query(models.User) \
  3430. .filter(models.User.email == 'admin@admin.admin') \
  3431. .one()
  3432. uapi = UserApi(
  3433. current_user=admin,
  3434. session=dbsession,
  3435. config=self.app_config,
  3436. )
  3437. gapi = GroupApi(
  3438. current_user=admin,
  3439. session=dbsession,
  3440. config=self.app_config,
  3441. )
  3442. groups = [gapi.get_one_with_name('users')]
  3443. test_user = uapi.create_user(
  3444. email='test@test.test',
  3445. password='pass',
  3446. name='bob',
  3447. groups=groups,
  3448. timezone='Europe/Paris',
  3449. do_save=True,
  3450. do_notify=False,
  3451. )
  3452. test_user2 = uapi.create_user(
  3453. email='test2@test2.test2',
  3454. password='pass',
  3455. name='bob2',
  3456. groups=groups,
  3457. timezone='Europe/Paris',
  3458. do_save=True,
  3459. do_notify=False,
  3460. )
  3461. uapi.save(test_user2)
  3462. uapi.save(test_user)
  3463. transaction.commit()
  3464. user_id = int(test_user.user_id)
  3465. self.testapp.authorization = (
  3466. 'Basic',
  3467. (
  3468. 'test@test.test',
  3469. 'pass'
  3470. )
  3471. )
  3472. # Set password
  3473. params = {
  3474. 'email': 'mysuperemail@email.fr',
  3475. 'loggedin_user_password': 'test2@test2.test2',
  3476. }
  3477. self.testapp.put_json(
  3478. '/api/v2/users/{}/email'.format(user_id),
  3479. params=params,
  3480. status=403,
  3481. )
  3482. class TestSetUserInfoEndpoint(FunctionalTest):
  3483. # -*- coding: utf-8 -*-
  3484. """
  3485. Tests for PUT /api/v2/users/{user_id}
  3486. """
  3487. fixtures = [BaseFixture]
  3488. def test_api__set_user_info__ok_200__admin(self):
  3489. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3490. admin = dbsession.query(models.User) \
  3491. .filter(models.User.email == 'admin@admin.admin') \
  3492. .one()
  3493. uapi = UserApi(
  3494. current_user=admin,
  3495. session=dbsession,
  3496. config=self.app_config,
  3497. )
  3498. gapi = GroupApi(
  3499. current_user=admin,
  3500. session=dbsession,
  3501. config=self.app_config,
  3502. )
  3503. groups = [gapi.get_one_with_name('users')]
  3504. test_user = uapi.create_user(
  3505. email='test@test.test',
  3506. password='pass',
  3507. name='bob',
  3508. groups=groups,
  3509. timezone='Europe/Paris',
  3510. do_save=True,
  3511. do_notify=False,
  3512. )
  3513. uapi.save(test_user)
  3514. transaction.commit()
  3515. user_id = int(test_user.user_id)
  3516. self.testapp.authorization = (
  3517. 'Basic',
  3518. (
  3519. 'admin@admin.admin',
  3520. 'admin@admin.admin'
  3521. )
  3522. )
  3523. # check before
  3524. res = self.testapp.get(
  3525. '/api/v2/users/{}'.format(user_id),
  3526. status=200
  3527. )
  3528. res = res.json_body
  3529. assert res['user_id'] == user_id
  3530. assert res['public_name'] == 'bob'
  3531. assert res['timezone'] == 'Europe/Paris'
  3532. # Set params
  3533. params = {
  3534. 'public_name': 'updated',
  3535. 'timezone': 'Europe/London',
  3536. }
  3537. self.testapp.put_json(
  3538. '/api/v2/users/{}'.format(user_id),
  3539. params=params,
  3540. status=200,
  3541. )
  3542. # Check After
  3543. res = self.testapp.get(
  3544. '/api/v2/users/{}'.format(user_id),
  3545. status=200
  3546. )
  3547. res = res.json_body
  3548. assert res['user_id'] == user_id
  3549. assert res['public_name'] == 'updated'
  3550. assert res['timezone'] == 'Europe/London'
  3551. def test_api__set_user_info__ok_200__user_itself(self):
  3552. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3553. admin = dbsession.query(models.User) \
  3554. .filter(models.User.email == 'admin@admin.admin') \
  3555. .one()
  3556. uapi = UserApi(
  3557. current_user=admin,
  3558. session=dbsession,
  3559. config=self.app_config,
  3560. )
  3561. gapi = GroupApi(
  3562. current_user=admin,
  3563. session=dbsession,
  3564. config=self.app_config,
  3565. )
  3566. groups = [gapi.get_one_with_name('users')]
  3567. test_user = uapi.create_user(
  3568. email='test@test.test',
  3569. password='pass',
  3570. name='bob',
  3571. groups=groups,
  3572. timezone='Europe/Paris',
  3573. do_save=True,
  3574. do_notify=False,
  3575. )
  3576. uapi.save(test_user)
  3577. transaction.commit()
  3578. user_id = int(test_user.user_id)
  3579. self.testapp.authorization = (
  3580. 'Basic',
  3581. (
  3582. 'test@test.test',
  3583. 'pass',
  3584. )
  3585. )
  3586. # check before
  3587. res = self.testapp.get(
  3588. '/api/v2/users/{}'.format(user_id),
  3589. status=200
  3590. )
  3591. res = res.json_body
  3592. assert res['user_id'] == user_id
  3593. assert res['public_name'] == 'bob'
  3594. assert res['timezone'] == 'Europe/Paris'
  3595. # Set params
  3596. params = {
  3597. 'public_name': 'updated',
  3598. 'timezone': 'Europe/London',
  3599. }
  3600. self.testapp.put_json(
  3601. '/api/v2/users/{}'.format(user_id),
  3602. params=params,
  3603. status=200,
  3604. )
  3605. # Check After
  3606. res = self.testapp.get(
  3607. '/api/v2/users/{}'.format(user_id),
  3608. status=200
  3609. )
  3610. res = res.json_body
  3611. assert res['user_id'] == user_id
  3612. assert res['public_name'] == 'updated'
  3613. assert res['timezone'] == 'Europe/London'
  3614. def test_api__set_user_email__err_403__other_normal_user(self):
  3615. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3616. admin = dbsession.query(models.User) \
  3617. .filter(models.User.email == 'admin@admin.admin') \
  3618. .one()
  3619. uapi = UserApi(
  3620. current_user=admin,
  3621. session=dbsession,
  3622. config=self.app_config,
  3623. )
  3624. gapi = GroupApi(
  3625. current_user=admin,
  3626. session=dbsession,
  3627. config=self.app_config,
  3628. )
  3629. groups = [gapi.get_one_with_name('users')]
  3630. test_user = uapi.create_user(
  3631. email='test@test.test',
  3632. password='pass',
  3633. name='bob',
  3634. groups=groups,
  3635. timezone='Europe/Paris',
  3636. do_save=True,
  3637. do_notify=False,
  3638. )
  3639. test_user2 = uapi.create_user(
  3640. email='test2@test2.test2',
  3641. password='pass',
  3642. name='test',
  3643. groups=groups,
  3644. timezone='Europe/Paris',
  3645. do_save=True,
  3646. do_notify=False,
  3647. )
  3648. uapi.save(test_user2)
  3649. uapi.save(test_user)
  3650. transaction.commit()
  3651. user_id = int(test_user.user_id)
  3652. self.testapp.authorization = (
  3653. 'Basic',
  3654. (
  3655. 'test2@test2.test2',
  3656. 'pass',
  3657. )
  3658. )
  3659. # Set params
  3660. params = {
  3661. 'public_name': 'updated',
  3662. 'timezone': 'Europe/London',
  3663. }
  3664. self.testapp.put_json(
  3665. '/api/v2/users/{}'.format(user_id),
  3666. params=params,
  3667. status=403,
  3668. )
  3669. class TestSetUserProfilEndpoint(FunctionalTest):
  3670. # -*- coding: utf-8 -*-
  3671. """
  3672. Tests for PUT /api/v2/users/{user_id}/profile
  3673. """
  3674. fixtures = [BaseFixture]
  3675. def test_api__set_user_info__ok_200__admin(self):
  3676. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3677. admin = dbsession.query(models.User) \
  3678. .filter(models.User.email == 'admin@admin.admin') \
  3679. .one()
  3680. uapi = UserApi(
  3681. current_user=admin,
  3682. session=dbsession,
  3683. config=self.app_config,
  3684. )
  3685. gapi = GroupApi(
  3686. current_user=admin,
  3687. session=dbsession,
  3688. config=self.app_config,
  3689. )
  3690. groups = [gapi.get_one_with_name('users')]
  3691. test_user = uapi.create_user(
  3692. email='test@test.test',
  3693. password='pass',
  3694. name='bob',
  3695. groups=groups,
  3696. timezone='Europe/Paris',
  3697. do_save=True,
  3698. do_notify=False,
  3699. )
  3700. uapi.save(test_user)
  3701. transaction.commit()
  3702. user_id = int(test_user.user_id)
  3703. self.testapp.authorization = (
  3704. 'Basic',
  3705. (
  3706. 'admin@admin.admin',
  3707. 'admin@admin.admin'
  3708. )
  3709. )
  3710. # check before
  3711. res = self.testapp.get(
  3712. '/api/v2/users/{}'.format(user_id),
  3713. status=200
  3714. )
  3715. res = res.json_body
  3716. assert res['user_id'] == user_id
  3717. assert res['profile'] == 'users'
  3718. # Set params
  3719. params = {
  3720. 'profile': 'administrators',
  3721. }
  3722. self.testapp.put_json(
  3723. '/api/v2/users/{}/profile'.format(user_id),
  3724. params=params,
  3725. status=204,
  3726. )
  3727. # Check After
  3728. res = self.testapp.get(
  3729. '/api/v2/users/{}'.format(user_id),
  3730. status=200
  3731. )
  3732. res = res.json_body
  3733. assert res['user_id'] == user_id
  3734. assert res['profile'] == 'administrators'
  3735. def test_api__set_user_info__err_403__user_itself(self):
  3736. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3737. admin = dbsession.query(models.User) \
  3738. .filter(models.User.email == 'admin@admin.admin') \
  3739. .one()
  3740. uapi = UserApi(
  3741. current_user=admin,
  3742. session=dbsession,
  3743. config=self.app_config,
  3744. )
  3745. gapi = GroupApi(
  3746. current_user=admin,
  3747. session=dbsession,
  3748. config=self.app_config,
  3749. )
  3750. groups = [gapi.get_one_with_name('users')]
  3751. test_user = uapi.create_user(
  3752. email='test@test.test',
  3753. password='pass',
  3754. name='bob',
  3755. groups=groups,
  3756. timezone='Europe/Paris',
  3757. do_save=True,
  3758. do_notify=False,
  3759. )
  3760. uapi.save(test_user)
  3761. transaction.commit()
  3762. user_id = int(test_user.user_id)
  3763. self.testapp.authorization = (
  3764. 'Basic',
  3765. (
  3766. 'test@test.test',
  3767. 'pass',
  3768. )
  3769. )
  3770. # check before
  3771. res = self.testapp.get(
  3772. '/api/v2/users/{}'.format(user_id),
  3773. status=200
  3774. )
  3775. res = res.json_body
  3776. assert res['user_id'] == user_id
  3777. assert res['profile'] == 'users'
  3778. # Set params
  3779. params = {
  3780. 'profile': 'administrators',
  3781. }
  3782. self.testapp.put_json(
  3783. '/api/v2/users/{}/profile'.format(user_id),
  3784. params=params,
  3785. status=403,
  3786. )
  3787. # Check After
  3788. res = self.testapp.get(
  3789. '/api/v2/users/{}'.format(user_id),
  3790. status=200
  3791. )
  3792. res = res.json_body
  3793. assert res['user_id'] == user_id
  3794. assert res['profile'] == 'users'
  3795. def test_api__set_user_email__err_403__other_normal_user(self):
  3796. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3797. admin = dbsession.query(models.User) \
  3798. .filter(models.User.email == 'admin@admin.admin') \
  3799. .one()
  3800. uapi = UserApi(
  3801. current_user=admin,
  3802. session=dbsession,
  3803. config=self.app_config,
  3804. )
  3805. gapi = GroupApi(
  3806. current_user=admin,
  3807. session=dbsession,
  3808. config=self.app_config,
  3809. )
  3810. groups = [gapi.get_one_with_name('users')]
  3811. test_user = uapi.create_user(
  3812. email='test@test.test',
  3813. password='pass',
  3814. name='bob',
  3815. groups=groups,
  3816. timezone='Europe/Paris',
  3817. do_save=True,
  3818. do_notify=False,
  3819. )
  3820. test_user2 = uapi.create_user(
  3821. email='test2@test2.test2',
  3822. password='pass',
  3823. name='test',
  3824. groups=groups,
  3825. timezone='Europe/Paris',
  3826. do_save=True,
  3827. do_notify=False,
  3828. )
  3829. uapi.save(test_user2)
  3830. uapi.save(test_user)
  3831. transaction.commit()
  3832. user_id = int(test_user.user_id)
  3833. self.testapp.authorization = (
  3834. 'Basic',
  3835. (
  3836. 'test2@test2.test2',
  3837. 'pass',
  3838. )
  3839. )
  3840. # Set params
  3841. params = {
  3842. 'profile': 'administrators',
  3843. }
  3844. self.testapp.put_json(
  3845. '/api/v2/users/{}/profile'.format(user_id),
  3846. params=params,
  3847. status=403,
  3848. )
  3849. class TestSetUserEnableDisableEndpoints(FunctionalTest):
  3850. # -*- coding: utf-8 -*-
  3851. """
  3852. Tests for PUT /api/v2/users/{user_id}/enable
  3853. and PUT /api/v2/users/{user_id}/disable
  3854. """
  3855. fixtures = [BaseFixture]
  3856. def test_api_enable_user__ok_200__admin(self):
  3857. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3858. admin = dbsession.query(models.User) \
  3859. .filter(models.User.email == 'admin@admin.admin') \
  3860. .one()
  3861. uapi = UserApi(
  3862. current_user=admin,
  3863. session=dbsession,
  3864. config=self.app_config,
  3865. )
  3866. gapi = GroupApi(
  3867. current_user=admin,
  3868. session=dbsession,
  3869. config=self.app_config,
  3870. )
  3871. groups = [gapi.get_one_with_name('users')]
  3872. test_user = uapi.create_user(
  3873. email='test@test.test',
  3874. password='pass',
  3875. name='bob',
  3876. groups=groups,
  3877. timezone='Europe/Paris',
  3878. do_save=True,
  3879. do_notify=False,
  3880. )
  3881. uapi.disable(test_user, do_save=True)
  3882. uapi.save(test_user)
  3883. transaction.commit()
  3884. user_id = int(test_user.user_id)
  3885. self.testapp.authorization = (
  3886. 'Basic',
  3887. (
  3888. 'admin@admin.admin',
  3889. 'admin@admin.admin'
  3890. )
  3891. )
  3892. # check before
  3893. res = self.testapp.get(
  3894. '/api/v2/users/{}'.format(user_id),
  3895. status=200
  3896. )
  3897. res = res.json_body
  3898. assert res['user_id'] == user_id
  3899. assert res['is_active'] is False
  3900. self.testapp.put_json(
  3901. '/api/v2/users/{}/enable'.format(user_id),
  3902. status=204,
  3903. )
  3904. # Check After
  3905. res = self.testapp.get(
  3906. '/api/v2/users/{}'.format(user_id),
  3907. status=200
  3908. )
  3909. res = res.json_body
  3910. assert res['user_id'] == user_id
  3911. assert res['is_active'] is True
  3912. def test_api_disable_user__ok_200__admin(self):
  3913. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3914. admin = dbsession.query(models.User) \
  3915. .filter(models.User.email == 'admin@admin.admin') \
  3916. .one()
  3917. uapi = UserApi(
  3918. current_user=admin,
  3919. session=dbsession,
  3920. config=self.app_config,
  3921. )
  3922. gapi = GroupApi(
  3923. current_user=admin,
  3924. session=dbsession,
  3925. config=self.app_config,
  3926. )
  3927. groups = [gapi.get_one_with_name('users')]
  3928. test_user = uapi.create_user(
  3929. email='test@test.test',
  3930. password='pass',
  3931. name='bob',
  3932. groups=groups,
  3933. timezone='Europe/Paris',
  3934. do_save=True,
  3935. do_notify=False,
  3936. )
  3937. uapi.enable(test_user, do_save=True)
  3938. uapi.save(test_user)
  3939. transaction.commit()
  3940. user_id = int(test_user.user_id)
  3941. self.testapp.authorization = (
  3942. 'Basic',
  3943. (
  3944. 'admin@admin.admin',
  3945. 'admin@admin.admin'
  3946. )
  3947. )
  3948. # check before
  3949. res = self.testapp.get(
  3950. '/api/v2/users/{}'.format(user_id),
  3951. status=200
  3952. )
  3953. res = res.json_body
  3954. assert res['user_id'] == user_id
  3955. assert res['is_active'] is True
  3956. self.testapp.put_json(
  3957. '/api/v2/users/{}/disable'.format(user_id),
  3958. status=204,
  3959. )
  3960. # Check After
  3961. res = self.testapp.get(
  3962. '/api/v2/users/{}'.format(user_id),
  3963. status=200
  3964. )
  3965. res = res.json_body
  3966. assert res['user_id'] == user_id
  3967. assert res['is_active'] is False
  3968. def test_api_enable_user__err_403__other_account(self):
  3969. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3970. admin = dbsession.query(models.User) \
  3971. .filter(models.User.email == 'admin@admin.admin') \
  3972. .one()
  3973. uapi = UserApi(
  3974. current_user=admin,
  3975. session=dbsession,
  3976. config=self.app_config,
  3977. )
  3978. gapi = GroupApi(
  3979. current_user=admin,
  3980. session=dbsession,
  3981. config=self.app_config,
  3982. )
  3983. groups = [gapi.get_one_with_name('users')]
  3984. test_user = uapi.create_user(
  3985. email='test@test.test',
  3986. password='pass',
  3987. name='bob',
  3988. groups=groups,
  3989. timezone='Europe/Paris',
  3990. do_save=True,
  3991. do_notify=False,
  3992. )
  3993. test_user2 = uapi.create_user(
  3994. email='test2@test2.test2',
  3995. password='pass',
  3996. name='test2',
  3997. groups=groups,
  3998. timezone='Europe/Paris',
  3999. do_save=True,
  4000. do_notify=False,
  4001. )
  4002. uapi.disable(test_user, do_save=True)
  4003. uapi.save(test_user2)
  4004. uapi.save(test_user)
  4005. transaction.commit()
  4006. user_id = int(test_user.user_id)
  4007. self.testapp.authorization = (
  4008. 'Basic',
  4009. (
  4010. 'test2@test2.test2',
  4011. 'pass'
  4012. )
  4013. )
  4014. self.testapp.put_json(
  4015. '/api/v2/users/{}/enable'.format(user_id),
  4016. status=403,
  4017. )
  4018. def test_api_disable_user__err_403__other_account(self):
  4019. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4020. admin = dbsession.query(models.User) \
  4021. .filter(models.User.email == 'admin@admin.admin') \
  4022. .one()
  4023. uapi = UserApi(
  4024. current_user=admin,
  4025. session=dbsession,
  4026. config=self.app_config,
  4027. )
  4028. gapi = GroupApi(
  4029. current_user=admin,
  4030. session=dbsession,
  4031. config=self.app_config,
  4032. )
  4033. groups = [gapi.get_one_with_name('users')]
  4034. test_user = uapi.create_user(
  4035. email='test@test.test',
  4036. password='pass',
  4037. name='bob',
  4038. groups=groups,
  4039. timezone='Europe/Paris',
  4040. do_save=True,
  4041. do_notify=False,
  4042. )
  4043. test_user2 = uapi.create_user(
  4044. email='test2@test2.test2',
  4045. password='pass',
  4046. name='test2',
  4047. groups=groups,
  4048. timezone='Europe/Paris',
  4049. do_save=True,
  4050. do_notify=False,
  4051. )
  4052. uapi.enable(test_user, do_save=True)
  4053. uapi.save(test_user2)
  4054. uapi.save(test_user)
  4055. transaction.commit()
  4056. user_id = int(test_user.user_id)
  4057. self.testapp.authorization = (
  4058. 'Basic',
  4059. (
  4060. 'test2@test2.test2',
  4061. 'pass'
  4062. )
  4063. )
  4064. self.testapp.put_json(
  4065. '/api/v2/users/{}/disable'.format(user_id),
  4066. status=403,
  4067. )
  4068. def test_api_disable_user__ok_200__user_itself(self):
  4069. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4070. admin = dbsession.query(models.User) \
  4071. .filter(models.User.email == 'admin@admin.admin') \
  4072. .one()
  4073. uapi = UserApi(
  4074. current_user=admin,
  4075. session=dbsession,
  4076. config=self.app_config,
  4077. )
  4078. gapi = GroupApi(
  4079. current_user=admin,
  4080. session=dbsession,
  4081. config=self.app_config,
  4082. )
  4083. groups = [gapi.get_one_with_name('users')]
  4084. test_user = uapi.create_user(
  4085. email='test@test.test',
  4086. password='pass',
  4087. name='bob',
  4088. groups=groups,
  4089. timezone='Europe/Paris',
  4090. do_save=True,
  4091. do_notify=False,
  4092. )
  4093. uapi.enable(test_user, do_save=True)
  4094. uapi.save(test_user)
  4095. transaction.commit()
  4096. user_id = int(test_user.user_id)
  4097. self.testapp.authorization = (
  4098. 'Basic',
  4099. (
  4100. 'test@test.test',
  4101. 'pass'
  4102. )
  4103. )
  4104. # check before
  4105. res = self.testapp.get(
  4106. '/api/v2/users/{}'.format(user_id),
  4107. status=200
  4108. )
  4109. res = res.json_body
  4110. assert res['user_id'] == user_id
  4111. assert res['is_active'] is True
  4112. self.testapp.put_json(
  4113. '/api/v2/users/{}/disable'.format(user_id),
  4114. status=403,
  4115. )
  4116. # Check After
  4117. res = self.testapp.get(
  4118. '/api/v2/users/{}'.format(user_id),
  4119. status=200
  4120. )
  4121. res = res.json_body
  4122. assert res['user_id'] == user_id
  4123. assert res['is_active'] is True