test_user.py 165KB

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