test_helpers.py 5.7 KB

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