test_helpers.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding: utf-8 -*-
  2. import datetime as dt
  3. from flaskbb.utils.helpers import slugify, forum_is_unread, topic_is_unread, \
  4. crop_title, render_markup, is_online, format_date, format_quote, \
  5. get_image_info, check_image, time_utcnow
  6. from flaskbb.utils.settings import flaskbb_config
  7. from flaskbb.forum.models import Forum
  8. def test_slugify():
  9. """Test the slugify helper method."""
  10. assert slugify(u'Hello world') == u'hello-world'
  11. assert slugify(u'¿Cómo está?') == u'como-esta'
  12. def test_forum_is_unread(guest, user, forum, topic, forumsread):
  13. """Test the forum is unread function."""
  14. # for a guest
  15. assert not forum_is_unread(None, None, guest)
  16. # for a logged in user without a forumsread
  17. assert forum_is_unread(forum, None, user)
  18. # same, just with forumsread
  19. assert forum_is_unread(forum, forumsread, user)
  20. # lets mark the forum as read
  21. # but before we have to add an read entry in forumsread and topicsread
  22. topic.update_read(user, topic.forum, forumsread)
  23. time_read = dt.datetime.utcnow() - dt.timedelta(hours=1)
  24. forumsread.cleared = time_read # lets cheat here a bit :P
  25. forumsread.last_read = dt.datetime.utcnow()
  26. forumsread.save()
  27. assert not forum_is_unread(forum, forumsread, user)
  28. # read tracker is disabled
  29. flaskbb_config["TRACKER_LENGTH"] = 0
  30. assert not forum_is_unread(forum, forumsread, user)
  31. # there haven't been a post since TRACKER_LENGTH and thus the forum is read
  32. flaskbb_config["TRACKER_LENGTH"] = 1
  33. # this is cheating; don't do this.
  34. forum.last_post_created = forum.last_post_created - dt.timedelta(hours=48)
  35. forum.save()
  36. assert not forum_is_unread(forum, forumsread, user)
  37. # no topics in this forum
  38. topic.delete()
  39. forum = Forum.query.filter_by(id=forum.id).first()
  40. flaskbb_config["TRACKER_LENGTH"] = 1 # activate the tracker again
  41. assert forum.topic_count == 0
  42. assert not forum_is_unread(forum, None, user)
  43. def test_topic_is_unread(guest, user, forum, topic, topicsread, forumsread):
  44. # test guest
  45. assert not topic_is_unread(None, None, guest)
  46. # compare topicsread.last_read with topic.last_post.date_created
  47. assert topic_is_unread(topic, topicsread, user, forumsread)
  48. # TopicsRead is none and the forum has never been marked as read
  49. assert topic_is_unread(topic, topicsread=None, user=user,
  50. forumsread=forumsread)
  51. # lets mark the forum as read
  52. forumsread.cleared = time_utcnow()
  53. forumsread.last_read = time_utcnow()
  54. forumsread.save()
  55. assert not topic_is_unread(topic, topicsread=None, user=user,
  56. forumsread=forumsread)
  57. # disabled tracker
  58. flaskbb_config["TRACKER_LENGTH"] = 0
  59. assert not topic_is_unread(topic, None, user, None)
  60. # post is older than tracker length
  61. time_posted = time_utcnow() - dt.timedelta(days=2)
  62. flaskbb_config["TRACKER_LENGTH"] = 1
  63. topic.last_post.date_created = time_posted
  64. topic.save()
  65. assert not topic_is_unread(topic, None, user, None)
  66. def test_crop_title(default_settings):
  67. short_title = "Short title"
  68. long_title = "This is just a test title which is too long."
  69. assert crop_title(short_title) == short_title
  70. assert crop_title(long_title) == "This is just a..."
  71. def test_render_markup(default_settings):
  72. markdown = "**Bold**"
  73. assert render_markup(markdown) == "<p><strong>Bold</strong></p>\n"
  74. def test_is_online(default_settings, user):
  75. assert is_online(user)
  76. def test_format_date():
  77. date = dt.date(2015, 2, 15)
  78. time = dt.datetime.combine(date, dt.datetime.min.time())
  79. assert format_date(time) == "2015-02-15"
  80. def test_format_quote(topic):
  81. expected_markdown = "**[test_normal](http://localhost:5000/user/test_normal) wrote:**\n> Test Content Normal\n" # noqa
  82. actual = format_quote(topic.first_post.username, topic.first_post.content)
  83. assert actual == expected_markdown
  84. def test_get_image_info():
  85. # some random jpg/gif/png images from my imgur account
  86. jpg = "http://i.imgur.com/NgVIeRG.jpg"
  87. gif = "http://i.imgur.com/l3Vmp4m.gif"
  88. png = "http://i.imgur.com/JXzKxNs.png"
  89. # Issue #207 Image - This works now
  90. # issue_img = "http://b.reich.io/gtlbjc.jpg"
  91. # issue_img = get_image_info(issue_img)
  92. # assert issue_img["content_type"] == "JPEG"
  93. jpg_img = get_image_info(jpg)
  94. assert jpg_img["content_type"] == "JPEG"
  95. assert jpg_img["height"] == 1024
  96. assert jpg_img["width"] == 1280
  97. assert jpg_img["size"] == 209.06
  98. gif_img = get_image_info(gif)
  99. assert gif_img["content_type"] == "GIF"
  100. assert gif_img["height"] == 168
  101. assert gif_img["width"] == 400
  102. assert gif_img["size"] == 576.138
  103. png_img = get_image_info(png)
  104. assert png_img["content_type"] == "PNG"
  105. assert png_img["height"] == 1080
  106. assert png_img["width"] == 1920
  107. assert png_img["size"] == 269.409
  108. def test_check_image(default_settings):
  109. # test200_100.png
  110. img_width = "http://i.imgur.com/4dAWAZI.png"
  111. # test100_200.png
  112. img_height = "http://i.imgur.com/I7GwF3D.png"
  113. # test100_100.png
  114. img_ok = "http://i.imgur.com/CYV6NzT.png"
  115. # random too big image
  116. img_size = "http://i.imgur.com/l3Vmp4m.gif"
  117. # random image wrong type
  118. img_type = "https://flaskbb.org/static/imgs/flask.svg"
  119. data = check_image(img_width)
  120. assert "wide" in data[0]
  121. assert not data[1]
  122. data = check_image(img_height)
  123. assert "high" in data[0]
  124. assert not data[1]
  125. data = check_image(img_type)
  126. assert "type" in data[0]
  127. assert not data[1]
  128. data = check_image(img_ok)
  129. assert data[0] is None
  130. assert data[1]
  131. flaskbb_config["AVATAR_WIDTH"] = 1000
  132. flaskbb_config["AVATAR_HEIGHT"] = 1000
  133. data = check_image(img_size)
  134. assert "big" in data[0]
  135. assert not data[1]