|
| 1 | +defmodule GroupherServer.Test.CMS.Hooks.NotifyBlog do |
| 2 | + use GroupherServer.TestTools |
| 3 | + |
| 4 | + import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1] |
| 5 | + |
| 6 | + alias GroupherServer.{CMS, Delivery} |
| 7 | + alias CMS.Delegate.Hooks |
| 8 | + |
| 9 | + setup do |
| 10 | + {:ok, user} = db_insert(:user) |
| 11 | + {:ok, user2} = db_insert(:user) |
| 12 | + {:ok, user3} = db_insert(:user) |
| 13 | + |
| 14 | + {:ok, community} = db_insert(:community) |
| 15 | + |
| 16 | + blog_attrs = mock_attrs(:blog, %{community_id: community.id}) |
| 17 | + {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) |
| 18 | + {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user) |
| 19 | + |
| 20 | + {:ok, ~m(user2 user3 blog comment)a} |
| 21 | + end |
| 22 | + |
| 23 | + describe "[upvote notify]" do |
| 24 | + @tag :wip2 |
| 25 | + test "upvote hook should work on blog", ~m(user2 blog)a do |
| 26 | + {:ok, blog} = preload_author(blog) |
| 27 | + |
| 28 | + {:ok, article} = CMS.upvote_article(:blog, blog.id, user2) |
| 29 | + Hooks.Notify.handle(:upvote, article, user2) |
| 30 | + |
| 31 | + {:ok, notifications} = |
| 32 | + Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20}) |
| 33 | + |
| 34 | + assert notifications.total_count == 1 |
| 35 | + |
| 36 | + notify = notifications.entries |> List.first() |
| 37 | + assert notify.action == "UPVOTE" |
| 38 | + assert notify.article_id == blog.id |
| 39 | + assert notify.thread == "BLOG" |
| 40 | + assert notify.user_id == blog.author.user.id |
| 41 | + assert user_exist_in?(user2, notify.from_users) |
| 42 | + end |
| 43 | + |
| 44 | + @tag :wip2 |
| 45 | + test "upvote hook should work on blog comment", ~m(user2 blog comment)a do |
| 46 | + {:ok, comment} = CMS.upvote_comment(comment.id, user2) |
| 47 | + {:ok, comment} = preload_author(comment) |
| 48 | + |
| 49 | + Hooks.Notify.handle(:upvote, comment, user2) |
| 50 | + |
| 51 | + {:ok, notifications} = |
| 52 | + Delivery.fetch(:notification, comment.author.id, %{page: 1, size: 20}) |
| 53 | + |
| 54 | + assert notifications.total_count == 1 |
| 55 | + |
| 56 | + notify = notifications.entries |> List.first() |
| 57 | + assert notify.action == "UPVOTE" |
| 58 | + assert notify.article_id == blog.id |
| 59 | + assert notify.thread == "BLOG" |
| 60 | + assert notify.user_id == comment.author.id |
| 61 | + assert notify.comment_id == comment.id |
| 62 | + assert user_exist_in?(user2, notify.from_users) |
| 63 | + end |
| 64 | + |
| 65 | + @tag :wip2 |
| 66 | + test "undo upvote hook should work on blog", ~m(user2 blog)a do |
| 67 | + {:ok, blog} = preload_author(blog) |
| 68 | + |
| 69 | + {:ok, article} = CMS.upvote_article(:blog, blog.id, user2) |
| 70 | + Hooks.Notify.handle(:upvote, article, user2) |
| 71 | + |
| 72 | + {:ok, article} = CMS.undo_upvote_article(:blog, blog.id, user2) |
| 73 | + Hooks.Notify.handle(:undo, :upvote, article, user2) |
| 74 | + |
| 75 | + {:ok, notifications} = |
| 76 | + Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20}) |
| 77 | + |
| 78 | + assert notifications.total_count == 0 |
| 79 | + end |
| 80 | + |
| 81 | + @tag :wip2 |
| 82 | + test "undo upvote hook should work on blog comment", ~m(user2 comment)a do |
| 83 | + {:ok, comment} = CMS.upvote_comment(comment.id, user2) |
| 84 | + |
| 85 | + Hooks.Notify.handle(:upvote, comment, user2) |
| 86 | + |
| 87 | + {:ok, comment} = CMS.undo_upvote_comment(comment.id, user2) |
| 88 | + Hooks.Notify.handle(:undo, :upvote, comment, user2) |
| 89 | + |
| 90 | + {:ok, comment} = preload_author(comment) |
| 91 | + |
| 92 | + {:ok, notifications} = |
| 93 | + Delivery.fetch(:notification, comment.author.id, %{page: 1, size: 20}) |
| 94 | + |
| 95 | + assert notifications.total_count == 0 |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + describe "[collect notify]" do |
| 100 | + @tag :wip2 |
| 101 | + test "collect hook should work on blog", ~m(user2 blog)a do |
| 102 | + {:ok, blog} = preload_author(blog) |
| 103 | + |
| 104 | + {:ok, _} = CMS.collect_article(:blog, blog.id, user2) |
| 105 | + Hooks.Notify.handle(:collect, blog, user2) |
| 106 | + |
| 107 | + {:ok, notifications} = |
| 108 | + Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20}) |
| 109 | + |
| 110 | + assert notifications.total_count == 1 |
| 111 | + |
| 112 | + notify = notifications.entries |> List.first() |
| 113 | + assert notify.action == "COLLECT" |
| 114 | + assert notify.article_id == blog.id |
| 115 | + assert notify.thread == "BLOG" |
| 116 | + assert notify.user_id == blog.author.user.id |
| 117 | + assert user_exist_in?(user2, notify.from_users) |
| 118 | + end |
| 119 | + |
| 120 | + @tag :wip2 |
| 121 | + test "undo collect hook should work on blog", ~m(user2 blog)a do |
| 122 | + {:ok, blog} = preload_author(blog) |
| 123 | + |
| 124 | + {:ok, _} = CMS.upvote_article(:blog, blog.id, user2) |
| 125 | + Hooks.Notify.handle(:collect, blog, user2) |
| 126 | + |
| 127 | + {:ok, _} = CMS.undo_upvote_article(:blog, blog.id, user2) |
| 128 | + Hooks.Notify.handle(:undo, :collect, blog, user2) |
| 129 | + |
| 130 | + {:ok, notifications} = |
| 131 | + Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20}) |
| 132 | + |
| 133 | + assert notifications.total_count == 0 |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + describe "[comment notify]" do |
| 138 | + @tag :wip |
| 139 | + test "blog author should get notify after some one comment on it", ~m(user2 blog)a do |
| 140 | + {:ok, blog} = preload_author(blog) |
| 141 | + |
| 142 | + {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user2) |
| 143 | + Hooks.Notify.handle(:comment, comment, user2) |
| 144 | + |
| 145 | + {:ok, notifications} = |
| 146 | + Delivery.fetch(:notification, blog.author.user.id, %{page: 1, size: 20}) |
| 147 | + |
| 148 | + assert notifications.total_count == 1 |
| 149 | + |
| 150 | + notify = notifications.entries |> List.first() |
| 151 | + assert notify.action == "COMMENT" |
| 152 | + assert notify.thread == "BLOG" |
| 153 | + assert notify.article_id == blog.id |
| 154 | + assert notify.user_id == blog.author.user.id |
| 155 | + assert user_exist_in?(user2, notify.from_users) |
| 156 | + end |
| 157 | + |
| 158 | + @tag :wip |
| 159 | + test "blog comment author should get notify after some one reply it", ~m(user2 user3 blog)a do |
| 160 | + {:ok, blog} = preload_author(blog) |
| 161 | + |
| 162 | + {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user2) |
| 163 | + {:ok, replyed_comment} = CMS.reply_comment(comment.id, mock_comment(), user3) |
| 164 | + |
| 165 | + Hooks.Notify.handle(:reply, replyed_comment, user3) |
| 166 | + |
| 167 | + {:ok, notifications} = |
| 168 | + Delivery.fetch(:notification, comment.author_id, %{page: 1, size: 20}) |
| 169 | + |
| 170 | + assert notifications.total_count == 1 |
| 171 | + |
| 172 | + notify = notifications.entries |> List.first() |
| 173 | + |
| 174 | + assert notify.action == "REPLY" |
| 175 | + assert notify.thread == "BLOG" |
| 176 | + assert notify.article_id == blog.id |
| 177 | + assert notify.comment_id == replyed_comment.id |
| 178 | + |
| 179 | + assert notify.user_id == comment.author_id |
| 180 | + assert user_exist_in?(user3, notify.from_users) |
| 181 | + end |
| 182 | + end |
| 183 | +end |
0 commit comments