def test_api(self): crs = CourseOffering.objects.get(slug=TEST_COURSE_SLUG) memb = Member.objects.get(offering=crs, person__userid="ggbaker") person = Person.objects.get(userid='ggbaker') p = Page(offering=crs, label="PageExists") p.save() v = PageVersion(page=p, title="Page Exists", wikitext="Original Contents", editor=memb, comment="original page") v.save() from dashboard.models import new_feed_token token = new_feed_token() updata = u"""{ "userid": "ggbaker", "token": "%s", "pages": [ { "label": "Index", "title": "The Coursé Page", "can_read": "ALL", "can_write": "INST", "wikitext-base64": "VGhpcyBwYWdlIGlzIHNwZWNpYWwgaW4gKipzb21lKiogd2F5LiBcKHh+XjIrMSA9IFxmcmFjezF9ezJ9XCkuCgpHb29kYnllIHdvcmxkIQ==", "comment": "page creation comment", "use_math": true }, { "label": "PageExists", "new_label": "PageChanged", "title": "Another Page", "can_read": "STUD", "wikitext": "This is some **new** page\\n\\ncontent." } ] }""" % (token) # make a request with no auth token in place c = Client() url = reverse('pages.views.api_import', kwargs={'course_slug': crs.slug}) response = c.post(url, data=updata.encode('utf8'), content_type="application/json") self.assertEquals(response.status_code, 403) # create token and try again person.config['pages-token'] = token person.save() response = c.post(url, data=updata.encode('utf8'), content_type="application/json") self.assertEquals(response.status_code, 200) # make sure the data arrived self.assertEquals(Page.objects.filter(offering=crs, label="PageExists").count(), 0) p = Page.objects.get(offering=crs, label="PageChanged") v = p.current_version() self.assertEqual(v.title, "Another Page") self.assertEqual(v.get_wikitext(), "This is some **new** page\n\ncontent.") p = Page.objects.get(offering=crs, label="Index") v = p.current_version() self.assertEqual(v.title, u"The Cours\u00e9 Page") self.assertEqual(v.get_wikitext(), 'This page is special in **some** way. \\(x~^2+1 = \\frac{1}{2}\\).\n\nGoodbye world!') self.assert_('math' in v.config) self.assertEqual(v.config['math'], True)
def test_version_diffs(self): "Test the old version diffing." crs = CourseOffering.objects.get(slug=TEST_COURSE_SLUG) memb = Member.objects.get(offering=crs, person__userid="ggbaker") p = Page(offering=crs, label="Test") p.save() v1 = PageVersion(page=p, title="T1", wikitext=contents1, editor=memb, comment="original page") v1.save() v2 = PageVersion(page=p, title="T2", wikitext=contents2, editor=memb, comment="some changes") v2.save() v3 = PageVersion(page=p, title="T3", wikitext=contents3, editor=memb, comment="total rewrite") v3.save() # refresh changes in DB v1 = PageVersion.objects.get(id=v1.id) v2 = PageVersion.objects.get(id=v2.id) v3 = PageVersion.objects.get(id=v3.id) # make sure the contents survived self.assertEqual(v1.get_wikitext(), contents1) self.assertEqual(v2.get_wikitext(), contents2) self.assertEqual(v3.get_wikitext(), contents3) self.assertEqual(v1.title, "T1") self.assertEqual(v2.title, "T2") self.assertEqual(v3.title, "T3") # make sure the diff got stored for incremental changes self.assertEqual(v1.wikitext, '') self.assertEqual(v1.diff_from_id, v2.id) # ... but big changes are stored verbatim self.assertEqual(v2.wikitext, contents2) self.assertEqual(v2.diff_from, None) # ... and the head has the current contents self.assertEqual(v3.wikitext, contents3) self.assertEqual(v3.diff_from, None)