test_news.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. from pprint import pformat
  4. from twisted.trial import unittest
  5. from twisted.news import database
  6. MESSAGE_ID = "f83ba57450ed0fd8ac9a472b847e830e"
  7. POST_STRING = """Path: not-for-mail
  8. From: <exarkun@somehost.domain.com>
  9. Subject: a test
  10. Newsgroups: alt.test.nntp
  11. Organization:
  12. Summary:
  13. Keywords:
  14. Message-Id: %s
  15. User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.4.17 (i686))
  16. this is a test
  17. ...
  18. lala
  19. moo
  20. --
  21. "One World, one Web, one Program." - Microsoft(R) promotional ad
  22. "Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler
  23. --
  24. 10:56pm up 4 days, 4:42, 1 user, load average: 0.08, 0.08, 0.12
  25. """ % (MESSAGE_ID)
  26. class NewsTests(unittest.TestCase):
  27. def setUp(self):
  28. self.backend = database.NewsShelf(None, 'news2.db')
  29. self.backend.addGroup('alt.test.nntp', 'y')
  30. self.backend.postRequest(POST_STRING.replace('\n', '\r\n'))
  31. def testArticleExists(self):
  32. d = self.backend.articleExistsRequest(MESSAGE_ID)
  33. d.addCallback(self.assertTrue)
  34. return d
  35. def testArticleRequest(self):
  36. d = self.backend.articleRequest(None, None, MESSAGE_ID)
  37. def cbArticle(result):
  38. self.assertTrue(isinstance(result, tuple),
  39. 'callback result is wrong type: ' + str(result))
  40. self.assertEqual(len(result), 3,
  41. 'callback result list should have three entries: ' +
  42. str(result))
  43. self.assertEqual(result[1], MESSAGE_ID,
  44. "callback result Message-Id doesn't match: %s vs %s" %
  45. (MESSAGE_ID, result[1]))
  46. body = result[2].read()
  47. self.assertNotEqual(body.find('\r\n\r\n'), -1,
  48. "Can't find \\r\\n\\r\\n between header and body")
  49. return result
  50. d.addCallback(cbArticle)
  51. return d
  52. def testHeadRequest(self):
  53. d = self.testArticleRequest()
  54. def cbArticle(result):
  55. index = result[0]
  56. d = self.backend.headRequest("alt.test.nntp", index)
  57. d.addCallback(cbHead)
  58. return d
  59. def cbHead(result):
  60. self.assertEqual(result[1], MESSAGE_ID,
  61. "callback result Message-Id doesn't match: %s vs %s" %
  62. (MESSAGE_ID, result[1]))
  63. self.assertEqual(result[2][-2:], '\r\n',
  64. "headers must be \\r\\n terminated.")
  65. d.addCallback(cbArticle)
  66. return d
  67. def testBodyRequest(self):
  68. d = self.testArticleRequest()
  69. def cbArticle(result):
  70. index = result[0]
  71. d = self.backend.bodyRequest("alt.test.nntp", index)
  72. d.addCallback(cbBody)
  73. return d
  74. def cbBody(result):
  75. body = result[2].read()
  76. self.assertEqual(body[0:4], 'this',
  77. "message body has been altered: " +
  78. pformat(body[0:4]))
  79. d.addCallback(cbArticle)
  80. return d