test_helpers.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #-*- coding: utf-8 -*-
  2. import datetime
  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
  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 = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
  24. forumsread.cleared = time_read # lets cheat here a bit :P
  25. forumsread.last_read = datetime.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. # no topics in this forum
  32. topic.delete()
  33. forum = Forum.query.filter_by(id=forum.id).first()
  34. flaskbb_config["TRACKER_LENGTH"] = 1 # activate the tracker again
  35. assert forum.topic_count == 0
  36. assert not forum_is_unread(forum, None, user)
  37. def test_topic_is_unread(guest, user, forum, topic, topicsread, forumsread):
  38. # test guest
  39. assert not topic_is_unread(None, None, guest)
  40. # compare topicsread.last_read with topic.last_post.date_created
  41. assert topic_is_unread(topic, topicsread, user, forumsread)
  42. # TopicsRead is none and the forum has never been marked as read
  43. assert topic_is_unread(topic, topicsread=None, user=user, forumsread=forumsread)
  44. # lets mark the forum as read
  45. forumsread.cleared = datetime.datetime.utcnow()
  46. forumsread.last_read = datetime.datetime.utcnow()
  47. forumsread.save()
  48. assert not topic_is_unread(topic, topicsread=None, user=user, forumsread=forumsread)
  49. # disabled tracker
  50. flaskbb_config["TRACKER_LENGTH"] = 0
  51. assert not topic_is_unread(topic, None, user, None)
  52. # post is older than tracker length
  53. time_posted = datetime.datetime.utcnow() - datetime.timedelta(days=2)
  54. flaskbb_config["TRACKER_LENGTH"] = 1
  55. topic.last_post.date_created = time_posted
  56. topic.save()
  57. assert not topic_is_unread(topic, None, user, None)
  58. def test_crop_title(default_settings):
  59. short_title = "Short title"
  60. long_title = "This is just a test title which is too long."
  61. assert crop_title(short_title) == short_title
  62. assert crop_title(long_title) == "This is just a..."
  63. def test_render_markup(default_settings):
  64. markdown = "**Bold**"
  65. assert render_markup(markdown) == "<p><strong>Bold</strong></p>\n"
  66. def test_is_online(default_settings, user):
  67. assert is_online(user)
  68. def test_format_date():
  69. date = datetime.date(2015, 2, 15)
  70. time = datetime.datetime.combine(date, datetime.datetime.min.time())
  71. assert format_date(time) == "2015-02-15"
  72. def test_format_quote(topic):
  73. expected_markdown = "**[test_normal](http://localhost:5000/user/test_normal) wrote:**\n> Test Content Normal\n"
  74. actual = format_quote(topic.first_post.username, topic.first_post.content)
  75. assert actual == expected_markdown
  76. def test_get_image_info():
  77. # some random jpg/gif/png images from my imgur account
  78. jpg = "http://i.imgur.com/NgVIeRG.jpg"
  79. gif = "http://i.imgur.com/l3Vmp4m.gif"
  80. png = "http://i.imgur.com/JXzKxNs.png"
  81. jpg_img = get_image_info(jpg)
  82. assert jpg_img["content-type"] == "image/jpeg"
  83. assert jpg_img["height"] == 1024
  84. assert jpg_img["width"] == 1280
  85. assert jpg_img["size"] == 209.06
  86. gif_img = get_image_info(gif)
  87. assert gif_img["content-type"] == "image/gif"
  88. assert gif_img["height"] == 168
  89. assert gif_img["width"] == 400
  90. assert gif_img["size"] == 576.138
  91. png_img = get_image_info(png)
  92. assert png_img["content-type"] == "image/png"
  93. assert png_img["height"] == 1080
  94. assert png_img["width"] == 1920
  95. assert png_img["size"] == 269.409
  96. def test_check_image(default_settings):
  97. # test200x100.png
  98. img_width = "http://i.imgur.com/4dAWAZI.png"
  99. # test100x200.png
  100. img_height = "http://i.imgur.com/I7GwF3D.png"
  101. # test100x100.png
  102. img_ok = "http://i.imgur.com/CYV6NzT.png"
  103. # random too big image
  104. img_size = "http://i.imgur.com/l3Vmp4m.gif"
  105. # random image wrong type
  106. img_type = "https://d11xdyzr0div58.cloudfront.net/static/logos/archlinux-logo-black-scalable.f931920e6cdb.svg"
  107. data = check_image(img_width)
  108. assert "wide" in data[0]
  109. assert not data[1]
  110. data = check_image(img_height)
  111. assert "high" in data[0]
  112. assert not data[1]
  113. data = check_image(img_type)
  114. assert "type" in data[0]
  115. assert not data[1]
  116. data = check_image(img_ok)
  117. assert data[0] is None
  118. assert data[1]
  119. flaskbb_config["AVATAR_WIDTH"] = 1000
  120. flaskbb_config["AVATAR_HEIGHT"] = 1000
  121. data = check_image(img_size)
  122. assert "big" in data[0]
  123. assert not data[1]