tests.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. # -*- coding: utf-8 -*-
  2. """
  3. Run me with:
  4. python manage.py test reader
  5. """
  6. import sys
  7. # Tells sefaria.system.database to use a test db
  8. sys._called_from_test = True
  9. from copy import deepcopy
  10. from pprint import pprint
  11. import json
  12. from django.test import TestCase
  13. from django.test.client import Client
  14. from django.contrib.auth.models import User
  15. # import selenium
  16. import sefaria.utils.testing_utils as tutils
  17. from sefaria.model import library, Index, IndexSet, VersionSet, LinkSet, NoteSet, HistorySet, Ref, VersionState, \
  18. VersionStateSet, TextChunk, Category, UserHistory, UserHistorySet
  19. from sefaria.system.database import db
  20. import sefaria.system.cache as scache
  21. c = Client()
  22. class SefariaTestCase(TestCase):
  23. def make_test_user(self):
  24. user = User.objects.create_user(username="test@sefaria.org", email='test@sefaria.org', password='!!!')
  25. user.set_password('!!!')
  26. user.first_name = "Test"
  27. user.last_name = "Testerberg"
  28. user.is_staff = True
  29. user.save()
  30. c.login(email="test@sefaria.org", password="!!!")
  31. def in_cache(self, title):
  32. self.assertTrue(title in library.full_title_list())
  33. self.assertTrue(title in json.loads(library.get_text_titles_json()))
  34. def not_in_cache(self, title):
  35. self.assertFalse(title in library._index_map)
  36. self.assertTrue(title not in library.full_title_list())
  37. self.assertTrue(title not in json.loads(library.get_text_titles_json()))
  38. self.assertFalse(any(key.startswith(title) for key, value in Ref._raw_cache().iteritems()))
  39. class PagesTest(SefariaTestCase):
  40. """
  41. Tests that an assortment of important pages can load without error.
  42. """
  43. def test_root(self):
  44. response = c.get('/')
  45. self.assertEqual(200, response.status_code)
  46. def test_activity(self):
  47. response = c.get('/activity')
  48. self.assertEqual(200, response.status_code)
  49. def test_text_history(self):
  50. response = c.get('/activity/Genesis_12/en/The_Holy_Scriptures:_A_New_Translation_(JPS_1917)')
  51. self.assertEqual(200, response.status_code)
  52. def test_toc(self):
  53. response = c.get('/texts')
  54. self.assertEqual(200, response.status_code)
  55. def test_dashboard(self):
  56. response = c.get('/dashboard')
  57. self.assertEqual(200, response.status_code)
  58. def test_get_text_tanakh(self):
  59. response = c.get('/Genesis.1')
  60. self.assertEqual(200, response.status_code)
  61. def test_get_text_talmud(self):
  62. response = c.get('/Shabbat.32a')
  63. self.assertEqual(200, response.status_code)
  64. def test_get_text_tanakh_commentary(self):
  65. response = c.get('/Rashi_on_Genesis.2.3')
  66. self.assertEqual(200, response.status_code)
  67. def test_get_text_talmud_commentary(self):
  68. response = c.get('/Tosafot_on_Sukkah.2a.1.1')
  69. self.assertEqual(200, response.status_code)
  70. def test_get_tanakh_toc(self):
  71. response = c.get('/Genesis')
  72. self.assertEqual(200, response.status_code)
  73. def test_get_talmud_toc(self):
  74. response = c.get('/Shabbat')
  75. self.assertEqual(200, response.status_code)
  76. def test_get_tanakh_commentary_toc(self):
  77. response = c.get('/Rashi_on_Genesis')
  78. self.assertEqual(200, response.status_code)
  79. def test_get_talmud_commentary_toc(self):
  80. response = c.get('/Tosafot_on_Sukkah')
  81. self.assertEqual(200, response.status_code)
  82. def test_get_text_unknown(self):
  83. response = c.get('/Gibbledeegoobledeemoop')
  84. self.assertEqual(404, response.status_code)
  85. def test_sheets_splash(self):
  86. response = c.get('/sheets')
  87. self.assertEqual(200, response.status_code)
  88. def test_new_sheet(self):
  89. response = c.get('/sheets/new')
  90. self.assertEqual(200, response.status_code)
  91. def test_new_sheet(self):
  92. response = c.get('/sheets/tags')
  93. self.assertEqual(200, response.status_code)
  94. def test_profile(self):
  95. response = c.get('/profile/brett-lockspeiser')
  96. self.assertEqual(200, response.status_code)
  97. def test_campaign(self):
  98. response = c.get('/translate/Bereishit_Rabbah')
  99. self.assertEqual(200, response.status_code)
  100. def test_explorer(self):
  101. response = c.get('/explore')
  102. self.assertEqual(200, response.status_code)
  103. def test_discussions(self):
  104. response = c.get('/discussions')
  105. self.assertEqual(200, response.status_code)
  106. def test_translation_requests(self):
  107. response = c.get('/translation-requests')
  108. self.assertEqual(200, response.status_code)
  109. def test_login(self):
  110. response = c.post('/login/', {'username': 'john', 'password': 'smith'})
  111. self.assertEqual(200, response.status_code)
  112. class ApiTest(SefariaTestCase):
  113. """
  114. Test data returned from GET calls to various APIs.
  115. """
  116. def setUp(self):
  117. pass
  118. def test_api_get_text_tanakh(self):
  119. response = c.get('/api/texts/Genesis.1')
  120. self.assertEqual(200, response.status_code)
  121. data = json.loads(response.content)
  122. self.assertTrue(len(data["text"]) > 0)
  123. self.assertTrue(len(data["he"]) > 0)
  124. self.assertTrue(len(data["commentary"]) > 0)
  125. self.assertEqual(data["book"], "Genesis")
  126. self.assertEqual(data["categories"], ["Tanakh", "Torah"])
  127. self.assertEqual(data["sections"], [1])
  128. self.assertEqual(data["toSections"], [1])
  129. def test_api_get_text_talmud(self):
  130. response = c.get('/api/texts/Shabbat.22a')
  131. self.assertEqual(200, response.status_code)
  132. data = json.loads(response.content)
  133. self.assertTrue(len(data["text"]) > 0)
  134. self.assertTrue(len(data["he"]) > 0)
  135. self.assertTrue(len(data["commentary"]) > 0)
  136. self.assertEqual(data["book"], "Shabbat")
  137. self.assertEqual(data["categories"], ["Talmud", "Bavli", "Seder Moed"])
  138. self.assertEqual(data["sections"], ["22a"])
  139. self.assertEqual(data["toSections"], ["22a"])
  140. def test_api_get_text_tanakh_commentary(self):
  141. response = c.get('/api/texts/Rashi_on_Genesis.2.3')
  142. self.assertEqual(200, response.status_code)
  143. data = json.loads(response.content)
  144. self.assertTrue(len(data["he"]) > 0)
  145. self.assertTrue(len(data["commentary"]) > 0)
  146. self.assertEqual(data["book"], "Rashi on Genesis")
  147. self.assertEqual(data["collectiveTitle"], "Rashi")
  148. self.assertEqual(data["categories"], ["Tanakh", "Commentary", "Rashi", "Torah"])
  149. self.assertEqual(data["sections"], [2, 3])
  150. self.assertEqual(data["toSections"], [2, 3])
  151. def test_api_get_text_talmud_commentary(self):
  152. response = c.get('/api/texts/Tosafot_on_Sukkah.2a.4.1')
  153. self.assertEqual(200, response.status_code)
  154. data = json.loads(response.content)
  155. self.assertTrue(len(data["he"]) > 0)
  156. self.assertTrue(len(data["commentary"]) > 0)
  157. self.assertEqual(data["book"], "Tosafot on Sukkah")
  158. self.assertEqual(data["collectiveTitle"], "Tosafot")
  159. self.assertEqual(data["categories"], ["Talmud", "Bavli", "Commentary", "Tosafot", "Seder Moed"])
  160. self.assertEqual(data["sections"], ["2a", 4, 1])
  161. self.assertEqual(data["toSections"], ["2a", 4, 1])
  162. def test_api_get_text_range(self):
  163. response = c.get('/api/texts/Job.5.2-4')
  164. self.assertEqual(200, response.status_code)
  165. data = json.loads(response.content)
  166. self.assertEqual(data["sections"], [5, 2])
  167. self.assertEqual(data["toSections"], [5, 4])
  168. def test_api_get_text_bad_text(self):
  169. response = c.get('/api/texts/Protocols_of_the_Elders_of_Zion.13.13')
  170. self.assertEqual(200, response.status_code)
  171. data = json.loads(response.content)
  172. self.assertEqual(data["error"],
  173. "Failed to parse sections for ref Protocols_of_the_Elders_of_Zion.13.13") # "Unrecognized Index record: Protocols of the Elders of Zion.13.13")
  174. def test_api_get_text_out_of_bound(self):
  175. response = c.get('/api/texts/Genesis.999')
  176. self.assertEqual(200, response.status_code)
  177. data = json.loads(response.content)
  178. self.assertEqual(data["error"], "Genesis ends at Chapter 50.")
  179. def test_api_get_text_too_many_hyphens(self):
  180. response = c.get('/api/texts/Genesis.9-4-5')
  181. self.assertEqual(200, response.status_code)
  182. data = json.loads(response.content)
  183. self.assertEqual(data["error"], "Couldn't understand ref 'Genesis.9-4-5' (too many -'s).")
  184. def test_api_get_text_bad_sections(self):
  185. response = c.get('/api/texts/Job.6-X')
  186. self.assertEqual(200, response.status_code)
  187. data = json.loads(response.content)
  188. self.assertEqual(data["error"], "Couldn't understand text sections: 'Job.6-X'.")
  189. def text_api_get_index(self):
  190. response = c.get('/api/index/Job')
  191. self.assertEqual(200, response.status_code)
  192. data = json.loads(response.content)
  193. self.assertEqual(data["title"], "Job")
  194. self.assertEqual(data["sectionNames"], ["Chapter", "Verse"])
  195. self.assertEqual(data["categories"], ["Tanakh", "Writings"])
  196. def text_api_get_commentator_index(self):
  197. response = c.get('/api/index/Rashi')
  198. self.assertEqual(200, response.status_code)
  199. data = json.loads(response.content)
  200. self.assertEqual(data["error"], "No book named 'Rashi'.")
  201. def text_api_get_toc(self):
  202. response = c.get('/api/index')
  203. self.assertEqual(200, response.status_code)
  204. data = json.loads(response.content)
  205. self.assertTrue(len(data) > 10)
  206. self.assertTrue(data[0]["category"] == "Tanakh")
  207. self.assertTrue(data[0]["contents"][0]["category"] == "Torah")
  208. self.assertTrue(len(data[0]["contents"][0]["contents"]) == 5)
  209. self.assertTrue(data[-1]["category"] == "Other")
  210. def text_api_get_books(self):
  211. response = c.get('/api/index/titles/')
  212. self.assertEqual(200, response.status_code)
  213. data = json.loads(response.content)
  214. self.assertTrue(len(data["books"]) > 1000)
  215. test_names = ("Genesis", "Sukkah", "Ex.", "Mishlei", "Mishnah Peah")
  216. for name in test_names:
  217. self.assertTrue(name in data["books"])
  218. def links_api_get(self):
  219. response = c.get("/api/links/Exodus.1.12")
  220. self.assertEqual(200, response.status_code)
  221. data = json.loads(response.content)
  222. self.assertTrue(len(data) > 20)
  223. class LoginTest(SefariaTestCase):
  224. def setUp(self):
  225. self.make_test_user()
  226. def test_logged_in(self):
  227. response = c.get('/')
  228. self.assertTrue(response.content.find("Log In") == -1)
  229. class PostV2IndexTest(SefariaTestCase):
  230. def setUp(self):
  231. self.make_test_user()
  232. def tearDown(self):
  233. IndexSet({"title": "Complex Book"}).delete()
  234. def test_add_alt_struct(self):
  235. # Add a simple Index
  236. index = {
  237. "title": "Complex Book",
  238. "titleVariants": [],
  239. "heTitle": "Hebrew Complex Book",
  240. "sectionNames": ["Chapter", "Paragraph"],
  241. "categories": ["Musar"],
  242. }
  243. response = c.post("/api/index/Complex_Book", {'json': json.dumps(index)})
  244. self.assertEqual(200, response.status_code)
  245. data = json.loads(response.content)
  246. self.assertNotIn("error", data)
  247. # Get it in raw v2 form
  248. response = c.get("/api/v2/raw/index/Complex_Book")
  249. data = json.loads(response.content)
  250. self.assertNotIn("error", data)
  251. # Add some alt structs to it
  252. data["alt_structs"] = {
  253. "Special Sections": {
  254. "nodes": [
  255. {
  256. "nodeType": "ArrayMapNode",
  257. "depth": 1,
  258. "titles": [
  259. {
  260. "lang": "en",
  261. "text": "Idrah Rabbah",
  262. "primary": True
  263. },
  264. {
  265. "lang": "he",
  266. "text": u"אידרה רבה",
  267. "primary": True
  268. }
  269. ],
  270. "addressTypes": [
  271. "Integer"
  272. ],
  273. "sectionNames": [
  274. "Paragraph"
  275. ],
  276. "wholeRef": "Complex Book 3:4-7:1",
  277. "refs": [
  278. "Complex Book 3:4-4:1",
  279. "Complex Book 4:2-6:3",
  280. "Complex Book 6:4-7:1"
  281. ]
  282. }
  283. ]
  284. }
  285. }
  286. # Save
  287. response = c.post("/api/v2/raw/index/Complex_Book", {'json': json.dumps(data)})
  288. self.assertEqual(200, response.status_code)
  289. data = json.loads(response.content)
  290. self.assertNotIn("error", data)
  291. # Load and validate alt structs
  292. response = c.get("/api/v2/raw/index/Complex_Book")
  293. data = json.loads(response.content)
  294. self.assertNotIn("error", data)
  295. self.assertIn("alt_structs", data)
  296. self.assertIn("Special Sections", data["alt_structs"])
  297. class PostIndexTest(SefariaTestCase):
  298. def setUp(self):
  299. self.make_test_user()
  300. def tearDown(self):
  301. job = Index().load({"title": "Pele Yoetz"})
  302. job.nodes.title_group.titles = [variant for variant in job.nodes.title_group.titles if variant["text"] != "Boj"]
  303. job.save()
  304. IndexSet({"title": "Book of Bad Index"}).delete()
  305. IndexSet({"title": "Reb Rabbit"}).delete()
  306. IndexSet({"title": "Book of Variants"}).delete()
  307. def test_post_index_change(self):
  308. """
  309. Tests:
  310. addition of title variant to existing text
  311. that new variant shows in index/titles/cache
  312. removal of new variant
  313. that it is removed from index/titles/cache
  314. """
  315. # Post a new Title Variant to an existing Index
  316. orig = json.loads(c.get("/api/index/Pele_Yoetz").content)
  317. self.assertTrue("Boj" not in orig["titleVariants"])
  318. new = deepcopy(orig)
  319. new["titleVariants"].append("Boj")
  320. response = c.post("/api/index/Pele_Yoetz", {'json': json.dumps(new)})
  321. self.assertEqual(200, response.status_code)
  322. data = json.loads(response.content)
  323. self.assertNotIn("error", data)
  324. self.in_cache("Boj")
  325. response = c.get("/api/index/titles")
  326. data = json.loads(response.content)
  327. self.assertIn("books", data)
  328. self.assertTrue("Boj" in data["books"])
  329. # Reset this change
  330. c.post("/api/index/Pele_Yoetz", {'json': json.dumps(orig)})
  331. response = c.get("/api/index/titles")
  332. data = json.loads(response.content)
  333. self.assertTrue("Boj" not in data["books"])
  334. self.not_in_cache("Boj")
  335. def test_post_index_fields_missing(self):
  336. """
  337. Tests:
  338. Posting new index with required fields missing
  339. """
  340. index = {
  341. "title": "Book of Bad Index",
  342. "titleVariants": ["Book of Bad Index"],
  343. "sectionNames": ["Chapter", "Paragraph"],
  344. }
  345. response = c.post("/api/index/Book_of_Bad_Index", {'json': json.dumps(index)})
  346. self.assertEqual(200, response.status_code)
  347. data = json.loads(response.content)
  348. self.assertIn("error", data)
  349. index = {
  350. "title": "Book of Bad Index",
  351. "titleVariants": ["Book of Bad Index"],
  352. "categories": ["Musar"]
  353. }
  354. response = c.post("/api/index/Book_of_Bad_Index", {'json': json.dumps(index)})
  355. self.assertEqual(200, response.status_code)
  356. data = json.loads(response.content)
  357. self.assertIn("error", data)
  358. '''
  359. Needs rewrite
  360. def test_primary_title_added_to_variants(self):
  361. """
  362. Tests:
  363. Posting new index without primary title in variants,
  364. primary should be added to variants
  365. """
  366. # Post with Empty variants
  367. index = {
  368. "title": "Book of Variants",
  369. "titleVariants": [],
  370. "heTitle": u"Hebrew Book of Variants",
  371. "heTitleVariants": [],
  372. "sectionNames": ["Chapter", "Paragraph"],
  373. "categories": ["Musar"],
  374. }
  375. response = c.post("/api/index/Book_of_Variants", {'json': json.dumps(index)})
  376. self.assertEqual(200, response.status_code)
  377. data = json.loads(response.content)
  378. self.assertNotIn("error", data)
  379. self.assertIn("titleVariants", data)
  380. self.assertIn("Book of Variants", data["titleVariants"])
  381. # Post with variants field missing
  382. index = {
  383. "title": "Book of Variants",
  384. "heTitle": "Hebrew Book of Variants",
  385. "sectionNames": ["Chapter", "Paragraph"],
  386. "categories": ["Musar"],
  387. }
  388. response = c.post("/api/index/Book_of_Variants", {'json': json.dumps(index)})
  389. self.assertEqual(200, response.status_code)
  390. data = json.loads(response.content)
  391. self.assertNotIn("error", data)
  392. self.assertIn("titleVariants", data)
  393. self.assertIn("Book of Variants", data["titleVariants"])
  394. # Post with non empty variants, missing title from variants
  395. index = {
  396. "title": "Book of Variants",
  397. "titleVariants": ["BOV"],
  398. "heTitle": u"Hebrew Book of Variants",
  399. "heTitleVariants": [],
  400. "sectionNames": ["Chapter", "Paragraph"],
  401. "categories": ["Musar"],
  402. }
  403. response = c.post("/api/index/Book_of_Variants", {'json': json.dumps(index)})
  404. self.assertEqual(200, response.status_code)
  405. data = json.loads(response.content)
  406. self.assertNotIn("error", data)
  407. self.assertIn("titleVariants", data)
  408. self.assertIn("Book of Variants", data["titleVariants"])
  409. # Post Commentary index with empty variants
  410. index = {
  411. "title": "Reb Rabbit",
  412. "heTitle": u"Hebrew Reb Rabbit",
  413. "titleVariants": [],
  414. "categories": ["Commentary"],
  415. }
  416. response = c.post("/api/index/Reb_Rabbit", {'json': json.dumps(index)})
  417. self.assertEqual(200, response.status_code)
  418. data = json.loads(response.content)
  419. self.assertNotIn("error", data)
  420. self.assertIn("titleVariants", data)
  421. self.assertIn("Reb Rabbit", data["titleVariants"])
  422. '''
  423. class PostTextNameChange(SefariaTestCase):
  424. """
  425. Tests:
  426. Post/Delete of Note
  427. Post/Delete of Link
  428. Index title change casacade to:
  429. Books list updated
  430. TOC updated
  431. Versions updated
  432. Notes updated
  433. Links updated
  434. History updated
  435. Cache updated
  436. """
  437. def setUp(self):
  438. self.make_test_user()
  439. def tearDown(self):
  440. IndexSet({"title": {"$in": ["Name Change Test", "Name Changed"]}}).delete()
  441. NoteSet({"ref": {"$regex": "^Name Change Test"}}).delete()
  442. NoteSet({"ref": {"$regex": "^Name Changed"}}).delete()
  443. HistorySet({"rev_type": "add index", "title": "Name Change Test"}).delete()
  444. HistorySet({"version": "The Name Change Test Edition", "rev_type": "add text"}).delete()
  445. HistorySet({"new.refs": {"$regex": "Name Change Test"}, "rev_type": "add link"}).delete()
  446. HistorySet({"new.ref": {"$regex": "Name Change Test"}, "rev_type": "add note"}).delete()
  447. HistorySet({"rev_type": "add index", "title": "Name Changed"}).delete()
  448. HistorySet({"ref": {"$regex": "Name Changed"}, "rev_type": "add text"}).delete()
  449. HistorySet({"new.ref": {"$regex": "Name Changed"}, "rev_type": "add note"}).delete()
  450. HistorySet({"new.refs": {"$regex": "Name Changed"}, "rev_type": "add link"}).delete()
  451. HistorySet({"old.ref": {"$regex": "Name Changed"}, "rev_type": "delete note"}).delete()
  452. HistorySet({"old.refs": {"$regex": "Name Changed"}, "rev_type": "delete link"}).delete()
  453. def test_change_index_name(self):
  454. # Set up an index and text to test
  455. index = {
  456. "title": "Name Change Test",
  457. "titleVariants": ["The Book of Name Change Test"],
  458. "heTitle": u'Hebrew Name Change Test',
  459. "sectionNames": ["Chapter", "Paragraph"],
  460. "categories": ["Musar"],
  461. }
  462. response = c.post("/api/index/Name_Change_Test", {'json': json.dumps(index)})
  463. self.assertEqual(200, response.status_code)
  464. self.assertEqual(1, HistorySet({"rev_type": "add index", "title": "Name Change Test"}).count())
  465. self.in_cache("Name Change Test")
  466. # Post some text, including one citation
  467. text = {
  468. "text": "Blah blah blah Genesis 5:12 blah",
  469. "versionTitle": "The Name Change Test Edition",
  470. "versionSource": "www.sefaria.org",
  471. "language": "en",
  472. }
  473. response = c.post("/api/texts/Name_Change_Test.1.1", {'json': json.dumps(text)})
  474. self.assertEqual(200, response.status_code)
  475. self.assertEqual(1, LinkSet({"refs": {"$regex": "^Name Change Test"}}).count())
  476. self.assertEqual(1, HistorySet({"version": "The Name Change Test Edition", "rev_type": "add text"}).count())
  477. self.assertEqual(1, HistorySet({"new.refs": {"$regex": "Name Change Test"}, "rev_type": "add link"}).count())
  478. # Test posting notes and links
  479. note1 = {
  480. 'title': u'test title 1',
  481. 'text': u'test body 1',
  482. 'type': u'note',
  483. 'ref': u'Name Change Test 1.1',
  484. 'public': False
  485. }
  486. note2 = {
  487. 'title': u'test title 2',
  488. 'text': u'test body 2',
  489. 'type': u'note',
  490. 'ref': u'Name Change Test 1.1',
  491. 'public': True
  492. }
  493. link1 = {
  494. 'refs': ['Name Change Test 1.1', 'Genesis 1:5'],
  495. 'type': 'reference'
  496. }
  497. link2 = {
  498. 'refs': ['Name Change Test 1.1', 'Rashi on Genesis 1:5'],
  499. 'type': 'reference'
  500. }
  501. # Post notes and refs and record ids of records
  502. for o in [note1, note2, link1, link2]:
  503. url = "/api/notes/" if o['type'] == 'note' else "/api/links/"
  504. response = c.post(url, {'json': json.dumps(o)})
  505. self.assertEqual(200, response.status_code)
  506. data = json.loads(response.content)
  507. self.assertIn("_id", data)
  508. o["id"] = data["_id"]
  509. # test history
  510. self.assertEqual(1, HistorySet(
  511. {"new.ref": {"$regex": "Name Change Test"}, "rev_type": "add note"}).count()) # only one is public
  512. self.assertEqual(3, HistorySet({"new.refs": {"$regex": "Name Change Test"}, "rev_type": "add link"}).count())
  513. # Change name of index record
  514. orig = json.loads(c.get("/api/index/Name_Change_Test").content)
  515. new = deepcopy(orig)
  516. new["oldTitle"] = orig["title"]
  517. new["title"] = "Name Changed"
  518. new["titleVariants"].remove("Name Change Test")
  519. response = c.post("/api/index/Name_Changed", {'json': json.dumps(new)})
  520. self.assertEqual(200, response.status_code)
  521. # Check for change on index record
  522. response = c.get("/api/index/Name_Changed")
  523. self.assertEqual(200, response.status_code)
  524. data = json.loads(response.content)
  525. self.assertTrue(u"Name Changed" == data["title"])
  526. self.assertIn(u"Name Changed", data["titleVariants"])
  527. self.assertTrue(u"Name Change Test" not in data["titleVariants"])
  528. # In History
  529. self.assertEqual(0, HistorySet({"rev_type": "add index", "title": "Name Change Test"}).count())
  530. self.assertEqual(0, HistorySet({"ref": {"$regex": "Name Change Test"}, "rev_type": "add text"}).count())
  531. self.assertEqual(0, HistorySet({"new.ref": {"$regex": "Name Change Test"}, "rev_type": "add note"}).count())
  532. self.assertEqual(0, HistorySet({"new.refs": {"$regex": "Name Change Test"}, "rev_type": "add link"}).count())
  533. self.assertEqual(1, HistorySet({"rev_type": "add index", "title": "Name Changed"}).count())
  534. self.assertEqual(1, HistorySet({"ref": {"$regex": "Name Changed"}, "rev_type": "add text"}).count())
  535. self.assertEqual(1, HistorySet({"new.ref": {"$regex": "Name Changed"}, "rev_type": "add note"}).count())
  536. self.assertEqual(3, HistorySet({"new.refs": {"$regex": "Name Changed"}, "rev_type": "add link"}).count())
  537. # In cache
  538. self.not_in_cache("Name Change Test")
  539. self.in_cache("Name Changed")
  540. # And in the titles api
  541. response = c.get("/api/index/titles")
  542. data = json.loads(response.content)
  543. self.assertTrue(u"Name Changed" in data["books"])
  544. self.assertTrue(u"Name Change Test" not in data["books"])
  545. # toc changed
  546. toc = json.loads(c.get("/api/index").content)
  547. tutils.verify_title_existence_in_toc(new["title"], expected_toc_location=orig['categories'])
  548. self.assertEqual(2, NoteSet({"ref": {"$regex": "^Name Changed"}}).count())
  549. self.assertEqual(3, LinkSet({"refs": {"$regex": "^Name Changed"}}).count())
  550. # Now delete a link and a note
  551. response = c.delete("/api/links/" + link1["id"])
  552. self.assertEqual(200, response.status_code)
  553. response = c.delete("/api/notes/" + note2["id"])
  554. self.assertEqual(200, response.status_code)
  555. # Make sure two are now deleted
  556. self.assertEqual(1, NoteSet({"ref": {"$regex": "^Name Changed"}}).count())
  557. self.assertEqual(2, LinkSet({"refs": {"$regex": "^Name Changed"}}).count())
  558. # and that deletes show up in history
  559. self.assertEqual(1, HistorySet({"old.ref": {"$regex": "Name Changed"}, "rev_type": "delete note"}).count())
  560. self.assertEqual(1, HistorySet({"old.refs": {"$regex": "Name Changed"}, "rev_type": "delete link"}).count())
  561. # Delete Test Index
  562. IndexSet({"title": u'Name Changed'}).delete()
  563. # Make sure that index was deleted, and that delete cascaded to: versions, counts, links, cache
  564. self.not_in_cache("Name Changed")
  565. self.assertEqual(0, IndexSet({"title": u'Name Changed'}).count())
  566. self.assertEqual(0, VersionSet({"title": u'Name Changed'}).count())
  567. self.assertEqual(0, VersionStateSet({"title": u'Name Changed'}).count())
  568. self.assertEqual(0, LinkSet({"refs": {"$regex": "^Name Changed"}}).count())
  569. self.assertEqual(0, NoteSet({"ref": {"$regex": "^Name Changed"}}).count())
  570. """class PostCommentatorNameChange(SefariaTestCase):
  571. def setUp(self):
  572. self.make_test_user()
  573. def tearDown(self):
  574. IndexSet({"title": "Ploni"}).delete()
  575. IndexSet({"title": "Shmoni"}).delete()
  576. HistorySet({"title": "Ploni"}).delete()
  577. HistorySet({"title": "Shmoni"}).delete()
  578. HistorySet({"version": "Ploni Edition"}).delete()
  579. HistorySet({"new.refs": {"$regex": "^Ploni on Job"}}).delete()
  580. HistorySet({"new.refs": {"$regex": "^Shmoni on Job"}}).delete()
  581. VersionStateSet({"title": "Ploni on Job"}).delete()
  582. VersionStateSet({"title":"Shmoni on Job"}).delete()
  583. def test_change_commentator_name(self):
  584. index = {
  585. "title": "Ploni",
  586. "heTitle": u"Hebrew Ploni",
  587. "titleVariants": ["Ploni"],
  588. "categories": ["Commentary"]
  589. }
  590. response = c.post("/api/index/Ploni", {'json': json.dumps(index)})
  591. self.assertEqual(200, response.status_code)
  592. data = json.loads(response.content)
  593. self.assertNotIn("error", data)
  594. self.assertEqual(1, IndexSet({"title": "Ploni"}).count())
  595. # Bare commentator names not in Index
  596. # self.in_cache("Ploni")
  597. # Virtual Indexes are available for commentary texts
  598. response = c.get("/api/index/Ploni_on_Job")
  599. self.assertEqual(200, response.status_code)
  600. data = json.loads(response.content)
  601. self.assertIn("categories", data)
  602. self.assertEqual(["Commentary", "Tanakh", "Ploni"], data["categories"])
  603. # Post some text
  604. text = {
  605. "text": ["Comment 1", "Comment 2", "Comment 3"],
  606. "versionTitle": "Ploni Edition",
  607. "versionSource": "www.sefaria.org",
  608. "language": "en",
  609. }
  610. response = c.post("/api/texts/Ploni_on_Job.2.2", {'json': json.dumps(text)})
  611. self.assertEqual(200, response.status_code)
  612. data = json.loads(response.content)
  613. self.assertNotIn("error", data)
  614. self.assertEqual(3, LinkSet({"refs": {"$regex": "^Ploni on Job"}}).count())
  615. # Change name of Commentator
  616. orig = json.loads(c.get("/api/index/Ploni").content)
  617. new = deepcopy(orig)
  618. new["oldTitle"] = orig["title"]
  619. new["title"] = "Shmoni"
  620. new["titleVariants"].remove("Ploni")
  621. response = c.post("/api/index/Shmoni", {'json': json.dumps(new)})
  622. self.assertEqual(200, response.status_code)
  623. data = json.loads(response.content)
  624. self.assertNotIn("error", data)
  625. # Check index records
  626. self.assertEqual(0, IndexSet({"title": "Ploni"}).count())
  627. self.assertEqual(1, IndexSet({"title": "Shmoni"}).count())
  628. # Check change propogated to Links
  629. self.assertEqual(0, VersionSet({"title": "Ploni on Job"}).count())
  630. self.assertEqual(1, VersionSet({"title": "Shmoni on Job"}).count())
  631. # Check change propogated to Links
  632. self.assertEqual(0, LinkSet({"refs": {"$regex": "^Ploni on Job"}}).count())
  633. self.assertEqual(3, LinkSet({"refs": {"$regex": "^Shmoni on Job"}}).count())
  634. # Check Cache Updated
  635. self.not_in_cache("Ploni")
  636. self.in_cache("Shmoni")
  637. #toc changed
  638. toc = json.loads(c.get("/api/index").content)
  639. tutils.verify_title_existence_in_toc(new["title"], None)
  640. """
  641. class PostTextTest(SefariaTestCase):
  642. """
  643. Tests posting text content to Texts API.
  644. """
  645. def setUp(self):
  646. self.make_test_user()
  647. def tearDown(self):
  648. IndexSet({"title": "Sefer Test"}).delete()
  649. IndexSet({"title": "Ploni"}).delete()
  650. VersionSet({"versionTitle": "test_default_node"}).delete()
  651. def test_post_new_text(self):
  652. """
  653. Tests:
  654. post of index & that new index is in index/titles
  655. post and get of English text
  656. post and get of Hebrew text
  657. Verify that in-text ref is caught and made a link
  658. Verify that changing of in-text ref results in old link removed and new one added
  659. counts docs of both he and en
  660. index delete and its cascading
  661. """
  662. # Post a new Index
  663. index = {
  664. "title": "Sefer Test",
  665. "titleVariants": ["The Book of Test"],
  666. "heTitle": u"Hebrew Sefer Test",
  667. "sectionNames": ["Chapter", "Paragraph"],
  668. "categories": ["Musar"],
  669. }
  670. response = c.post("/api/index/Sefer_Test", {'json': json.dumps(index)})
  671. self.assertEqual(200, response.status_code)
  672. data = json.loads(response.content)
  673. self.assertIn("titleVariants", data)
  674. self.assertIn(u'Sefer Test', data["titleVariants"])
  675. response = c.get("/api/index/titles")
  676. data = json.loads(response.content)
  677. self.assertIn(u'Sefer Test', data["books"])
  678. # test the toc is updated
  679. toc = json.loads(c.get("/api/index").content)
  680. tutils.verify_title_existence_in_toc(index['title'], expected_toc_location=index['categories'])
  681. # Post Text (with English citation)
  682. text = {
  683. "text": "As it is written in Job 3:14, waste places.",
  684. "versionTitle": "The Test Edition",
  685. "versionSource": "www.sefaria.org",
  686. "language": "en",
  687. }
  688. response = c.post("/api/texts/Sefer_Test.99.99", {'json': json.dumps(text)})
  689. self.assertEqual(200, response.status_code)
  690. data = json.loads(response.content)
  691. self.assertTrue("error" not in data)
  692. # Verify one link was auto extracted
  693. response = c.get('/api/texts/Sefer_Test.99.99')
  694. self.assertEqual(200, response.status_code)
  695. data = json.loads(response.content)
  696. self.assertEqual(1, len(data["commentary"]))
  697. # Verify Count doc was updated
  698. response = c.get('/api/counts/Sefer_Test')
  699. self.assertEqual(200, response.status_code)
  700. data = json.loads(response.content)
  701. self.assertNotIn("error", data)
  702. self.assertEqual([1, 1], data["_en"]["availableCounts"])
  703. self.assertEqual(1, data["_en"]["availableTexts"][98][98])
  704. self.assertEqual(0, data["_en"]["availableTexts"][98][55])
  705. # Update link in the text
  706. text = {
  707. "text": "As it is written in Job 4:10, The lions may roar and growl.",
  708. "versionTitle": "The Test Edition",
  709. "versionSource": "www.sefaria.org",
  710. "language": "en",
  711. }
  712. response = c.post("/api/texts/Sefer_Test.99.99", {'json': json.dumps(text)})
  713. self.assertEqual(200, response.status_code)
  714. data = json.loads(response.content)
  715. self.assertTrue("error" not in data)
  716. # Verify one link was auto extracted
  717. response = c.get('/api/texts/Sefer_Test.99.99')
  718. self.assertEqual(200, response.status_code)
  719. data = json.loads(response.content)
  720. self.assertEqual(1, len(data["commentary"]))
  721. self.assertEqual(data["commentary"][0]["ref"], 'Job 4:10')
  722. # Post Text (with Hebrew citation)
  723. text = {
  724. "text": 'כדכתיב: "לא תעשה לך פסל כל תמונה" כו (דברים ה ח)',
  725. "versionTitle": "The Hebrew Test Edition",
  726. "versionSource": "www.sefaria.org",
  727. "language": "he",
  728. }
  729. response = c.post("/api/texts/Sefer_Test.88.88", {'json': json.dumps(text)})
  730. self.assertEqual(200, response.status_code)
  731. # Verify one link was auto extracted
  732. response = c.get('/api/texts/Sefer_Test.88.88')
  733. self.assertEqual(200, response.status_code)
  734. data = json.loads(response.content)
  735. self.assertEqual(1, len(data["commentary"]))
  736. # Verify count doc was updated
  737. response = c.get('/api/counts/Sefer_Test')
  738. self.assertEqual(200, response.status_code)
  739. data = json.loads(response.content)
  740. self.assertEqual([1, 1], data["_he"]["availableCounts"])
  741. self.assertEqual(1, data["_he"]["availableTexts"][87][87])
  742. self.assertEqual(0, data["_en"]["availableTexts"][87][87])
  743. # Delete Test Index
  744. textRegex = Ref('Sefer Test').regex()
  745. IndexSet({"title": u'Sefer Test'}).delete()
  746. # Make sure that index was deleted, and that delete cascaded to: versions, counts, links, cache,
  747. # todo: notes?, reviews?
  748. self.assertEqual(0, IndexSet({"title": u'Sefer Test'}).count())
  749. self.assertEqual(0, VersionSet({"title": u'Sefer Test'}).count())
  750. self.assertEqual(0, VersionStateSet({"title": u'Sefer Test'}).count())
  751. # todo: better way to do this?
  752. self.assertEqual(0, LinkSet({"refs": {"$regex": textRegex}}).count())
  753. """def test_post_commentary_text(self):
  754. '''
  755. Tests:
  756. Posting a new commentator index
  757. Get a virtual index for comentator on a text
  758. Posting commentary text
  759. Commentary links auto generated
  760. '''
  761. index = {
  762. "title": "Ploni",
  763. "heTitle": "Hebrew Ploni",
  764. "titleVariants": ["Ploni"],
  765. "categories": ["Commentary"]
  766. }
  767. response = c.post("/api/index/Ploni", {'json': json.dumps(index)})
  768. self.assertEqual(200, response.status_code)
  769. data = json.loads(response.content)
  770. self.assertNotIn("error", data)
  771. self.assertEqual(1, IndexSet({"title": "Ploni"}).count())
  772. # Virtual Indexes are available for commentary texts
  773. response = c.get("/api/index/Ploni_on_Job")
  774. self.assertEqual(200, response.status_code)
  775. data = json.loads(response.content)
  776. self.assertIn("categories", data)
  777. self.assertEqual(["Commentary", "Tanakh", "Ploni"], data["categories"])
  778. # Post some text
  779. text = {
  780. "text": ["Comment 1", "Comment 2", "Comment 3"],
  781. "versionTitle": "Ploni Edition",
  782. "versionSource": "www.sefaria.org",
  783. "language": "en",
  784. }
  785. response = c.post("/api/texts/Ploni_on_Job.2.2", {'json': json.dumps(text)})
  786. self.assertEqual(200, response.status_code)
  787. data = json.loads(response.content)
  788. self.assertNotIn("error", data)
  789. self.assertEqual(3, LinkSet({"refs": {"$regex": "^Ploni on Job"}}).count())
  790. """
  791. def test_post_to_default_node(self):
  792. text = {
  793. "text": [["BFoo", "PBar", "Dub Blitz"], ["GGGlam", "BBBlam", "Ber Flam"]],
  794. "versionTitle": "test_default_node",
  795. "versionSource": "www.sefaria.org",
  796. "language": "en",
  797. }
  798. response = c.post("/api/texts/Chofetz_Chaim,_Part_One,_The_Prohibition_Against_Lashon_Hara,_Principle_1",
  799. {'json': json.dumps(text)})
  800. self.assertEqual(200, response.status_code)
  801. data = json.loads(response.content)
  802. self.assertTrue("error" not in data)
  803. subref = Ref("Chofetz_Chaim,_Part_One,_The_Prohibition_Against_Lashon_Hara,_Principle_1.2.3")
  804. assert TextChunk(subref, "en", "test_default_node").text == "Ber Flam"
  805. class PostCategory(SefariaTestCase):
  806. """
  807. Tests posting text content to Texts API.
  808. """
  809. def setUp(self):
  810. self.make_test_user()
  811. def tearDown(self):
  812. cat = Category().load({"path": ["Tanakh", "New Works"]})
  813. if cat:
  814. cat.delete()
  815. library.rebuild(include_toc=True)
  816. def test_duplicate_rejected(self):
  817. cat = {
  818. "path": [
  819. "Tanakh",
  820. "Torah"
  821. ],
  822. "titles": [
  823. {
  824. "lang": "en",
  825. "text": "Torah",
  826. "primary": True
  827. },
  828. {
  829. "lang": "he",
  830. "text": u"תורה",
  831. "primary": True
  832. }
  833. ]
  834. }
  835. response = c.post("/api/category", {'json': json.dumps(cat)})
  836. self.assertEqual(200, response.status_code)
  837. data = json.loads(response.content)
  838. self.assertTrue("error" in data)
  839. def test_orphan_rejected(self):
  840. cat = {
  841. "path": [
  842. "Tanakh",
  843. "Other Stuff",
  844. "New Works"
  845. ],
  846. "titles": [
  847. {
  848. "lang": "en",
  849. "text": "New Works",
  850. "primary": True
  851. },
  852. {
  853. "lang": "he",
  854. "text": u"חידושים",
  855. "primary": True
  856. }
  857. ]
  858. }
  859. response = c.post("/api/category", {'json': json.dumps(cat)})
  860. self.assertEqual(200, response.status_code)
  861. data = json.loads(response.content)
  862. self.assertTrue("error" in data)
  863. def test_incomplete_record_rejected(self):
  864. cat = {
  865. "titles": [
  866. {
  867. "lang": "en",
  868. "text": "New Works",
  869. "primary": True
  870. },
  871. {
  872. "lang": "he",
  873. "text": u"חידושים",
  874. "primary": True
  875. }
  876. ]
  877. }
  878. response = c.post("/api/category", {'json': json.dumps(cat)})
  879. self.assertEqual(200, response.status_code)
  880. data = json.loads(response.content)
  881. self.assertTrue("error" in data)
  882. cat = {
  883. "path": [
  884. "Tanakh",
  885. "New Works"
  886. ],
  887. "titles": [
  888. {
  889. "lang": "en",
  890. "text": "New Works",
  891. "primary": True
  892. }
  893. ]
  894. }
  895. response = c.post("/api/category", {'json': json.dumps(cat)})
  896. self.assertEqual(200, response.status_code)
  897. data = json.loads(response.content)
  898. self.assertTrue("error" in data)
  899. cat = {
  900. "path": [
  901. "Tanakh",
  902. "New Works"
  903. ],
  904. "titles": [
  905. {
  906. "lang": "he",
  907. "text": u"חידושים",
  908. "primary": True
  909. }
  910. ]
  911. }
  912. response = c.post("/api/category", {'json': json.dumps(cat)})
  913. self.assertEqual(200, response.status_code)
  914. data = json.loads(response.content)
  915. self.assertTrue("error" in data)
  916. def test_legit_post(self):
  917. cat = {
  918. "path": [
  919. "Tanakh",
  920. "New Works"
  921. ],
  922. "titles": [
  923. {
  924. "lang": "en",
  925. "text": "New Works",
  926. "primary": True
  927. },
  928. {
  929. "lang": "he",
  930. "text": u"חידושים",
  931. "primary": True
  932. }
  933. ]
  934. }
  935. response = c.post("/api/category", {'json': json.dumps(cat)})
  936. self.assertEqual(200, response.status_code)
  937. data = json.loads(response.content)
  938. self.assertTrue("error" not in data)
  939. self.assertTrue(Category().load({"path": ["Tanakh", "New Works"]}))
  940. class PostLinks(SefariaTestCase):
  941. """
  942. Tests posting text content to Texts API.
  943. """
  944. def setUp(self):
  945. self.make_test_user()
  946. def tearDown(self):
  947. LinkSet({"refs": {"$regex": 'Meshech Hochma'}, "anchorText": {"$exists": 1, "$ne": ""}}).delete()
  948. def test_post_new_links(self):
  949. """
  950. Tests:
  951. posts a batch of links
  952. """
  953. # Post a new Index
  954. import random as rand
  955. bible_books = ['Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy', 'Joshua', 'Judges', 'Ruth', 'Esther',
  956. 'Lamentations']
  957. links = []
  958. for i in range(1, 61):
  959. for j in range(1, 11):
  960. link_obj = {
  961. "type": "commentary",
  962. "refs": ["Meshech Hochma %d:%d" % (i, j), "%s 1:1" % rand.choice(bible_books)],
  963. "anchorText": u"עת לעשות לה' הפרו תורתך",
  964. }
  965. links.append(link_obj)
  966. self.assertEqual(600, len(links))
  967. response = c.post("/api/links/", {'json': json.dumps(links)})
  968. print response.status_code
  969. self.assertEqual(200, response.status_code)
  970. self.assertNotEqual(600, LinkSet({"refs": {"$regex": 'Meshech Hochma'}}).count())
  971. # Delete links
  972. LinkSet({"refs": {"$regex": 'Meshech Hochma'}, "anchorText": {"$exists": 1, "$ne": ""}}).delete()
  973. class SheetPostTest(SefariaTestCase):
  974. """
  975. Tests posting a Source Sheet.
  976. """
  977. _sheet_id = None
  978. def setUp(self):
  979. self.make_test_user()
  980. def tearDown(self):
  981. if self._sheet_id:
  982. db.sheets.remove({"id": self._sheet_id})
  983. db.history.remove({"sheet": self._sheet_id})
  984. def test_post_sheet(self):
  985. """
  986. Tests:
  987. Posting a new source sheet
  988. Add a source via add_source_to_sheet API
  989. Publish Sheet, history recorded
  990. Unpublish Sheet, history deleted
  991. Deleting a source sheet
  992. """
  993. sheet = {
  994. "title": "Test Sheet",
  995. "sources": [],
  996. "options": {},
  997. "status": "unlisted"
  998. }
  999. response = c.post("/api/sheets", {'json': json.dumps(sheet)})
  1000. self.assertEqual(200, response.status_code)
  1001. data = json.loads(response.content)
  1002. self.assertIn("id", data)
  1003. self.assertIn("dateCreated", data)
  1004. self.assertIn("dateModified", data)
  1005. self.assertIn("views", data)
  1006. self.assertEqual(1, data["owner"])
  1007. sheet_id = data["id"]
  1008. self._sheet_id = sheet_id
  1009. sheet = data
  1010. sheet["lastModified"] = sheet["dateModified"]
  1011. # Add a source via add source API
  1012. response = c.post("/api/sheets/{}/add_ref".format(sheet_id), {"ref": "Mishnah Peah 1:1"})
  1013. self.assertEqual(200, response.status_code)
  1014. data = json.loads(response.content)
  1015. self.assertTrue("error" not in data)
  1016. response = c.get("/api/sheets/{}".format(sheet_id))
  1017. self.assertEqual(200, response.status_code)
  1018. data = json.loads(response.content)
  1019. self.assertEqual("Mishnah Peah 1:1", data["sources"][0]["ref"])
  1020. # Publish Sheet
  1021. sheet["status"] = "public"
  1022. sheet["lastModified"] = data["dateModified"]
  1023. response = c.post("/api/sheets", {'json': json.dumps(sheet)})
  1024. self.assertEqual(200, response.status_code)
  1025. data = json.loads(response.content)
  1026. self.assertIn("datePublished", data)
  1027. self.assertEqual("public", data["status"])
  1028. log = db.history.find().sort([["_id", -1]]).limit(1).next()
  1029. self.assertEqual(1, log["user"])
  1030. self.assertEqual(sheet_id, log["sheet"])
  1031. self.assertEqual("publish sheet", log["rev_type"])
  1032. # Unpublish Sheet
  1033. sheet["status"] = "unlisted"
  1034. sheet["lastModified"] = data["dateModified"]
  1035. response = c.post("/api/sheets", {'json': json.dumps(sheet)})
  1036. self.assertEqual(200, response.status_code)
  1037. data = json.loads(response.content)
  1038. self.assertEqual("unlisted", data["status"])
  1039. log = db.history.find_one({"rev_type": "publish sheet", "sheet": sheet_id})
  1040. self.assertEqual(None, log)
  1041. # Delete the Sheet
  1042. response = c.post("/api/sheets/{}/delete".format(sheet_id), {})
  1043. self.assertEqual(200, response.status_code)
  1044. data = json.loads(response.content)
  1045. self.assertTrue("error" not in data)
  1046. self.assertEqual(0, db.sheets.find({"id": sheet_id}).count())
  1047. class UserSyncTest(SefariaTestCase):
  1048. """
  1049. Test syncing user's settings and history
  1050. """
  1051. def setUp(self):
  1052. self.make_test_user()
  1053. def tearDown(self):
  1054. uhs = UserHistorySet()
  1055. uhs.delete()
  1056. @staticmethod
  1057. def d(x):
  1058. return json.dumps(x)
  1059. @staticmethod
  1060. def l(x):
  1061. return json.loads(x)
  1062. def test_simple_sync(s):
  1063. response = c.post("/api/profile/sync", {
  1064. 'last_sync': s.d(0),
  1065. 'user_history': s.d([
  1066. {
  1067. "ref": "Genesis 1:1",
  1068. "time_stamp": 1,
  1069. "server_time_stamp": 1,
  1070. "versions": {}
  1071. }
  1072. ])
  1073. })
  1074. data = s.l(response.content)
  1075. s.assertEqual(len(data["user_history"]), 0)
  1076. response = c.post("/api/profile/sync", {
  1077. 'last_sync': s.d(0),
  1078. 'user_history': s.d([
  1079. {
  1080. "ref": "Genesis 1:2",
  1081. "time_stamp": 2,
  1082. "server_time_stamp": 2,
  1083. "versions": {}
  1084. }
  1085. ])
  1086. })
  1087. data = s.l(response.content)
  1088. s.assertEqual(len(data["user_history"]), 1)
  1089. s.assertEqual(data["user_history"][0]["ref"], "Genesis 1:1")
  1090. def test_save_delete(s):
  1091. hist1 = {
  1092. "ref": "Genesis 1:1",
  1093. "time_stamp": 0,
  1094. "server_time_stamp": 0,
  1095. "versions": {}
  1096. }
  1097. hist1save = hist1.copy()
  1098. hist1save['action'] = 'add_saved'
  1099. hist1delete = hist1.copy()
  1100. hist1delete['action'] = 'delete_saved'
  1101. hist1delete['server_time_stamp'] = 1
  1102. hist2 = hist1.copy()
  1103. hist2['ref'] = "Genesis 1:2"
  1104. response = c.post("/api/profile/sync", {
  1105. 'last_sync': s.d(0),
  1106. 'user_history': s.d([
  1107. hist1
  1108. ])
  1109. })
  1110. response = c.post("/api/profile/sync", {
  1111. 'last_sync': s.d(0),
  1112. 'user_history': s.d([
  1113. hist1save
  1114. ])
  1115. })
  1116. response = c.get("/api/profile/user_history?saved=1")
  1117. data = s.l(response.content)
  1118. s.assertEqual(len(data), 1)
  1119. s.assertEqual(data[0]['ref'], "Genesis 1:1")
  1120. response = c.post("/api/profile/sync", {
  1121. 'last_sync': s.d(0),
  1122. 'user_history': s.d([
  1123. hist1delete
  1124. ])
  1125. })
  1126. response = c.get("/api/profile/user_history?saved=1")
  1127. data = s.l(response.content)
  1128. s.assertEqual(len(data), 0)
  1129. # check that another device will pick up a modification of a saved item
  1130. response = c.post("/api/profile/sync", {
  1131. 'last_sync': s.d(0),
  1132. })
  1133. data = s.l(response.content)
  1134. s.assertEqual(data["user_history"][0]["ref"], "Genesis 1:1")
  1135. s.assertFalse(data["user_history"][0]["saved"])
  1136. def test_settings(self):
  1137. pass
  1138. '''
  1139. # Fails. Ignore
  1140. class VersionAttrsPostTest(SefariaTestCase):
  1141. def test_post_atts(self):
  1142. vattrs = {
  1143. "status" : "locked",
  1144. "license" : "Public domain",
  1145. "licenseVetted" : True,
  1146. "digitizedBySefaria" : True,
  1147. "priority" : 1
  1148. }
  1149. response = c.post("api/version/flags/Genesis/he/Tanach+With+Nikkud", {'json': json.dumps(vattrs)})
  1150. self.assertEqual(200, response.status_code)
  1151. data = json.loads(response.content)
  1152. '''