test_user.py 163KB

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