test_user.py 161KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684
  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 len(workspace['sidebar_entries']) == 5
  2278. # TODO - G.M - 2018-08-02 - Better test for sidebar entry, make it
  2279. # not fixed on active application/content-file
  2280. sidebar_entry = workspace['sidebar_entries'][0]
  2281. assert sidebar_entry['slug'] == 'dashboard'
  2282. assert sidebar_entry['label'] == 'Dashboard'
  2283. assert sidebar_entry['route'] == '/#/workspaces/1/dashboard' # nopep8
  2284. assert sidebar_entry['hexcolor'] == "#252525"
  2285. assert sidebar_entry['fa_icon'] == "signal"
  2286. sidebar_entry = workspace['sidebar_entries'][1]
  2287. assert sidebar_entry['slug'] == 'contents/all'
  2288. assert sidebar_entry['label'] == 'All Contents'
  2289. assert sidebar_entry['route'] == "/#/workspaces/1/contents" # nopep8
  2290. assert sidebar_entry['hexcolor'] == "#fdfdfd"
  2291. assert sidebar_entry['fa_icon'] == "th"
  2292. sidebar_entry = workspace['sidebar_entries'][2]
  2293. assert sidebar_entry['slug'] == 'contents/html-document'
  2294. assert sidebar_entry['label'] == 'Text Documents'
  2295. assert sidebar_entry['route'] == '/#/workspaces/1/contents?type=html-document' # nopep8
  2296. assert sidebar_entry['hexcolor'] == "#3f52e3"
  2297. assert sidebar_entry['fa_icon'] == "file-text-o"
  2298. sidebar_entry = workspace['sidebar_entries'][3]
  2299. assert sidebar_entry['slug'] == 'contents/file'
  2300. assert sidebar_entry['label'] == 'Files'
  2301. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=file" # nopep8
  2302. assert sidebar_entry['hexcolor'] == "#FF9900"
  2303. assert sidebar_entry['fa_icon'] == "paperclip"
  2304. sidebar_entry = workspace['sidebar_entries'][4]
  2305. assert sidebar_entry['slug'] == 'contents/thread'
  2306. assert sidebar_entry['label'] == 'Threads'
  2307. assert sidebar_entry['route'] == "/#/workspaces/1/contents?type=thread" # nopep8
  2308. assert sidebar_entry['hexcolor'] == "#ad4cf9"
  2309. assert sidebar_entry['fa_icon'] == "comments-o"
  2310. def test_api__get_user_workspaces__err_403__unallowed_user(self):
  2311. """
  2312. Check obtain all workspaces reachables for one user
  2313. with another non-admin user auth.
  2314. """
  2315. self.testapp.authorization = (
  2316. 'Basic',
  2317. (
  2318. 'lawrence-not-real-email@fsf.local',
  2319. 'foobarbaz'
  2320. )
  2321. )
  2322. res = self.testapp.get('/api/v2/users/1/workspaces', status=403)
  2323. assert isinstance(res.json, dict)
  2324. assert 'code' in res.json.keys()
  2325. assert 'message' in res.json.keys()
  2326. assert 'details' in res.json.keys()
  2327. def test_api__get_user_workspaces__err_401__unregistered_user(self):
  2328. """
  2329. Check obtain all workspaces reachables for one user
  2330. without correct user auth (user unregistered).
  2331. """
  2332. self.testapp.authorization = (
  2333. 'Basic',
  2334. (
  2335. 'john@doe.doe',
  2336. 'lapin'
  2337. )
  2338. )
  2339. res = self.testapp.get('/api/v2/users/1/workspaces', status=401)
  2340. assert isinstance(res.json, dict)
  2341. assert 'code' in res.json.keys()
  2342. assert 'message' in res.json.keys()
  2343. assert 'details' in res.json.keys()
  2344. def test_api__get_user_workspaces__err_400__user_does_not_exist(self):
  2345. """
  2346. Check obtain all workspaces reachables for one user who does
  2347. not exist
  2348. with a correct user auth.
  2349. """
  2350. self.testapp.authorization = (
  2351. 'Basic',
  2352. (
  2353. 'admin@admin.admin',
  2354. 'admin@admin.admin'
  2355. )
  2356. )
  2357. res = self.testapp.get('/api/v2/users/5/workspaces', status=400)
  2358. assert isinstance(res.json, dict)
  2359. assert 'code' in res.json.keys()
  2360. assert 'message' in res.json.keys()
  2361. assert 'details' in res.json.keys()
  2362. class TestUserEndpoint(FunctionalTest):
  2363. """
  2364. Tests for GET/POST /api/v2/users/{user_id}
  2365. """
  2366. fixtures = [BaseFixture]
  2367. def test_api__get_user__ok_200__admin(self):
  2368. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2369. admin = dbsession.query(models.User) \
  2370. .filter(models.User.email == 'admin@admin.admin') \
  2371. .one()
  2372. uapi = UserApi(
  2373. current_user=admin,
  2374. session=dbsession,
  2375. config=self.app_config,
  2376. )
  2377. gapi = GroupApi(
  2378. current_user=admin,
  2379. session=dbsession,
  2380. config=self.app_config,
  2381. )
  2382. groups = [gapi.get_one_with_name('users')]
  2383. test_user = uapi.create_user(
  2384. email='test@test.test',
  2385. password='pass',
  2386. name='bob',
  2387. groups=groups,
  2388. timezone='Europe/Paris',
  2389. do_save=True,
  2390. do_notify=False,
  2391. )
  2392. uapi.save(test_user)
  2393. transaction.commit()
  2394. user_id = int(test_user.user_id)
  2395. self.testapp.authorization = (
  2396. 'Basic',
  2397. (
  2398. 'admin@admin.admin',
  2399. 'admin@admin.admin'
  2400. )
  2401. )
  2402. res = self.testapp.get(
  2403. '/api/v2/users/{}'.format(user_id),
  2404. status=200
  2405. )
  2406. res = res.json_body
  2407. assert res['user_id'] == user_id
  2408. assert res['created']
  2409. assert res['is_active'] is True
  2410. assert res['profile'] == 'users'
  2411. assert res['email'] == 'test@test.test'
  2412. assert res['public_name'] == 'bob'
  2413. assert res['timezone'] == 'Europe/Paris'
  2414. def test_api__get_user__ok_200__user_itself(self):
  2415. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2416. admin = dbsession.query(models.User) \
  2417. .filter(models.User.email == 'admin@admin.admin') \
  2418. .one()
  2419. uapi = UserApi(
  2420. current_user=admin,
  2421. session=dbsession,
  2422. config=self.app_config,
  2423. )
  2424. gapi = GroupApi(
  2425. current_user=admin,
  2426. session=dbsession,
  2427. config=self.app_config,
  2428. )
  2429. groups = [gapi.get_one_with_name('users')]
  2430. test_user = uapi.create_user(
  2431. email='test@test.test',
  2432. password='pass',
  2433. name='bob',
  2434. groups=groups,
  2435. timezone='Europe/Paris',
  2436. do_save=True,
  2437. do_notify=False,
  2438. )
  2439. uapi.save(test_user)
  2440. transaction.commit()
  2441. user_id = int(test_user.user_id)
  2442. self.testapp.authorization = (
  2443. 'Basic',
  2444. (
  2445. 'test@test.test',
  2446. 'pass'
  2447. )
  2448. )
  2449. res = self.testapp.get(
  2450. '/api/v2/users/{}'.format(user_id),
  2451. status=200
  2452. )
  2453. res = res.json_body
  2454. assert res['user_id'] == user_id
  2455. assert res['created']
  2456. assert res['is_active'] is True
  2457. assert res['profile'] == 'users'
  2458. assert res['email'] == 'test@test.test'
  2459. assert res['public_name'] == 'bob'
  2460. assert res['timezone'] == 'Europe/Paris'
  2461. def test_api__get_user__err_403__other_normal_user(self):
  2462. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2463. admin = dbsession.query(models.User) \
  2464. .filter(models.User.email == 'admin@admin.admin') \
  2465. .one()
  2466. uapi = UserApi(
  2467. current_user=admin,
  2468. session=dbsession,
  2469. config=self.app_config,
  2470. )
  2471. gapi = GroupApi(
  2472. current_user=admin,
  2473. session=dbsession,
  2474. config=self.app_config,
  2475. )
  2476. groups = [gapi.get_one_with_name('users')]
  2477. test_user = uapi.create_user(
  2478. email='test@test.test',
  2479. password='pass',
  2480. name='bob',
  2481. groups=groups,
  2482. timezone='Europe/Paris',
  2483. do_save=True,
  2484. do_notify=False,
  2485. )
  2486. test_user2 = uapi.create_user(
  2487. email='test2@test2.test2',
  2488. password='pass',
  2489. name='bob2',
  2490. groups=groups,
  2491. timezone='Europe/Paris',
  2492. do_save=True,
  2493. do_notify=False,
  2494. )
  2495. uapi.save(test_user2)
  2496. uapi.save(test_user)
  2497. transaction.commit()
  2498. user_id = int(test_user.user_id)
  2499. self.testapp.authorization = (
  2500. 'Basic',
  2501. (
  2502. 'test2@test2.test2',
  2503. 'pass'
  2504. )
  2505. )
  2506. self.testapp.get(
  2507. '/api/v2/users/{}'.format(user_id),
  2508. status=403
  2509. )
  2510. def test_api__create_user__ok_200__full_admin(self):
  2511. self.testapp.authorization = (
  2512. 'Basic',
  2513. (
  2514. 'admin@admin.admin',
  2515. 'admin@admin.admin'
  2516. )
  2517. )
  2518. params = {
  2519. 'email': 'test@test.test',
  2520. 'password': 'mysuperpassword',
  2521. 'profile': 'users',
  2522. 'timezone': 'Europe/Paris',
  2523. 'public_name': 'test user',
  2524. 'email_notification': False,
  2525. }
  2526. res = self.testapp.post_json(
  2527. '/api/v2/users',
  2528. status=200,
  2529. params=params,
  2530. )
  2531. res = res.json_body
  2532. assert res['user_id']
  2533. user_id = res['user_id']
  2534. assert res['created']
  2535. assert res['is_active'] is True
  2536. assert res['profile'] == 'users'
  2537. assert res['email'] == 'test@test.test'
  2538. assert res['public_name'] == 'test user'
  2539. assert res['timezone'] == 'Europe/Paris'
  2540. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2541. admin = dbsession.query(models.User) \
  2542. .filter(models.User.email == 'admin@admin.admin') \
  2543. .one()
  2544. uapi = UserApi(
  2545. current_user=admin,
  2546. session=dbsession,
  2547. config=self.app_config,
  2548. )
  2549. user = uapi.get_one(user_id)
  2550. assert user.email == 'test@test.test'
  2551. assert user.validate_password('mysuperpassword')
  2552. def test_api__create_user__ok_200__limited_admin(self):
  2553. self.testapp.authorization = (
  2554. 'Basic',
  2555. (
  2556. 'admin@admin.admin',
  2557. 'admin@admin.admin'
  2558. )
  2559. )
  2560. params = {
  2561. 'email': 'test@test.test',
  2562. 'email_notification': False,
  2563. }
  2564. res = self.testapp.post_json(
  2565. '/api/v2/users',
  2566. status=200,
  2567. params=params,
  2568. )
  2569. res = res.json_body
  2570. assert res['user_id']
  2571. user_id = res['user_id']
  2572. assert res['created']
  2573. assert res['is_active'] is True
  2574. assert res['profile'] == 'users'
  2575. assert res['email'] == 'test@test.test'
  2576. assert res['public_name'] == 'test'
  2577. assert res['timezone'] == ''
  2578. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2579. admin = dbsession.query(models.User) \
  2580. .filter(models.User.email == 'admin@admin.admin') \
  2581. .one()
  2582. uapi = UserApi(
  2583. current_user=admin,
  2584. session=dbsession,
  2585. config=self.app_config,
  2586. )
  2587. user = uapi.get_one(user_id)
  2588. assert user.email == 'test@test.test'
  2589. assert user.password
  2590. def test_api__create_user__err_400__email_already_in_db(self):
  2591. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2592. admin = dbsession.query(models.User) \
  2593. .filter(models.User.email == 'admin@admin.admin') \
  2594. .one()
  2595. uapi = UserApi(
  2596. current_user=admin,
  2597. session=dbsession,
  2598. config=self.app_config,
  2599. )
  2600. gapi = GroupApi(
  2601. current_user=admin,
  2602. session=dbsession,
  2603. config=self.app_config,
  2604. )
  2605. groups = [gapi.get_one_with_name('users')]
  2606. test_user = uapi.create_user(
  2607. email='test@test.test',
  2608. password='pass',
  2609. name='bob',
  2610. groups=groups,
  2611. timezone='Europe/Paris',
  2612. do_save=True,
  2613. do_notify=False,
  2614. )
  2615. uapi.save(test_user)
  2616. transaction.commit()
  2617. self.testapp.authorization = (
  2618. 'Basic',
  2619. (
  2620. 'admin@admin.admin',
  2621. 'admin@admin.admin'
  2622. )
  2623. )
  2624. params = {
  2625. 'email': 'test@test.test',
  2626. 'password': 'mysuperpassword',
  2627. 'profile': 'users',
  2628. 'timezone': 'Europe/Paris',
  2629. 'public_name': 'test user',
  2630. 'email_notification': False,
  2631. }
  2632. res = self.testapp.post_json(
  2633. '/api/v2/users',
  2634. status=400,
  2635. params=params,
  2636. )
  2637. def test_api__create_user__err_403__other_user(self):
  2638. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2639. admin = dbsession.query(models.User) \
  2640. .filter(models.User.email == 'admin@admin.admin') \
  2641. .one()
  2642. uapi = UserApi(
  2643. current_user=admin,
  2644. session=dbsession,
  2645. config=self.app_config,
  2646. )
  2647. gapi = GroupApi(
  2648. current_user=admin,
  2649. session=dbsession,
  2650. config=self.app_config,
  2651. )
  2652. groups = [gapi.get_one_with_name('users')]
  2653. test_user = uapi.create_user(
  2654. email='test@test.test',
  2655. password='pass',
  2656. name='bob',
  2657. groups=groups,
  2658. timezone='Europe/Paris',
  2659. do_save=True,
  2660. do_notify=False,
  2661. )
  2662. uapi.save(test_user)
  2663. transaction.commit()
  2664. self.testapp.authorization = (
  2665. 'Basic',
  2666. (
  2667. 'test@test.test',
  2668. 'pass',
  2669. )
  2670. )
  2671. params = {
  2672. 'email': 'test2@test2.test2',
  2673. 'password': 'mysuperpassword',
  2674. 'profile': 'users',
  2675. 'timezone': 'Europe/Paris',
  2676. 'public_name': 'test user',
  2677. 'email_notification': False,
  2678. }
  2679. res = self.testapp.post_json(
  2680. '/api/v2/users',
  2681. status=403,
  2682. params=params,
  2683. )
  2684. class TestUserWithNotificationEndpoint(FunctionalTest):
  2685. """
  2686. Tests for POST /api/v2/users/{user_id}
  2687. """
  2688. config_section = 'functional_test_with_mail_test_sync'
  2689. def test_api__create_user__ok_200__full_admin_with_notif(self):
  2690. requests.delete('http://127.0.0.1:8025/api/v1/messages')
  2691. self.testapp.authorization = (
  2692. 'Basic',
  2693. (
  2694. 'admin@admin.admin',
  2695. 'admin@admin.admin'
  2696. )
  2697. )
  2698. params = {
  2699. 'email': 'test@test.test',
  2700. 'password': 'mysuperpassword',
  2701. 'profile': 'users',
  2702. 'timezone': 'Europe/Paris',
  2703. 'public_name': 'test user',
  2704. 'email_notification': True,
  2705. }
  2706. res = self.testapp.post_json(
  2707. '/api/v2/users',
  2708. status=200,
  2709. params=params,
  2710. )
  2711. res = res.json_body
  2712. assert res['user_id']
  2713. user_id = res['user_id']
  2714. assert res['created']
  2715. assert res['is_active'] is True
  2716. assert res['profile'] == 'users'
  2717. assert res['email'] == 'test@test.test'
  2718. assert res['public_name'] == 'test user'
  2719. assert res['timezone'] == 'Europe/Paris'
  2720. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2721. admin = dbsession.query(models.User) \
  2722. .filter(models.User.email == 'admin@admin.admin') \
  2723. .one()
  2724. uapi = UserApi(
  2725. current_user=admin,
  2726. session=dbsession,
  2727. config=self.app_config,
  2728. )
  2729. user = uapi.get_one(user_id)
  2730. assert user.email == 'test@test.test'
  2731. assert user.validate_password('mysuperpassword')
  2732. # check mail received
  2733. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  2734. response = response.json()
  2735. assert len(response) == 1
  2736. headers = response[0]['Content']['Headers']
  2737. assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>' # nopep8
  2738. assert headers['To'][0] == 'test user <test@test.test>'
  2739. assert headers['Subject'][0] == '[TRACIM] Created account'
  2740. # TODO - G.M - 2018-08-02 - Place cleanup outside of the test
  2741. requests.delete('http://127.0.0.1:8025/api/v1/messages')
  2742. def test_api__create_user__ok_200__limited_admin_with_notif(self):
  2743. requests.delete('http://127.0.0.1:8025/api/v1/messages')
  2744. self.testapp.authorization = (
  2745. 'Basic',
  2746. (
  2747. 'admin@admin.admin',
  2748. 'admin@admin.admin'
  2749. )
  2750. )
  2751. params = {
  2752. 'email': 'test@test.test',
  2753. 'email_notification': True,
  2754. }
  2755. res = self.testapp.post_json(
  2756. '/api/v2/users',
  2757. status=200,
  2758. params=params,
  2759. )
  2760. res = res.json_body
  2761. assert res['user_id']
  2762. user_id = res['user_id']
  2763. assert res['created']
  2764. assert res['is_active'] is True
  2765. assert res['profile'] == 'users'
  2766. assert res['email'] == 'test@test.test'
  2767. assert res['public_name'] == 'test'
  2768. assert res['timezone'] == ''
  2769. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2770. admin = dbsession.query(models.User) \
  2771. .filter(models.User.email == 'admin@admin.admin') \
  2772. .one()
  2773. uapi = UserApi(
  2774. current_user=admin,
  2775. session=dbsession,
  2776. config=self.app_config,
  2777. )
  2778. user = uapi.get_one(user_id)
  2779. assert user.email == 'test@test.test'
  2780. assert user.password
  2781. # check mail received
  2782. response = requests.get('http://127.0.0.1:8025/api/v1/messages')
  2783. response = response.json()
  2784. assert len(response) == 1
  2785. headers = response[0]['Content']['Headers']
  2786. assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>' # nopep8
  2787. assert headers['To'][0] == 'test <test@test.test>'
  2788. assert headers['Subject'][0] == '[TRACIM] Created account'
  2789. # TODO - G.M - 2018-08-02 - Place cleanup outside of the test
  2790. requests.delete('http://127.0.0.1:8025/api/v1/messages')
  2791. class TestUsersEndpoint(FunctionalTest):
  2792. # -*- coding: utf-8 -*-
  2793. """
  2794. Tests for GET /api/v2/users
  2795. """
  2796. fixtures = [BaseFixture]
  2797. def test_api__get_user__ok_200__admin(self):
  2798. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2799. admin = dbsession.query(models.User) \
  2800. .filter(models.User.email == 'admin@admin.admin') \
  2801. .one()
  2802. uapi = UserApi(
  2803. current_user=admin,
  2804. session=dbsession,
  2805. config=self.app_config,
  2806. )
  2807. gapi = GroupApi(
  2808. current_user=admin,
  2809. session=dbsession,
  2810. config=self.app_config,
  2811. )
  2812. groups = [gapi.get_one_with_name('users')]
  2813. test_user = uapi.create_user(
  2814. email='test@test.test',
  2815. password='pass',
  2816. name='bob',
  2817. groups=groups,
  2818. timezone='Europe/Paris',
  2819. do_save=True,
  2820. do_notify=False,
  2821. )
  2822. uapi.save(test_user)
  2823. transaction.commit()
  2824. user_id = int(test_user.user_id)
  2825. self.testapp.authorization = (
  2826. 'Basic',
  2827. (
  2828. 'admin@admin.admin',
  2829. 'admin@admin.admin'
  2830. )
  2831. )
  2832. res = self.testapp.get(
  2833. '/api/v2/users',
  2834. status=200
  2835. )
  2836. res = res.json_body
  2837. assert len(res) == 2
  2838. assert res[0]['user_id'] == admin.user_id
  2839. assert res[0]['public_name'] == admin.display_name
  2840. assert res[0]['avatar_url'] is None
  2841. assert res[1]['user_id'] == test_user.user_id
  2842. assert res[1]['public_name'] == test_user.display_name
  2843. assert res[1]['avatar_url'] is None
  2844. def test_api__get_user__err_403__normal_user(self):
  2845. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2846. admin = dbsession.query(models.User) \
  2847. .filter(models.User.email == 'admin@admin.admin') \
  2848. .one()
  2849. uapi = UserApi(
  2850. current_user=admin,
  2851. session=dbsession,
  2852. config=self.app_config,
  2853. )
  2854. gapi = GroupApi(
  2855. current_user=admin,
  2856. session=dbsession,
  2857. config=self.app_config,
  2858. )
  2859. groups = [gapi.get_one_with_name('users')]
  2860. test_user = uapi.create_user(
  2861. email='test@test.test',
  2862. password='pass',
  2863. name='bob',
  2864. groups=groups,
  2865. timezone='Europe/Paris',
  2866. do_save=True,
  2867. do_notify=False,
  2868. )
  2869. uapi.save(test_user)
  2870. transaction.commit()
  2871. user_id = int(test_user.user_id)
  2872. self.testapp.authorization = (
  2873. 'Basic',
  2874. (
  2875. 'test@test.test',
  2876. 'pass'
  2877. )
  2878. )
  2879. self.testapp.get(
  2880. '/api/v2/users',
  2881. status=403
  2882. )
  2883. class TestKnownMembersEndpoint(FunctionalTest):
  2884. # -*- coding: utf-8 -*-
  2885. """
  2886. Tests for GET /api/v2/users/{user_id}
  2887. """
  2888. fixtures = [BaseFixture]
  2889. def test_api__get_user__ok_200__admin__by_name(self):
  2890. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2891. admin = dbsession.query(models.User) \
  2892. .filter(models.User.email == 'admin@admin.admin') \
  2893. .one()
  2894. uapi = UserApi(
  2895. current_user=admin,
  2896. session=dbsession,
  2897. config=self.app_config,
  2898. )
  2899. gapi = GroupApi(
  2900. current_user=admin,
  2901. session=dbsession,
  2902. config=self.app_config,
  2903. )
  2904. groups = [gapi.get_one_with_name('users')]
  2905. test_user = uapi.create_user(
  2906. email='test@test.test',
  2907. password='pass',
  2908. name='bob',
  2909. groups=groups,
  2910. timezone='Europe/Paris',
  2911. do_save=True,
  2912. do_notify=False,
  2913. )
  2914. test_user2 = uapi.create_user(
  2915. email='test2@test2.test2',
  2916. password='pass',
  2917. name='bob2',
  2918. groups=groups,
  2919. timezone='Europe/Paris',
  2920. do_save=True,
  2921. do_notify=False,
  2922. )
  2923. uapi.save(test_user)
  2924. uapi.save(test_user2)
  2925. transaction.commit()
  2926. user_id = int(admin.user_id)
  2927. self.testapp.authorization = (
  2928. 'Basic',
  2929. (
  2930. 'admin@admin.admin',
  2931. 'admin@admin.admin'
  2932. )
  2933. )
  2934. params = {
  2935. 'acp': 'bob',
  2936. }
  2937. res = self.testapp.get(
  2938. '/api/v2/users/{user_id}/known_members'.format(user_id=user_id),
  2939. status=200,
  2940. params=params,
  2941. )
  2942. res = res.json_body
  2943. assert len(res) == 2
  2944. assert res[0]['user_id'] == test_user.user_id
  2945. assert res[0]['public_name'] == test_user.display_name
  2946. assert res[0]['avatar_url'] is None
  2947. assert res[1]['user_id'] == test_user2.user_id
  2948. assert res[1]['public_name'] == test_user2.display_name
  2949. assert res[1]['avatar_url'] is None
  2950. def test_api__get_user__ok_200__admin__by_email(self):
  2951. dbsession = get_tm_session(self.session_factory, transaction.manager)
  2952. admin = dbsession.query(models.User) \
  2953. .filter(models.User.email == 'admin@admin.admin') \
  2954. .one()
  2955. uapi = UserApi(
  2956. current_user=admin,
  2957. session=dbsession,
  2958. config=self.app_config,
  2959. )
  2960. gapi = GroupApi(
  2961. current_user=admin,
  2962. session=dbsession,
  2963. config=self.app_config,
  2964. )
  2965. groups = [gapi.get_one_with_name('users')]
  2966. test_user = uapi.create_user(
  2967. email='test@test.test',
  2968. password='pass',
  2969. name='bob',
  2970. groups=groups,
  2971. timezone='Europe/Paris',
  2972. do_save=True,
  2973. do_notify=False,
  2974. )
  2975. test_user2 = uapi.create_user(
  2976. email='test2@test2.test2',
  2977. password='pass',
  2978. name='bob2',
  2979. groups=groups,
  2980. timezone='Europe/Paris',
  2981. do_save=True,
  2982. do_notify=False,
  2983. )
  2984. uapi.save(test_user)
  2985. uapi.save(test_user2)
  2986. transaction.commit()
  2987. user_id = int(admin.user_id)
  2988. self.testapp.authorization = (
  2989. 'Basic',
  2990. (
  2991. 'admin@admin.admin',
  2992. 'admin@admin.admin'
  2993. )
  2994. )
  2995. params = {
  2996. 'acp': 'test',
  2997. }
  2998. res = self.testapp.get(
  2999. '/api/v2/users/{user_id}/known_members'.format(user_id=user_id),
  3000. status=200,
  3001. params=params,
  3002. )
  3003. res = res.json_body
  3004. assert len(res) == 2
  3005. assert res[0]['user_id'] == test_user.user_id
  3006. assert res[0]['public_name'] == test_user.display_name
  3007. assert res[0]['avatar_url'] is None
  3008. assert res[1]['user_id'] == test_user2.user_id
  3009. assert res[1]['public_name'] == test_user2.display_name
  3010. assert res[1]['avatar_url'] is None
  3011. def test_api__get_user__err_403__admin__too_small_acp(self):
  3012. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3013. admin = dbsession.query(models.User) \
  3014. .filter(models.User.email == 'admin@admin.admin') \
  3015. .one()
  3016. uapi = UserApi(
  3017. current_user=admin,
  3018. session=dbsession,
  3019. config=self.app_config,
  3020. )
  3021. gapi = GroupApi(
  3022. current_user=admin,
  3023. session=dbsession,
  3024. config=self.app_config,
  3025. )
  3026. groups = [gapi.get_one_with_name('users')]
  3027. test_user = uapi.create_user(
  3028. email='test@test.test',
  3029. password='pass',
  3030. name='bob',
  3031. groups=groups,
  3032. timezone='Europe/Paris',
  3033. do_save=True,
  3034. do_notify=False,
  3035. )
  3036. test_user2 = uapi.create_user(
  3037. email='test2@test2.test2',
  3038. password='pass',
  3039. name='bob2',
  3040. groups=groups,
  3041. timezone='Europe/Paris',
  3042. do_save=True,
  3043. do_notify=False,
  3044. )
  3045. uapi.save(test_user)
  3046. transaction.commit()
  3047. user_id = int(admin.user_id)
  3048. self.testapp.authorization = (
  3049. 'Basic',
  3050. (
  3051. 'admin@admin.admin',
  3052. 'admin@admin.admin'
  3053. )
  3054. )
  3055. params = {
  3056. 'acp': 't',
  3057. }
  3058. res = self.testapp.get(
  3059. '/api/v2/users/{user_id}/known_members'.format(user_id=user_id),
  3060. status=400,
  3061. params=params
  3062. )
  3063. def test_api__get_user__ok_200__normal_user_by_email(self):
  3064. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3065. admin = dbsession.query(models.User) \
  3066. .filter(models.User.email == 'admin@admin.admin') \
  3067. .one()
  3068. uapi = UserApi(
  3069. current_user=admin,
  3070. session=dbsession,
  3071. config=self.app_config,
  3072. )
  3073. gapi = GroupApi(
  3074. current_user=admin,
  3075. session=dbsession,
  3076. config=self.app_config,
  3077. )
  3078. groups = [gapi.get_one_with_name('users')]
  3079. test_user = uapi.create_user(
  3080. email='test@test.test',
  3081. password='pass',
  3082. name='bob',
  3083. groups=groups,
  3084. timezone='Europe/Paris',
  3085. do_save=True,
  3086. do_notify=False,
  3087. )
  3088. test_user2 = uapi.create_user(
  3089. email='test2@test2.test2',
  3090. password='pass',
  3091. name='bob2',
  3092. groups=groups,
  3093. timezone='Europe/Paris',
  3094. do_save=True,
  3095. do_notify=False,
  3096. )
  3097. test_user3 = uapi.create_user(
  3098. email='test3@test3.test3',
  3099. password='pass',
  3100. name='bob3',
  3101. groups=groups,
  3102. timezone='Europe/Paris',
  3103. do_save=True,
  3104. do_notify=False,
  3105. )
  3106. uapi.save(test_user)
  3107. uapi.save(test_user2)
  3108. uapi.save(test_user3)
  3109. workspace_api = WorkspaceApi(
  3110. current_user=admin,
  3111. session=dbsession,
  3112. config=self.app_config
  3113. )
  3114. workspace = WorkspaceApi(
  3115. current_user=admin,
  3116. session=dbsession,
  3117. config=self.app_config,
  3118. ).create_workspace(
  3119. 'test workspace',
  3120. save_now=True
  3121. )
  3122. role_api = RoleApi(
  3123. current_user=admin,
  3124. session=dbsession,
  3125. config=self.app_config,
  3126. )
  3127. role_api.create_one(test_user, workspace, UserRoleInWorkspace.READER, False)
  3128. role_api.create_one(test_user2, workspace, UserRoleInWorkspace.READER, False)
  3129. transaction.commit()
  3130. user_id = int(test_user.user_id)
  3131. self.testapp.authorization = (
  3132. 'Basic',
  3133. (
  3134. 'test@test.test',
  3135. 'pass'
  3136. )
  3137. )
  3138. params = {
  3139. 'acp': 'test',
  3140. }
  3141. res = self.testapp.get(
  3142. '/api/v2/users/{user_id}/known_members'.format(user_id=user_id),
  3143. status=200,
  3144. params=params
  3145. )
  3146. res = res.json_body
  3147. assert len(res) == 2
  3148. assert res[0]['user_id'] == test_user.user_id
  3149. assert res[0]['public_name'] == test_user.display_name
  3150. assert res[0]['avatar_url'] is None
  3151. assert res[1]['user_id'] == test_user2.user_id
  3152. assert res[1]['public_name'] == test_user2.display_name
  3153. assert res[1]['avatar_url'] is None
  3154. class TestSetEmailEndpoint(FunctionalTest):
  3155. # -*- coding: utf-8 -*-
  3156. """
  3157. Tests for PUT /api/v2/users/{user_id}/email
  3158. """
  3159. fixtures = [BaseFixture]
  3160. def test_api__set_user_email__ok_200__admin(self):
  3161. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3162. admin = dbsession.query(models.User) \
  3163. .filter(models.User.email == 'admin@admin.admin') \
  3164. .one()
  3165. uapi = UserApi(
  3166. current_user=admin,
  3167. session=dbsession,
  3168. config=self.app_config,
  3169. )
  3170. gapi = GroupApi(
  3171. current_user=admin,
  3172. session=dbsession,
  3173. config=self.app_config,
  3174. )
  3175. groups = [gapi.get_one_with_name('users')]
  3176. test_user = uapi.create_user(
  3177. email='test@test.test',
  3178. password='pass',
  3179. name='bob',
  3180. groups=groups,
  3181. timezone='Europe/Paris',
  3182. do_save=True,
  3183. do_notify=False,
  3184. )
  3185. uapi.save(test_user)
  3186. transaction.commit()
  3187. user_id = int(test_user.user_id)
  3188. self.testapp.authorization = (
  3189. 'Basic',
  3190. (
  3191. 'admin@admin.admin',
  3192. 'admin@admin.admin'
  3193. )
  3194. )
  3195. # check before
  3196. res = self.testapp.get(
  3197. '/api/v2/users/{}'.format(user_id),
  3198. status=200
  3199. )
  3200. res = res.json_body
  3201. assert res['email'] == 'test@test.test'
  3202. # Set password
  3203. params = {
  3204. 'email': 'mysuperemail@email.fr',
  3205. 'loggedin_user_password': 'admin@admin.admin',
  3206. }
  3207. self.testapp.put_json(
  3208. '/api/v2/users/{}/email'.format(user_id),
  3209. params=params,
  3210. status=200,
  3211. )
  3212. # Check After
  3213. res = self.testapp.get(
  3214. '/api/v2/users/{}'.format(user_id),
  3215. status=200
  3216. )
  3217. res = res.json_body
  3218. assert res['email'] == 'mysuperemail@email.fr'
  3219. def test_api__set_user_email__err_400__admin_same_email(self):
  3220. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3221. admin = dbsession.query(models.User) \
  3222. .filter(models.User.email == 'admin@admin.admin') \
  3223. .one()
  3224. uapi = UserApi(
  3225. current_user=admin,
  3226. session=dbsession,
  3227. config=self.app_config,
  3228. )
  3229. gapi = GroupApi(
  3230. current_user=admin,
  3231. session=dbsession,
  3232. config=self.app_config,
  3233. )
  3234. groups = [gapi.get_one_with_name('users')]
  3235. test_user = uapi.create_user(
  3236. email='test@test.test',
  3237. password='pass',
  3238. name='bob',
  3239. groups=groups,
  3240. timezone='Europe/Paris',
  3241. do_save=True,
  3242. do_notify=False,
  3243. )
  3244. uapi.save(test_user)
  3245. transaction.commit()
  3246. user_id = int(test_user.user_id)
  3247. self.testapp.authorization = (
  3248. 'Basic',
  3249. (
  3250. 'admin@admin.admin',
  3251. 'admin@admin.admin'
  3252. )
  3253. )
  3254. # check before
  3255. res = self.testapp.get(
  3256. '/api/v2/users/{}'.format(user_id),
  3257. status=200
  3258. )
  3259. res = res.json_body
  3260. assert res['email'] == 'test@test.test'
  3261. # Set password
  3262. params = {
  3263. 'email': 'admin@admin.admin',
  3264. 'loggedin_user_password': 'admin@admin.admin',
  3265. }
  3266. self.testapp.put_json(
  3267. '/api/v2/users/{}/email'.format(user_id),
  3268. params=params,
  3269. status=400,
  3270. )
  3271. # Check After
  3272. res = self.testapp.get(
  3273. '/api/v2/users/{}'.format(user_id),
  3274. status=200
  3275. )
  3276. res = res.json_body
  3277. assert res['email'] == 'test@test.test'
  3278. def test_api__set_user_email__err_403__admin_wrong_password(self):
  3279. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3280. admin = dbsession.query(models.User) \
  3281. .filter(models.User.email == 'admin@admin.admin') \
  3282. .one()
  3283. uapi = UserApi(
  3284. current_user=admin,
  3285. session=dbsession,
  3286. config=self.app_config,
  3287. )
  3288. gapi = GroupApi(
  3289. current_user=admin,
  3290. session=dbsession,
  3291. config=self.app_config,
  3292. )
  3293. groups = [gapi.get_one_with_name('users')]
  3294. test_user = uapi.create_user(
  3295. email='test@test.test',
  3296. password='pass',
  3297. name='bob',
  3298. groups=groups,
  3299. timezone='Europe/Paris',
  3300. do_save=True,
  3301. do_notify=False,
  3302. )
  3303. uapi.save(test_user)
  3304. transaction.commit()
  3305. user_id = int(test_user.user_id)
  3306. self.testapp.authorization = (
  3307. 'Basic',
  3308. (
  3309. 'admin@admin.admin',
  3310. 'admin@admin.admin'
  3311. )
  3312. )
  3313. # check before
  3314. res = self.testapp.get(
  3315. '/api/v2/users/{}'.format(user_id),
  3316. status=200
  3317. )
  3318. res = res.json_body
  3319. assert res['email'] == 'test@test.test'
  3320. # Set password
  3321. params = {
  3322. 'email': 'mysuperemail@email.fr',
  3323. 'loggedin_user_password': 'badpassword',
  3324. }
  3325. self.testapp.put_json(
  3326. '/api/v2/users/{}/email'.format(user_id),
  3327. params=params,
  3328. status=403,
  3329. )
  3330. # Check After
  3331. res = self.testapp.get(
  3332. '/api/v2/users/{}'.format(user_id),
  3333. status=200
  3334. )
  3335. res = res.json_body
  3336. assert res['email'] == 'test@test.test'
  3337. def test_api__set_user_email__err_400__admin_string_is_not_email(self):
  3338. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3339. admin = dbsession.query(models.User) \
  3340. .filter(models.User.email == 'admin@admin.admin') \
  3341. .one()
  3342. uapi = UserApi(
  3343. current_user=admin,
  3344. session=dbsession,
  3345. config=self.app_config,
  3346. )
  3347. gapi = GroupApi(
  3348. current_user=admin,
  3349. session=dbsession,
  3350. config=self.app_config,
  3351. )
  3352. groups = [gapi.get_one_with_name('users')]
  3353. test_user = uapi.create_user(
  3354. email='test@test.test',
  3355. password='pass',
  3356. name='bob',
  3357. groups=groups,
  3358. timezone='Europe/Paris',
  3359. do_save=True,
  3360. do_notify=False,
  3361. )
  3362. uapi.save(test_user)
  3363. transaction.commit()
  3364. user_id = int(test_user.user_id)
  3365. self.testapp.authorization = (
  3366. 'Basic',
  3367. (
  3368. 'admin@admin.admin',
  3369. 'admin@admin.admin'
  3370. )
  3371. )
  3372. # check before
  3373. res = self.testapp.get(
  3374. '/api/v2/users/{}'.format(user_id),
  3375. status=200
  3376. )
  3377. res = res.json_body
  3378. assert res['email'] == 'test@test.test'
  3379. # Set password
  3380. params = {
  3381. 'email': 'thatisnotandemail',
  3382. 'loggedin_user_password': 'admin@admin.admin',
  3383. }
  3384. self.testapp.put_json(
  3385. '/api/v2/users/{}/email'.format(user_id),
  3386. params=params,
  3387. status=400,
  3388. )
  3389. # Check After
  3390. res = self.testapp.get(
  3391. '/api/v2/users/{}'.format(user_id),
  3392. status=200
  3393. )
  3394. res = res.json_body
  3395. assert res['email'] == 'test@test.test'
  3396. def test_api__set_user_email__ok_200__user_itself(self):
  3397. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3398. admin = dbsession.query(models.User) \
  3399. .filter(models.User.email == 'admin@admin.admin') \
  3400. .one()
  3401. uapi = UserApi(
  3402. current_user=admin,
  3403. session=dbsession,
  3404. config=self.app_config,
  3405. )
  3406. gapi = GroupApi(
  3407. current_user=admin,
  3408. session=dbsession,
  3409. config=self.app_config,
  3410. )
  3411. groups = [gapi.get_one_with_name('users')]
  3412. test_user = uapi.create_user(
  3413. email='test@test.test',
  3414. password='pass',
  3415. name='bob',
  3416. groups=groups,
  3417. timezone='Europe/Paris',
  3418. do_save=True,
  3419. do_notify=False,
  3420. )
  3421. uapi.save(test_user)
  3422. transaction.commit()
  3423. user_id = int(test_user.user_id)
  3424. self.testapp.authorization = (
  3425. 'Basic',
  3426. (
  3427. 'test@test.test',
  3428. 'pass'
  3429. )
  3430. )
  3431. # check before
  3432. res = self.testapp.get(
  3433. '/api/v2/users/{}'.format(user_id),
  3434. status=200
  3435. )
  3436. res = res.json_body
  3437. assert res['email'] == 'test@test.test'
  3438. # Set password
  3439. params = {
  3440. 'email': 'mysuperemail@email.fr',
  3441. 'loggedin_user_password': 'pass',
  3442. }
  3443. self.testapp.put_json(
  3444. '/api/v2/users/{}/email'.format(user_id),
  3445. params=params,
  3446. status=200,
  3447. )
  3448. self.testapp.authorization = (
  3449. 'Basic',
  3450. (
  3451. 'mysuperemail@email.fr',
  3452. 'pass'
  3453. )
  3454. )
  3455. # Check After
  3456. res = self.testapp.get(
  3457. '/api/v2/users/{}'.format(user_id),
  3458. status=200
  3459. )
  3460. res = res.json_body
  3461. assert res['email'] == 'mysuperemail@email.fr'
  3462. def test_api__set_user_email__err_403__other_normal_user(self):
  3463. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3464. admin = dbsession.query(models.User) \
  3465. .filter(models.User.email == 'admin@admin.admin') \
  3466. .one()
  3467. uapi = UserApi(
  3468. current_user=admin,
  3469. session=dbsession,
  3470. config=self.app_config,
  3471. )
  3472. gapi = GroupApi(
  3473. current_user=admin,
  3474. session=dbsession,
  3475. config=self.app_config,
  3476. )
  3477. groups = [gapi.get_one_with_name('users')]
  3478. test_user = uapi.create_user(
  3479. email='test@test.test',
  3480. password='pass',
  3481. name='bob',
  3482. groups=groups,
  3483. timezone='Europe/Paris',
  3484. do_save=True,
  3485. do_notify=False,
  3486. )
  3487. test_user2 = uapi.create_user(
  3488. email='test2@test2.test2',
  3489. password='pass',
  3490. name='bob2',
  3491. groups=groups,
  3492. timezone='Europe/Paris',
  3493. do_save=True,
  3494. do_notify=False,
  3495. )
  3496. uapi.save(test_user2)
  3497. uapi.save(test_user)
  3498. transaction.commit()
  3499. user_id = int(test_user.user_id)
  3500. self.testapp.authorization = (
  3501. 'Basic',
  3502. (
  3503. 'test@test.test',
  3504. 'pass'
  3505. )
  3506. )
  3507. # Set password
  3508. params = {
  3509. 'email': 'mysuperemail@email.fr',
  3510. 'loggedin_user_password': 'test2@test2.test2',
  3511. }
  3512. self.testapp.put_json(
  3513. '/api/v2/users/{}/email'.format(user_id),
  3514. params=params,
  3515. status=403,
  3516. )
  3517. class TestSetPasswordEndpoint(FunctionalTest):
  3518. # -*- coding: utf-8 -*-
  3519. """
  3520. Tests for PUT /api/v2/users/{user_id}/password
  3521. """
  3522. fixtures = [BaseFixture]
  3523. def test_api__set_user_password__ok_200__admin(self):
  3524. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3525. admin = dbsession.query(models.User) \
  3526. .filter(models.User.email == 'admin@admin.admin') \
  3527. .one()
  3528. uapi = UserApi(
  3529. current_user=admin,
  3530. session=dbsession,
  3531. config=self.app_config,
  3532. )
  3533. gapi = GroupApi(
  3534. current_user=admin,
  3535. session=dbsession,
  3536. config=self.app_config,
  3537. )
  3538. groups = [gapi.get_one_with_name('users')]
  3539. test_user = uapi.create_user(
  3540. email='test@test.test',
  3541. password='pass',
  3542. name='bob',
  3543. groups=groups,
  3544. timezone='Europe/Paris',
  3545. do_save=True,
  3546. do_notify=False,
  3547. )
  3548. uapi.save(test_user)
  3549. transaction.commit()
  3550. user_id = int(test_user.user_id)
  3551. self.testapp.authorization = (
  3552. 'Basic',
  3553. (
  3554. 'admin@admin.admin',
  3555. 'admin@admin.admin'
  3556. )
  3557. )
  3558. # check before
  3559. user = uapi.get_one(user_id)
  3560. assert user.validate_password('pass')
  3561. assert not user.validate_password('mynewpassword')
  3562. # Set password
  3563. params = {
  3564. 'new_password': 'mynewpassword',
  3565. 'new_password2': 'mynewpassword',
  3566. 'loggedin_user_password': 'admin@admin.admin',
  3567. }
  3568. self.testapp.put_json(
  3569. '/api/v2/users/{}/password'.format(user_id),
  3570. params=params,
  3571. status=204,
  3572. )
  3573. # Check After
  3574. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3575. uapi = UserApi(
  3576. current_user=admin,
  3577. session=dbsession,
  3578. config=self.app_config,
  3579. )
  3580. user = uapi.get_one(user_id)
  3581. assert not user.validate_password('pass')
  3582. assert user.validate_password('mynewpassword')
  3583. def test_api__set_user_password__err_403__admin_wrong_password(self):
  3584. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3585. admin = dbsession.query(models.User) \
  3586. .filter(models.User.email == 'admin@admin.admin') \
  3587. .one()
  3588. uapi = UserApi(
  3589. current_user=admin,
  3590. session=dbsession,
  3591. config=self.app_config,
  3592. )
  3593. gapi = GroupApi(
  3594. current_user=admin,
  3595. session=dbsession,
  3596. config=self.app_config,
  3597. )
  3598. groups = [gapi.get_one_with_name('users')]
  3599. test_user = uapi.create_user(
  3600. email='test@test.test',
  3601. password='pass',
  3602. name='bob',
  3603. groups=groups,
  3604. timezone='Europe/Paris',
  3605. do_save=True,
  3606. do_notify=False,
  3607. )
  3608. uapi.save(test_user)
  3609. transaction.commit()
  3610. user_id = int(test_user.user_id)
  3611. self.testapp.authorization = (
  3612. 'Basic',
  3613. (
  3614. 'admin@admin.admin',
  3615. 'admin@admin.admin'
  3616. )
  3617. )
  3618. # check before
  3619. user = uapi.get_one(user_id)
  3620. assert user.validate_password('pass')
  3621. assert not user.validate_password('mynewpassword')
  3622. # Set password
  3623. params = {
  3624. 'new_password': 'mynewpassword',
  3625. 'new_password2': 'mynewpassword',
  3626. 'loggedin_user_password': 'wrongpassword',
  3627. }
  3628. self.testapp.put_json(
  3629. '/api/v2/users/{}/password'.format(user_id),
  3630. params=params,
  3631. status=403,
  3632. )
  3633. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3634. uapi = UserApi(
  3635. current_user=admin,
  3636. session=dbsession,
  3637. config=self.app_config,
  3638. )
  3639. # Check After
  3640. user = uapi.get_one(user_id)
  3641. assert user.validate_password('pass')
  3642. assert not user.validate_password('mynewpassword')
  3643. def test_api__set_user_password__err_400__admin_passwords_do_not_match(self): # nopep8
  3644. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3645. admin = dbsession.query(models.User) \
  3646. .filter(models.User.email == 'admin@admin.admin') \
  3647. .one()
  3648. uapi = UserApi(
  3649. current_user=admin,
  3650. session=dbsession,
  3651. config=self.app_config,
  3652. )
  3653. gapi = GroupApi(
  3654. current_user=admin,
  3655. session=dbsession,
  3656. config=self.app_config,
  3657. )
  3658. groups = [gapi.get_one_with_name('users')]
  3659. test_user = uapi.create_user(
  3660. email='test@test.test',
  3661. password='pass',
  3662. name='bob',
  3663. groups=groups,
  3664. timezone='Europe/Paris',
  3665. do_save=True,
  3666. do_notify=False,
  3667. )
  3668. uapi.save(test_user)
  3669. transaction.commit()
  3670. user_id = int(test_user.user_id)
  3671. self.testapp.authorization = (
  3672. 'Basic',
  3673. (
  3674. 'admin@admin.admin',
  3675. 'admin@admin.admin'
  3676. )
  3677. )
  3678. # check before
  3679. user = uapi.get_one(user_id)
  3680. assert user.validate_password('pass')
  3681. assert not user.validate_password('mynewpassword')
  3682. assert not user.validate_password('mynewpassword2')
  3683. # Set password
  3684. params = {
  3685. 'new_password': 'mynewpassword',
  3686. 'new_password2': 'mynewpassword2',
  3687. 'loggedin_user_password': 'admin@admin.admin',
  3688. }
  3689. self.testapp.put_json(
  3690. '/api/v2/users/{}/password'.format(user_id),
  3691. params=params,
  3692. status=400,
  3693. )
  3694. # Check After
  3695. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3696. uapi = UserApi(
  3697. current_user=admin,
  3698. session=dbsession,
  3699. config=self.app_config,
  3700. )
  3701. user = uapi.get_one(user_id)
  3702. assert user.validate_password('pass')
  3703. assert not user.validate_password('mynewpassword')
  3704. assert not user.validate_password('mynewpassword2')
  3705. def test_api__set_user_password__ok_200__user_itself(self):
  3706. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3707. admin = dbsession.query(models.User) \
  3708. .filter(models.User.email == 'admin@admin.admin') \
  3709. .one()
  3710. uapi = UserApi(
  3711. current_user=admin,
  3712. session=dbsession,
  3713. config=self.app_config,
  3714. )
  3715. gapi = GroupApi(
  3716. current_user=admin,
  3717. session=dbsession,
  3718. config=self.app_config,
  3719. )
  3720. groups = [gapi.get_one_with_name('users')]
  3721. test_user = uapi.create_user(
  3722. email='test@test.test',
  3723. password='pass',
  3724. name='bob',
  3725. groups=groups,
  3726. timezone='Europe/Paris',
  3727. do_save=True,
  3728. do_notify=False,
  3729. )
  3730. uapi.save(test_user)
  3731. transaction.commit()
  3732. user_id = int(test_user.user_id)
  3733. self.testapp.authorization = (
  3734. 'Basic',
  3735. (
  3736. 'test@test.test',
  3737. 'pass'
  3738. )
  3739. )
  3740. # check before
  3741. user = uapi.get_one(user_id)
  3742. assert user.validate_password('pass')
  3743. assert not user.validate_password('mynewpassword')
  3744. # Set password
  3745. params = {
  3746. 'new_password': 'mynewpassword',
  3747. 'new_password2': 'mynewpassword',
  3748. 'loggedin_user_password': 'pass',
  3749. }
  3750. self.testapp.put_json(
  3751. '/api/v2/users/{}/password'.format(user_id),
  3752. params=params,
  3753. status=204,
  3754. )
  3755. # Check After
  3756. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3757. uapi = UserApi(
  3758. current_user=admin,
  3759. session=dbsession,
  3760. config=self.app_config,
  3761. )
  3762. user = uapi.get_one(user_id)
  3763. assert not user.validate_password('pass')
  3764. assert user.validate_password('mynewpassword')
  3765. def test_api__set_user_email__err_403__other_normal_user(self):
  3766. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3767. admin = dbsession.query(models.User) \
  3768. .filter(models.User.email == 'admin@admin.admin') \
  3769. .one()
  3770. uapi = UserApi(
  3771. current_user=admin,
  3772. session=dbsession,
  3773. config=self.app_config,
  3774. )
  3775. gapi = GroupApi(
  3776. current_user=admin,
  3777. session=dbsession,
  3778. config=self.app_config,
  3779. )
  3780. groups = [gapi.get_one_with_name('users')]
  3781. test_user = uapi.create_user(
  3782. email='test@test.test',
  3783. password='pass',
  3784. name='bob',
  3785. groups=groups,
  3786. timezone='Europe/Paris',
  3787. do_save=True,
  3788. do_notify=False,
  3789. )
  3790. test_user2 = uapi.create_user(
  3791. email='test2@test2.test2',
  3792. password='pass',
  3793. name='bob2',
  3794. groups=groups,
  3795. timezone='Europe/Paris',
  3796. do_save=True,
  3797. do_notify=False,
  3798. )
  3799. uapi.save(test_user2)
  3800. uapi.save(test_user)
  3801. transaction.commit()
  3802. user_id = int(test_user.user_id)
  3803. self.testapp.authorization = (
  3804. 'Basic',
  3805. (
  3806. 'test@test.test',
  3807. 'pass'
  3808. )
  3809. )
  3810. # Set password
  3811. params = {
  3812. 'email': 'mysuperemail@email.fr',
  3813. 'loggedin_user_password': 'test2@test2.test2',
  3814. }
  3815. self.testapp.put_json(
  3816. '/api/v2/users/{}/email'.format(user_id),
  3817. params=params,
  3818. status=403,
  3819. )
  3820. class TestSetUserInfoEndpoint(FunctionalTest):
  3821. # -*- coding: utf-8 -*-
  3822. """
  3823. Tests for PUT /api/v2/users/{user_id}
  3824. """
  3825. fixtures = [BaseFixture]
  3826. def test_api__set_user_info__ok_200__admin(self):
  3827. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3828. admin = dbsession.query(models.User) \
  3829. .filter(models.User.email == 'admin@admin.admin') \
  3830. .one()
  3831. uapi = UserApi(
  3832. current_user=admin,
  3833. session=dbsession,
  3834. config=self.app_config,
  3835. )
  3836. gapi = GroupApi(
  3837. current_user=admin,
  3838. session=dbsession,
  3839. config=self.app_config,
  3840. )
  3841. groups = [gapi.get_one_with_name('users')]
  3842. test_user = uapi.create_user(
  3843. email='test@test.test',
  3844. password='pass',
  3845. name='bob',
  3846. groups=groups,
  3847. timezone='Europe/Paris',
  3848. do_save=True,
  3849. do_notify=False,
  3850. )
  3851. uapi.save(test_user)
  3852. transaction.commit()
  3853. user_id = int(test_user.user_id)
  3854. self.testapp.authorization = (
  3855. 'Basic',
  3856. (
  3857. 'admin@admin.admin',
  3858. 'admin@admin.admin'
  3859. )
  3860. )
  3861. # check before
  3862. res = self.testapp.get(
  3863. '/api/v2/users/{}'.format(user_id),
  3864. status=200
  3865. )
  3866. res = res.json_body
  3867. assert res['user_id'] == user_id
  3868. assert res['public_name'] == 'bob'
  3869. assert res['timezone'] == 'Europe/Paris'
  3870. # Set params
  3871. params = {
  3872. 'public_name': 'updated',
  3873. 'timezone': 'Europe/London',
  3874. }
  3875. self.testapp.put_json(
  3876. '/api/v2/users/{}'.format(user_id),
  3877. params=params,
  3878. status=200,
  3879. )
  3880. # Check After
  3881. res = self.testapp.get(
  3882. '/api/v2/users/{}'.format(user_id),
  3883. status=200
  3884. )
  3885. res = res.json_body
  3886. assert res['user_id'] == user_id
  3887. assert res['public_name'] == 'updated'
  3888. assert res['timezone'] == 'Europe/London'
  3889. def test_api__set_user_info__ok_200__user_itself(self):
  3890. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3891. admin = dbsession.query(models.User) \
  3892. .filter(models.User.email == 'admin@admin.admin') \
  3893. .one()
  3894. uapi = UserApi(
  3895. current_user=admin,
  3896. session=dbsession,
  3897. config=self.app_config,
  3898. )
  3899. gapi = GroupApi(
  3900. current_user=admin,
  3901. session=dbsession,
  3902. config=self.app_config,
  3903. )
  3904. groups = [gapi.get_one_with_name('users')]
  3905. test_user = uapi.create_user(
  3906. email='test@test.test',
  3907. password='pass',
  3908. name='bob',
  3909. groups=groups,
  3910. timezone='Europe/Paris',
  3911. do_save=True,
  3912. do_notify=False,
  3913. )
  3914. uapi.save(test_user)
  3915. transaction.commit()
  3916. user_id = int(test_user.user_id)
  3917. self.testapp.authorization = (
  3918. 'Basic',
  3919. (
  3920. 'test@test.test',
  3921. 'pass',
  3922. )
  3923. )
  3924. # check before
  3925. res = self.testapp.get(
  3926. '/api/v2/users/{}'.format(user_id),
  3927. status=200
  3928. )
  3929. res = res.json_body
  3930. assert res['user_id'] == user_id
  3931. assert res['public_name'] == 'bob'
  3932. assert res['timezone'] == 'Europe/Paris'
  3933. # Set params
  3934. params = {
  3935. 'public_name': 'updated',
  3936. 'timezone': 'Europe/London',
  3937. }
  3938. self.testapp.put_json(
  3939. '/api/v2/users/{}'.format(user_id),
  3940. params=params,
  3941. status=200,
  3942. )
  3943. # Check After
  3944. res = self.testapp.get(
  3945. '/api/v2/users/{}'.format(user_id),
  3946. status=200
  3947. )
  3948. res = res.json_body
  3949. assert res['user_id'] == user_id
  3950. assert res['public_name'] == 'updated'
  3951. assert res['timezone'] == 'Europe/London'
  3952. def test_api__set_user_email__err_403__other_normal_user(self):
  3953. dbsession = get_tm_session(self.session_factory, transaction.manager)
  3954. admin = dbsession.query(models.User) \
  3955. .filter(models.User.email == 'admin@admin.admin') \
  3956. .one()
  3957. uapi = UserApi(
  3958. current_user=admin,
  3959. session=dbsession,
  3960. config=self.app_config,
  3961. )
  3962. gapi = GroupApi(
  3963. current_user=admin,
  3964. session=dbsession,
  3965. config=self.app_config,
  3966. )
  3967. groups = [gapi.get_one_with_name('users')]
  3968. test_user = uapi.create_user(
  3969. email='test@test.test',
  3970. password='pass',
  3971. name='bob',
  3972. groups=groups,
  3973. timezone='Europe/Paris',
  3974. do_save=True,
  3975. do_notify=False,
  3976. )
  3977. test_user2 = uapi.create_user(
  3978. email='test2@test2.test2',
  3979. password='pass',
  3980. name='test',
  3981. groups=groups,
  3982. timezone='Europe/Paris',
  3983. do_save=True,
  3984. do_notify=False,
  3985. )
  3986. uapi.save(test_user2)
  3987. uapi.save(test_user)
  3988. transaction.commit()
  3989. user_id = int(test_user.user_id)
  3990. self.testapp.authorization = (
  3991. 'Basic',
  3992. (
  3993. 'test2@test2.test2',
  3994. 'pass',
  3995. )
  3996. )
  3997. # Set params
  3998. params = {
  3999. 'public_name': 'updated',
  4000. 'timezone': 'Europe/London',
  4001. }
  4002. self.testapp.put_json(
  4003. '/api/v2/users/{}'.format(user_id),
  4004. params=params,
  4005. status=403,
  4006. )
  4007. class TestSetUserProfilEndpoint(FunctionalTest):
  4008. # -*- coding: utf-8 -*-
  4009. """
  4010. Tests for PUT /api/v2/users/{user_id}/profile
  4011. """
  4012. fixtures = [BaseFixture]
  4013. def test_api__set_user_info__ok_200__admin(self):
  4014. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4015. admin = dbsession.query(models.User) \
  4016. .filter(models.User.email == 'admin@admin.admin') \
  4017. .one()
  4018. uapi = UserApi(
  4019. current_user=admin,
  4020. session=dbsession,
  4021. config=self.app_config,
  4022. )
  4023. gapi = GroupApi(
  4024. current_user=admin,
  4025. session=dbsession,
  4026. config=self.app_config,
  4027. )
  4028. groups = [gapi.get_one_with_name('users')]
  4029. test_user = uapi.create_user(
  4030. email='test@test.test',
  4031. password='pass',
  4032. name='bob',
  4033. groups=groups,
  4034. timezone='Europe/Paris',
  4035. do_save=True,
  4036. do_notify=False,
  4037. )
  4038. uapi.save(test_user)
  4039. transaction.commit()
  4040. user_id = int(test_user.user_id)
  4041. self.testapp.authorization = (
  4042. 'Basic',
  4043. (
  4044. 'admin@admin.admin',
  4045. 'admin@admin.admin'
  4046. )
  4047. )
  4048. # check before
  4049. res = self.testapp.get(
  4050. '/api/v2/users/{}'.format(user_id),
  4051. status=200
  4052. )
  4053. res = res.json_body
  4054. assert res['user_id'] == user_id
  4055. assert res['profile'] == 'users'
  4056. # Set params
  4057. params = {
  4058. 'profile': 'administrators',
  4059. }
  4060. self.testapp.put_json(
  4061. '/api/v2/users/{}/profile'.format(user_id),
  4062. params=params,
  4063. status=204,
  4064. )
  4065. # Check After
  4066. res = self.testapp.get(
  4067. '/api/v2/users/{}'.format(user_id),
  4068. status=200
  4069. )
  4070. res = res.json_body
  4071. assert res['user_id'] == user_id
  4072. assert res['profile'] == 'administrators'
  4073. def test_api__set_user_info__err_403__user_itself(self):
  4074. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4075. admin = dbsession.query(models.User) \
  4076. .filter(models.User.email == 'admin@admin.admin') \
  4077. .one()
  4078. uapi = UserApi(
  4079. current_user=admin,
  4080. session=dbsession,
  4081. config=self.app_config,
  4082. )
  4083. gapi = GroupApi(
  4084. current_user=admin,
  4085. session=dbsession,
  4086. config=self.app_config,
  4087. )
  4088. groups = [gapi.get_one_with_name('users')]
  4089. test_user = uapi.create_user(
  4090. email='test@test.test',
  4091. password='pass',
  4092. name='bob',
  4093. groups=groups,
  4094. timezone='Europe/Paris',
  4095. do_save=True,
  4096. do_notify=False,
  4097. )
  4098. uapi.save(test_user)
  4099. transaction.commit()
  4100. user_id = int(test_user.user_id)
  4101. self.testapp.authorization = (
  4102. 'Basic',
  4103. (
  4104. 'test@test.test',
  4105. 'pass',
  4106. )
  4107. )
  4108. # check before
  4109. res = self.testapp.get(
  4110. '/api/v2/users/{}'.format(user_id),
  4111. status=200
  4112. )
  4113. res = res.json_body
  4114. assert res['user_id'] == user_id
  4115. assert res['profile'] == 'users'
  4116. # Set params
  4117. params = {
  4118. 'profile': 'administrators',
  4119. }
  4120. self.testapp.put_json(
  4121. '/api/v2/users/{}/profile'.format(user_id),
  4122. params=params,
  4123. status=403,
  4124. )
  4125. # Check After
  4126. res = self.testapp.get(
  4127. '/api/v2/users/{}'.format(user_id),
  4128. status=200
  4129. )
  4130. res = res.json_body
  4131. assert res['user_id'] == user_id
  4132. assert res['profile'] == 'users'
  4133. def test_api__set_user_email__err_403__other_normal_user(self):
  4134. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4135. admin = dbsession.query(models.User) \
  4136. .filter(models.User.email == 'admin@admin.admin') \
  4137. .one()
  4138. uapi = UserApi(
  4139. current_user=admin,
  4140. session=dbsession,
  4141. config=self.app_config,
  4142. )
  4143. gapi = GroupApi(
  4144. current_user=admin,
  4145. session=dbsession,
  4146. config=self.app_config,
  4147. )
  4148. groups = [gapi.get_one_with_name('users')]
  4149. test_user = uapi.create_user(
  4150. email='test@test.test',
  4151. password='pass',
  4152. name='bob',
  4153. groups=groups,
  4154. timezone='Europe/Paris',
  4155. do_save=True,
  4156. do_notify=False,
  4157. )
  4158. test_user2 = uapi.create_user(
  4159. email='test2@test2.test2',
  4160. password='pass',
  4161. name='test',
  4162. groups=groups,
  4163. timezone='Europe/Paris',
  4164. do_save=True,
  4165. do_notify=False,
  4166. )
  4167. uapi.save(test_user2)
  4168. uapi.save(test_user)
  4169. transaction.commit()
  4170. user_id = int(test_user.user_id)
  4171. self.testapp.authorization = (
  4172. 'Basic',
  4173. (
  4174. 'test2@test2.test2',
  4175. 'pass',
  4176. )
  4177. )
  4178. # Set params
  4179. params = {
  4180. 'profile': 'administrators',
  4181. }
  4182. self.testapp.put_json(
  4183. '/api/v2/users/{}/profile'.format(user_id),
  4184. params=params,
  4185. status=403,
  4186. )
  4187. class TestSetUserEnableDisableEndpoints(FunctionalTest):
  4188. # -*- coding: utf-8 -*-
  4189. """
  4190. Tests for PUT /api/v2/users/{user_id}/enable
  4191. and PUT /api/v2/users/{user_id}/disable
  4192. """
  4193. fixtures = [BaseFixture]
  4194. def test_api_enable_user__ok_200__admin(self):
  4195. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4196. admin = dbsession.query(models.User) \
  4197. .filter(models.User.email == 'admin@admin.admin') \
  4198. .one()
  4199. uapi = UserApi(
  4200. current_user=admin,
  4201. session=dbsession,
  4202. config=self.app_config,
  4203. )
  4204. gapi = GroupApi(
  4205. current_user=admin,
  4206. session=dbsession,
  4207. config=self.app_config,
  4208. )
  4209. groups = [gapi.get_one_with_name('users')]
  4210. test_user = uapi.create_user(
  4211. email='test@test.test',
  4212. password='pass',
  4213. name='bob',
  4214. groups=groups,
  4215. timezone='Europe/Paris',
  4216. do_save=True,
  4217. do_notify=False,
  4218. )
  4219. uapi.disable(test_user, do_save=True)
  4220. uapi.save(test_user)
  4221. transaction.commit()
  4222. user_id = int(test_user.user_id)
  4223. self.testapp.authorization = (
  4224. 'Basic',
  4225. (
  4226. 'admin@admin.admin',
  4227. 'admin@admin.admin'
  4228. )
  4229. )
  4230. # check before
  4231. res = self.testapp.get(
  4232. '/api/v2/users/{}'.format(user_id),
  4233. status=200
  4234. )
  4235. res = res.json_body
  4236. assert res['user_id'] == user_id
  4237. assert res['is_active'] is False
  4238. self.testapp.put_json(
  4239. '/api/v2/users/{}/enable'.format(user_id),
  4240. status=204,
  4241. )
  4242. # Check After
  4243. res = self.testapp.get(
  4244. '/api/v2/users/{}'.format(user_id),
  4245. status=200
  4246. )
  4247. res = res.json_body
  4248. assert res['user_id'] == user_id
  4249. assert res['is_active'] is True
  4250. def test_api_disable_user__ok_200__admin(self):
  4251. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4252. admin = dbsession.query(models.User) \
  4253. .filter(models.User.email == 'admin@admin.admin') \
  4254. .one()
  4255. uapi = UserApi(
  4256. current_user=admin,
  4257. session=dbsession,
  4258. config=self.app_config,
  4259. )
  4260. gapi = GroupApi(
  4261. current_user=admin,
  4262. session=dbsession,
  4263. config=self.app_config,
  4264. )
  4265. groups = [gapi.get_one_with_name('users')]
  4266. test_user = uapi.create_user(
  4267. email='test@test.test',
  4268. password='pass',
  4269. name='bob',
  4270. groups=groups,
  4271. timezone='Europe/Paris',
  4272. do_save=True,
  4273. do_notify=False,
  4274. )
  4275. uapi.enable(test_user, do_save=True)
  4276. uapi.save(test_user)
  4277. transaction.commit()
  4278. user_id = int(test_user.user_id)
  4279. self.testapp.authorization = (
  4280. 'Basic',
  4281. (
  4282. 'admin@admin.admin',
  4283. 'admin@admin.admin'
  4284. )
  4285. )
  4286. # check before
  4287. res = self.testapp.get(
  4288. '/api/v2/users/{}'.format(user_id),
  4289. status=200
  4290. )
  4291. res = res.json_body
  4292. assert res['user_id'] == user_id
  4293. assert res['is_active'] is True
  4294. self.testapp.put_json(
  4295. '/api/v2/users/{}/disable'.format(user_id),
  4296. status=204,
  4297. )
  4298. # Check After
  4299. res = self.testapp.get(
  4300. '/api/v2/users/{}'.format(user_id),
  4301. status=200
  4302. )
  4303. res = res.json_body
  4304. assert res['user_id'] == user_id
  4305. assert res['is_active'] is False
  4306. def test_api_enable_user__err_403__other_account(self):
  4307. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4308. admin = dbsession.query(models.User) \
  4309. .filter(models.User.email == 'admin@admin.admin') \
  4310. .one()
  4311. uapi = UserApi(
  4312. current_user=admin,
  4313. session=dbsession,
  4314. config=self.app_config,
  4315. )
  4316. gapi = GroupApi(
  4317. current_user=admin,
  4318. session=dbsession,
  4319. config=self.app_config,
  4320. )
  4321. groups = [gapi.get_one_with_name('users')]
  4322. test_user = uapi.create_user(
  4323. email='test@test.test',
  4324. password='pass',
  4325. name='bob',
  4326. groups=groups,
  4327. timezone='Europe/Paris',
  4328. do_save=True,
  4329. do_notify=False,
  4330. )
  4331. test_user2 = uapi.create_user(
  4332. email='test2@test2.test2',
  4333. password='pass',
  4334. name='test2',
  4335. groups=groups,
  4336. timezone='Europe/Paris',
  4337. do_save=True,
  4338. do_notify=False,
  4339. )
  4340. uapi.disable(test_user, do_save=True)
  4341. uapi.save(test_user2)
  4342. uapi.save(test_user)
  4343. transaction.commit()
  4344. user_id = int(test_user.user_id)
  4345. self.testapp.authorization = (
  4346. 'Basic',
  4347. (
  4348. 'test2@test2.test2',
  4349. 'pass'
  4350. )
  4351. )
  4352. self.testapp.put_json(
  4353. '/api/v2/users/{}/enable'.format(user_id),
  4354. status=403,
  4355. )
  4356. def test_api_disable_user__err_403__other_account(self):
  4357. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4358. admin = dbsession.query(models.User) \
  4359. .filter(models.User.email == 'admin@admin.admin') \
  4360. .one()
  4361. uapi = UserApi(
  4362. current_user=admin,
  4363. session=dbsession,
  4364. config=self.app_config,
  4365. )
  4366. gapi = GroupApi(
  4367. current_user=admin,
  4368. session=dbsession,
  4369. config=self.app_config,
  4370. )
  4371. groups = [gapi.get_one_with_name('users')]
  4372. test_user = uapi.create_user(
  4373. email='test@test.test',
  4374. password='pass',
  4375. name='bob',
  4376. groups=groups,
  4377. timezone='Europe/Paris',
  4378. do_save=True,
  4379. do_notify=False,
  4380. )
  4381. test_user2 = uapi.create_user(
  4382. email='test2@test2.test2',
  4383. password='pass',
  4384. name='test2',
  4385. groups=groups,
  4386. timezone='Europe/Paris',
  4387. do_save=True,
  4388. do_notify=False,
  4389. )
  4390. uapi.enable(test_user, do_save=True)
  4391. uapi.save(test_user2)
  4392. uapi.save(test_user)
  4393. transaction.commit()
  4394. user_id = int(test_user.user_id)
  4395. self.testapp.authorization = (
  4396. 'Basic',
  4397. (
  4398. 'test2@test2.test2',
  4399. 'pass'
  4400. )
  4401. )
  4402. self.testapp.put_json(
  4403. '/api/v2/users/{}/disable'.format(user_id),
  4404. status=403,
  4405. )
  4406. def test_api_disable_user__ok_200__user_itself(self):
  4407. dbsession = get_tm_session(self.session_factory, transaction.manager)
  4408. admin = dbsession.query(models.User) \
  4409. .filter(models.User.email == 'admin@admin.admin') \
  4410. .one()
  4411. uapi = UserApi(
  4412. current_user=admin,
  4413. session=dbsession,
  4414. config=self.app_config,
  4415. )
  4416. gapi = GroupApi(
  4417. current_user=admin,
  4418. session=dbsession,
  4419. config=self.app_config,
  4420. )
  4421. groups = [gapi.get_one_with_name('users')]
  4422. test_user = uapi.create_user(
  4423. email='test@test.test',
  4424. password='pass',
  4425. name='bob',
  4426. groups=groups,
  4427. timezone='Europe/Paris',
  4428. do_save=True,
  4429. do_notify=False,
  4430. )
  4431. uapi.enable(test_user, do_save=True)
  4432. uapi.save(test_user)
  4433. transaction.commit()
  4434. user_id = int(test_user.user_id)
  4435. self.testapp.authorization = (
  4436. 'Basic',
  4437. (
  4438. 'test@test.test',
  4439. 'pass'
  4440. )
  4441. )
  4442. # check before
  4443. res = self.testapp.get(
  4444. '/api/v2/users/{}'.format(user_id),
  4445. status=200
  4446. )
  4447. res = res.json_body
  4448. assert res['user_id'] == user_id
  4449. assert res['is_active'] is True
  4450. self.testapp.put_json(
  4451. '/api/v2/users/{}/disable'.format(user_id),
  4452. status=403,
  4453. )
  4454. # Check After
  4455. res = self.testapp.get(
  4456. '/api/v2/users/{}'.format(user_id),
  4457. status=200
  4458. )
  4459. res = res.json_body
  4460. assert res['user_id'] == user_id
  4461. assert res['is_active'] is True