test_user.py 149KB

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