test_helpers.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. import datetime as dt
  3. from flaskbb.forum.models import Forum
  4. from flaskbb.utils.helpers import (
  5. check_image,
  6. crop_title,
  7. format_quote,
  8. forum_is_unread,
  9. get_image_info,
  10. is_online,
  11. slugify,
  12. time_utcnow,
  13. topic_is_unread,
  14. )
  15. from flaskbb.utils.settings import flaskbb_config
  16. def test_slugify():
  17. """Test the slugify helper method."""
  18. assert slugify(u"Hello world") == u"hello-world"
  19. assert slugify(u"¿Cómo está?") == u"como-esta"
  20. def test_forum_is_unread(guest, user, forum, topic, forumsread):
  21. """Test the forum is unread function."""
  22. # for a guest
  23. assert not forum_is_unread(None, None, guest)
  24. # for a logged in user without a forumsread
  25. assert forum_is_unread(forum, None, user)
  26. # same, just with forumsread
  27. assert forum_is_unread(forum, forumsread, user)
  28. # lets mark the forum as read
  29. # but before we have to add an read entry in forumsread and topicsread
  30. topic.update_read(user, topic.forum, forumsread)
  31. time_read = dt.datetime.utcnow() - dt.timedelta(hours=1)
  32. forumsread.cleared = time_read # lets cheat here a bit :P
  33. forumsread.last_read = dt.datetime.utcnow()
  34. forumsread.save()
  35. assert not forum_is_unread(forum, forumsread, user)
  36. # read tracker is disabled
  37. flaskbb_config["TRACKER_LENGTH"] = 0
  38. assert not forum_is_unread(forum, forumsread, user)
  39. # there haven't been a post since TRACKER_LENGTH and thus the forum is read
  40. flaskbb_config["TRACKER_LENGTH"] = 1
  41. # this is cheating; don't do this.
  42. forum.last_post_created = forum.last_post_created - dt.timedelta(hours=48)
  43. forum.save()
  44. assert not forum_is_unread(forum, forumsread, user)
  45. # no topics in this forum
  46. topic.delete()
  47. forum = Forum.query.filter_by(id=forum.id).first()
  48. flaskbb_config["TRACKER_LENGTH"] = 1 # activate the tracker again
  49. assert forum.topic_count == 0
  50. assert not forum_is_unread(forum, None, user)
  51. def test_topic_is_unread(guest, user, forum, topic, topicsread, forumsread):
  52. # test guest
  53. assert not topic_is_unread(None, None, guest)
  54. # compare topicsread.last_read with topic.last_post.date_created
  55. assert topic_is_unread(topic, topicsread, user, forumsread)
  56. # TopicsRead is none and the forum has never been marked as read
  57. assert topic_is_unread(topic, topicsread=None, user=user, forumsread=forumsread)
  58. # lets mark the forum as read
  59. forumsread.cleared = time_utcnow()
  60. forumsread.last_read = time_utcnow()
  61. forumsread.save()
  62. assert not topic_is_unread(topic, topicsread=None, user=user, forumsread=forumsread)
  63. # disabled tracker
  64. flaskbb_config["TRACKER_LENGTH"] = 0
  65. assert not topic_is_unread(topic, None, user, None)
  66. # post is older than tracker length
  67. time_posted = time_utcnow() - dt.timedelta(days=2)
  68. flaskbb_config["TRACKER_LENGTH"] = 1
  69. topic.last_post.date_created = time_posted
  70. topic.last_updated = time_posted
  71. topic.save()
  72. assert not topic_is_unread(topic, None, user, None)
  73. def test_crop_title(default_settings):
  74. short_title = "Short title"
  75. long_title = "This is just a test title which is too long."
  76. assert crop_title(short_title) == short_title
  77. assert crop_title(long_title) == "This is just a..."
  78. def test_is_online(default_settings, user):
  79. assert is_online(user)
  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_jpg(responses, image_jpg):
  85. responses.add(image_jpg)
  86. jpg_img = get_image_info(image_jpg.url)
  87. assert jpg_img["content_type"] == "JPEG"
  88. assert jpg_img["height"] == 1024
  89. assert jpg_img["width"] == 1280
  90. assert jpg_img["size"] == 209.06
  91. def test_get_image_info_gif(responses, image_gif):
  92. responses.add(image_gif)
  93. gif_img = get_image_info(image_gif.url)
  94. assert gif_img["content_type"] == "GIF"
  95. assert gif_img["height"] == 168
  96. assert gif_img["width"] == 400
  97. assert gif_img["size"] == 576.138
  98. def test_get_image_info_png(responses, image_png):
  99. responses.add(image_png)
  100. png_img = get_image_info(image_png.url)
  101. assert png_img["content_type"] == "PNG"
  102. assert png_img["height"] == 1080
  103. assert png_img["width"] == 1920
  104. assert png_img["size"] == 269.409
  105. def assert_bad_image_check(result, mesg):
  106. assert not result[1]
  107. assert mesg in result[0]
  108. def test_check_image_too_big(image_too_big, default_settings, responses):
  109. responses.add(image_too_big)
  110. flaskbb_config["AVATAR_WIDTH"] = 1000
  111. flaskbb_config["AVATAR_HEIGHT"] = 1000
  112. assert_bad_image_check(check_image(image_too_big.url), "big")
  113. def test_check_image_too_tall(image_too_tall, default_settings, responses):
  114. responses.add(image_too_tall)
  115. assert_bad_image_check(check_image(image_too_tall.url), "high")
  116. def test_check_image_too_wide(image_too_wide, default_settings, responses):
  117. responses.add(image_too_wide)
  118. assert_bad_image_check(check_image(image_too_wide.url), "wide")
  119. def test_check_image_wrong_mime(image_wrong_mime, default_settings, responses):
  120. responses.add(image_wrong_mime)
  121. assert_bad_image_check(check_image(image_wrong_mime.url), "type")
  122. def test_check_image_just_right(image_just_right, default_settings, responses):
  123. responses.add(image_just_right)
  124. result = check_image(image_just_right.url)
  125. assert result[1]