test_user.py 138KB

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