test_user.py 150KB

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