test_user.py 164KB

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