diff --git a/config/config.exs b/config/config.exs index 5eedcd262..f3d93ff86 100644 --- a/config/config.exs +++ b/config/config.exs @@ -96,7 +96,8 @@ config :groupher_server, :article, :confused, :pill, :popcorn - ] + ], + digest_length: 120 config :groupher_server, GroupherServerWeb.Gettext, default_locale: "zh_CN", locales: ~w(en zh_CN) diff --git a/lib/groupher_server/cms/delegates/article_comment.ex b/lib/groupher_server/cms/delegates/article_comment.ex index ba1be04c9..8c102fbb1 100644 --- a/lib/groupher_server/cms/delegates/article_comment.ex +++ b/lib/groupher_server/cms/delegates/article_comment.ex @@ -11,7 +11,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do import ShortMaps alias Helper.Types, as: T - alias Helper.{ORM, QueryBuilder} + alias Helper.{ORM, QueryBuilder, Converter} alias GroupherServer.{Accounts, CMS, Repo} alias CMS.Model.Post @@ -92,13 +92,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do @doc """ creates a comment for article like psot, job ... """ - def create_article_comment(thread, article_id, content, %User{} = user) do + def create_article_comment(thread, article_id, body, %User{} = user) do with {:ok, info} <- match(thread), {:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]), true <- can_comment?(article, user) do Multi.new() |> Multi.run(:create_article_comment, fn _, _ -> - do_create_comment(content, info.foreign_key, article, user) + do_create_comment(body, info.foreign_key, article, user) end) |> Multi.run(:update_article_comments_count, fn _, %{create_article_comment: comment} -> update_article_comments_count(comment, :inc) @@ -133,15 +133,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do update a comment for article like psot, job ... """ # 如果是 solution, 那么要更新对应的 post 的 solution_digest - def update_article_comment(%ArticleComment{is_solution: true} = article_comment, content) do - with {:ok, post} <- ORM.find(Post, article_comment.post_id) do - post |> ORM.update(%{solution_digest: content}) - article_comment |> ORM.update(%{body_html: content}) + def update_article_comment(%ArticleComment{is_solution: true} = article_comment, body) do + with {:ok, post} <- ORM.find(Post, article_comment.post_id), + {:ok, parsed} <- Converter.Article.parse_body(body), + {:ok, digest} <- Converter.Article.parse_digest(parsed.body_map) do + %{body: body, body_html: body_html} = parsed + post |> ORM.update(%{solution_digest: digest}) + article_comment |> ORM.update(%{body: body, body_html: body_html}) end end - def update_article_comment(%ArticleComment{} = article_comment, content) do - article_comment |> ORM.update(%{body_html: content}) + def update_article_comment(%ArticleComment{} = article_comment, body) do + with {:ok, %{body: body, body_html: body_html}} <- Converter.Article.parse_body(body) do + article_comment |> ORM.update(%{body: body, body_html: body_html}) + end end @doc """ @@ -216,7 +221,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do |> result() end - # add participator to article-like content (Post, Job ...) and update count + # add participator to article-like(Post, Job ...) and update count def add_participator_to_article( %{article_comments_participators: article_comments_participators} = article, %User{} = user @@ -255,32 +260,27 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do end end - # creat article comment for parent or reply - # set floor - # TODO: parse editor-json - # set default emotions - def do_create_comment(content, foreign_key, article, %User{id: user_id}) do - thread = foreign_key |> to_string |> String.split("_id") |> List.first() |> String.upcase() - - count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article.id) - floor = Repo.aggregate(count_query, :count) + 1 - - ArticleComment - |> ORM.create( - Map.put( - %{ - author_id: user_id, - body_html: content, - emotions: @default_emotions, - floor: floor, - is_article_author: user_id == article.author.user.id, - thread: thread, - meta: @default_comment_meta - }, - foreign_key, - article.id - ) - ) + @doc """ + create article comment for parent or reply + """ + def do_create_comment(body, foreign_key, article, %User{id: user_id}) do + with {:ok, %{body: body, body_html: body_html}} <- Converter.Article.parse_body(body) do + # e.g: :post_id -> "POST", :job_id -> "JOB" + thread = foreign_key |> to_string |> String.slice(0..-4) |> String.upcase() + + attrs = %{ + author_id: user_id, + body: body, + body_html: body_html, + emotions: @default_emotions, + floor: get_comment_floor(article, foreign_key), + is_article_author: user_id == article.author.user.id, + thread: thread, + meta: @default_comment_meta + } + + ArticleComment |> ORM.create(Map.put(attrs, foreign_key, article.id)) + end end defp do_paged_article_comment(thread, article_id, filters, where_query, user) do @@ -398,6 +398,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do {:ok, :pass} end + # get next floor under an article's comments list + defp get_comment_floor(article, foreign_key) do + count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article.id) + Repo.aggregate(count_query, :count) + 1 + end + defp result({:ok, %{set_question_flag_ifneed: result}}), do: {:ok, result} defp result({:ok, %{delete_article_comment: result}}), do: {:ok, result} defp result({:ok, %{mark_solution: result}}), do: {:ok, result} diff --git a/lib/groupher_server/cms/delegates/article_curd.ex b/lib/groupher_server/cms/delegates/article_curd.ex index 0ca271631..21e5fce72 100644 --- a/lib/groupher_server/cms/delegates/article_curd.ex +++ b/lib/groupher_server/cms/delegates/article_curd.ex @@ -6,22 +6,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do import GroupherServer.CMS.Helper.Matcher - import Helper.Utils, - only: [ - done: 1, - pick_by: 2, - integerfy: 1, - strip_struct: 1, - module_to_atom: 1, - get_config: 2, - ensure: 2 - ] + import Helper.Utils, only: [done: 1, pick_by: 2, module_to_atom: 1, get_config: 2, ensure: 2] import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2] import Helper.ErrorCode import ShortMaps - alias Helper.{Later, ORM, QueryBuilder} + alias Helper.{Later, ORM, QueryBuilder, Converter} alias GroupherServer.{Accounts, CMS, Delivery, Email, Repo, Statistics} alias Accounts.Model.User @@ -172,7 +163,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do |> Multi.run(:update_user_published_meta, fn _, _ -> Accounts.update_published_states(uid, thread) end) + # TODO: run mini tasks |> Multi.run(:mention_users, fn _, %{create_article: article} -> + # article.body |> Jason.decode!() |> 各种小 task Delivery.mention_from_content(community.raw, thread, article, attrs, %User{id: uid}) {:ok, :pass} end) @@ -213,7 +206,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do """ def update_article(article, args) do Multi.new() - |> Multi.run(:update_article, fn _, _ -> ORM.update(article, args) end) + |> Multi.run(:update_article, fn _, _ -> + do_update_article(article, args) + end) |> Multi.run(:update_comment_question_flag_if_need, fn _, %{update_article: update_article} -> # 如果帖子的类型变了,那么 update 所有的 flag case Map.has_key?(args, :is_question) do @@ -386,17 +381,43 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do end # for create artilce step in Multi.new - defp do_create_article(target, attrs, %Author{id: aid}, %Community{id: cid}) do - target - |> struct() - |> target.changeset(attrs) - |> Ecto.Changeset.put_change(:emotions, @default_emotions) - |> Ecto.Changeset.put_change(:author_id, aid) - |> Ecto.Changeset.put_change(:original_community_id, integerfy(cid)) - |> Ecto.Changeset.put_embed(:meta, @default_article_meta) - |> Repo.insert() + defp do_create_article(model, attrs, %Author{id: author_id}, %Community{id: community_id}) do + # special article like Repo do not have :body, assign it with default-empty rich text + body = Map.get(attrs, :body, Converter.Article.default_rich_text()) + attrs = attrs |> Map.merge(%{body: body}) + + with {:ok, attrs} <- add_rich_text_attrs(attrs) do + model.__struct__ + |> model.changeset(attrs) + |> Ecto.Changeset.put_change(:emotions, @default_emotions) + |> Ecto.Changeset.put_change(:author_id, author_id) + |> Ecto.Changeset.put_change(:original_community_id, community_id) + |> Ecto.Changeset.put_embed(:meta, @default_article_meta) + |> Repo.insert() + end end + defp do_update_article(article, %{body: _} = attrs) do + with {:ok, attrs} <- add_rich_text_attrs(attrs) do + ORM.update(article, attrs) + end + end + + defp do_update_article(article, attrs), do: ORM.update(article, attrs) + + # is update or create article with body field, parsed and extand it into attrs + defp add_rich_text_attrs(%{body: body} = attrs) when not is_nil(body) do + with {:ok, parsed} <- Converter.Article.parse_body(body), + {:ok, digest} <- Converter.Article.parse_digest(parsed.body_map) do + attrs + |> Map.merge(Map.take(parsed, [:body, :body_html])) + |> Map.merge(%{digest: digest}) + |> done + end + end + + defp add_rich_text_attrs(attrs), do: attrs + # except Job, other article will just pass, should use set_article_tags function instead # defp exec_update_tags(_, _tags_ids), do: {:ok, :pass} @@ -413,7 +434,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do case Enum.empty?(meta.viewed_user_ids) or user_not_viewed do true -> new_ids = Enum.uniq([user_id] ++ meta.viewed_user_ids) - meta = meta |> Map.merge(%{viewed_user_ids: new_ids}) |> strip_struct + meta = meta |> Map.merge(%{viewed_user_ids: new_ids}) ORM.update_meta(article, meta) false -> @@ -429,11 +450,16 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do defp result({:ok, %{update_edit_status: result}}), do: {:ok, result} defp result({:ok, %{update_article: result}}), do: {:ok, result} + # NOTE: for read article, order is import defp result({:ok, %{set_viewer_has_states: result}}), do: result |> done() defp result({:ok, %{update_article_meta: result}}), do: {:ok, result} defp result({:error, :create_article, _result, _steps}) do - {:error, [message: "create cms article author", code: ecode(:create_fails)]} + {:error, [message: "create article", code: ecode(:create_fails)]} + end + + defp result({:error, :update_article, _result, _steps}) do + {:error, [message: "update article", code: ecode(:update_fails)]} end defp result({:error, :mirror_article, _result, _steps}) do diff --git a/lib/groupher_server/cms/helper/macros.ex b/lib/groupher_server/cms/helper/macros.ex index 5ee406b72..9d542d641 100644 --- a/lib/groupher_server/cms/helper/macros.ex +++ b/lib/groupher_server/cms/helper/macros.ex @@ -125,6 +125,7 @@ defmodule GroupherServer.CMS.Helper.Macros do def general_article_fields(:cast) do [ :body, + :body_html, :digest, :original_community_id, :article_comments_count, @@ -187,6 +188,7 @@ defmodule GroupherServer.CMS.Helper.Macros do quote do field(:title, :string) field(:body, :string) + field(:body_html, :string) field(:digest, :string) belongs_to(:author, Author) diff --git a/lib/groupher_server/cms/models/article_comment.ex b/lib/groupher_server/cms/models/article_comment.ex index d75f12380..c0563bb50 100644 --- a/lib/groupher_server/cms/models/article_comment.ex +++ b/lib/groupher_server/cms/models/article_comment.ex @@ -17,9 +17,9 @@ defmodule GroupherServer.CMS.Model.ArticleComment do # alias Helper.HTML @article_threads get_config(:article, :threads) - @required_fields ~w(body_html author_id)a - @optional_fields ~w(reply_to_id replies_count is_folded is_deleted floor is_article_author thread is_for_question is_solution)a - @updatable_fields ~w(is_folded is_deleted floor upvotes_count is_pinned is_for_question is_solution)a + @required_fields ~w(body author_id)a + @optional_fields ~w(body_html reply_to_id replies_count is_folded is_deleted floor is_article_author thread is_for_question is_solution)a + @updatable_fields ~w(body_html is_folded is_deleted floor upvotes_count is_pinned is_for_question is_solution)a @article_fields @article_threads |> Enum.map(&:"#{&1}_id") @@ -52,6 +52,7 @@ defmodule GroupherServer.CMS.Model.ArticleComment do belongs_to(:author, User, foreign_key: :author_id) field(:thread, :string) + field(:body, :string) field(:body_html, :string) # 是否被折叠 field(:is_folded, :boolean, default: false) diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index df7f0f7f7..fc3b697ce 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -69,8 +69,8 @@ defmodule GroupherServerWeb.Resolvers.CMS do CMS.create_article(%Community{id: community_id}, thread, args, user) end - def update_article(_root, %{passport_source: content} = args, _info) do - CMS.update_article(content, args) + def update_article(_root, %{passport_source: article} = args, _info) do + CMS.update_article(article, args) end def delete_article(_root, %{passport_source: content}, _info), do: ORM.delete(content) @@ -294,13 +294,13 @@ defmodule GroupherServerWeb.Resolvers.CMS do CMS.paged_article_comments_participators(thread, id, filter) end - def create_article_comment(_root, ~m(thread id content)a, %{context: %{cur_user: user}}) do - CMS.create_article_comment(thread, id, content, user) + def create_article_comment(_root, ~m(thread id body)a, %{context: %{cur_user: user}}) do + CMS.create_article_comment(thread, id, body, user) end - def update_article_comment(_root, ~m(content passport_source)a, _info) do + def update_article_comment(_root, ~m(body passport_source)a, _info) do comment = passport_source - CMS.update_article_comment(comment, content) + CMS.update_article_comment(comment, body) end def delete_article_comment(_root, ~m(passport_source)a, _info) do @@ -308,8 +308,8 @@ defmodule GroupherServerWeb.Resolvers.CMS do CMS.delete_article_comment(comment) end - def reply_article_comment(_root, ~m(id content)a, %{context: %{cur_user: user}}) do - CMS.reply_article_comment(id, content, user) + def reply_article_comment(_root, ~m(id body)a, %{context: %{cur_user: user}}) do + CMS.reply_article_comment(id, body, user) end def upvote_article_comment(_root, ~m(id)a, %{context: %{cur_user: user}}) do diff --git a/lib/groupher_server_web/schema/Helper/fields.ex b/lib/groupher_server_web/schema/Helper/fields.ex index edd51ce39..46d73b3d8 100644 --- a/lib/groupher_server_web/schema/Helper/fields.ex +++ b/lib/groupher_server_web/schema/Helper/fields.ex @@ -19,6 +19,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do field(:id, :id) field(:title, :string) field(:body, :string) + field(:body_html, :string) field(:digest, :string) field(:views, :integer) field(:is_pinned, :boolean) diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index 31042e8ec..509a1f1f4 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -61,7 +61,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:length, :integer) field(:link_addr, :string) field(:copy_right, :string) - field(:body, :string) timestamp_fields(:article) end @@ -78,7 +77,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:length, :integer) field(:link_addr, :string) field(:copy_right, :string) - field(:body, :string) timestamp_fields(:article) end @@ -91,7 +89,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:length, :integer) field(:link_addr, :string) - # field(:body, :string) timestamp_fields(:article) end @@ -259,6 +256,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do object :article_comment_reply do field(:id, :id) + field(:body, :string) field(:body_html, :string) field(:author, :user, resolve: dataloader(CMS, :author)) field(:floor, :integer) diff --git a/lib/groupher_server_web/schema/cms/mutations/comment.ex b/lib/groupher_server_web/schema/cms/mutations/comment.ex index 3ef9ecc1f..26da702f6 100644 --- a/lib/groupher_server_web/schema/cms/mutations/comment.ex +++ b/lib/groupher_server_web/schema/cms/mutations/comment.ex @@ -10,7 +10,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do # TODO use thread and force community pass-in arg(:thread, :thread, default_value: :post) arg(:id, non_null(:id)) - arg(:content, non_null(:string)) + arg(:body, non_null(:string)) # arg(:mention_users, list_of(:ids)) # TDOO: use a comment resolver @@ -23,7 +23,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do @desc "update a comment" field :update_article_comment, :article_comment do arg(:id, non_null(:id)) - arg(:content, non_null(:string)) + arg(:body, non_null(:string)) # arg(:mention_users, list_of(:ids)) middleware(M.Authorize, :login) @@ -47,7 +47,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do @desc "reply to a comment" field :reply_article_comment, :article_comment do arg(:id, non_null(:id)) - arg(:content, non_null(:string)) + arg(:body, non_null(:string)) middleware(M.Authorize, :login) # TODO: 文章作者可以删除评论,文章可以设置禁止评论 diff --git a/lib/helper/converter/article.ex b/lib/helper/converter/article.ex new file mode 100644 index 000000000..f8651f94d --- /dev/null +++ b/lib/helper/converter/article.ex @@ -0,0 +1,86 @@ +defmodule Helper.Converter.Article do + @moduledoc """ + convert body + + {:ok, { body: body, body_html: body_html }} = Converter.Article.parse_body(body) + """ + import Helper.Utils, only: [done: 1, uid: 0, keys_to_strings: 1, get_config: 2] + + alias Helper.Converter.{EditorToHTML, HtmlSanitizer} + + @article_digest_length get_config(:article, :digest_length) + + def default_rich_text() do + """ + { + "time": 2018, + "blocks": [], + "version": "2.22.0" + } + """ + end + + @doc """ + parse article body field + """ + @spec parse_body(String.t()) :: + {:ok, %{body: String.t(), body_map: Map.t(), body_html: String.t()}} + def parse_body(body) when is_binary(body) do + with {:ok, body_map} <- to_editor_map(body), + {:ok, body_html} <- EditorToHTML.to_html(body_map), + {:ok, body_encode} <- Jason.encode(body_map) do + %{body: body_encode, body_html: body_html, body_map: body_map} |> done + end + end + + def parse_body(_), do: {:error, "wrong body fmt"} + + @doc """ + parse digest by concat all the paragraph blocks + """ + def parse_digest(%{"blocks" => blocks} = body_map) when is_map(body_map) do + paragraph_blocks = Enum.filter(blocks, &(&1["type"] == "paragraph")) + + Enum.reduce(paragraph_blocks, "", fn block, acc -> + text = block["data"]["text"] |> HtmlSanitizer.strip_all_tags() + acc <> text <> " " + end) + |> String.trim_trailing() + |> String.slice(0, @article_digest_length) + |> done + end + + def parse_digest(_), do: {:ok, "unknow digest"} + + @doc """ + decode article body string to editor map and assign id for each block + """ + def to_editor_map(string) when is_binary(string) do + with {:ok, map} <- Jason.decode(string), + {:ok, _} <- EditorToHTML.Validator.is_valid(map) do + blocks = Enum.map(map["blocks"], &Map.merge(&1, %{"id" => get_block_id(&1)})) + Map.merge(map, %{"blocks" => blocks}) |> done + end + end + + # for markdown blocks + def to_editor_map(blocks) when is_list(blocks) do + Enum.map(blocks, fn block -> + block = keys_to_strings(block) + Map.merge(block, %{"id" => get_block_id(block)}) + end) + |> done + end + + def to_editor_map(_), do: {:error, "wrong editor fmt"} + + # use custom block id instead of editor.js's default block id + defp get_block_id(%{"id" => id} = block) when not is_nil(id) do + case String.starts_with?(block["id"], "block-") do + true -> id + false -> "block-#{uid()}" + end + end + + defp get_block_id(_), do: "block-#{uid()}" +end diff --git a/lib/helper/converter/editor_to_html/frags/header.ex b/lib/helper/converter/editor_to_html/frags/header.ex index 1acf4968d..5169ae53f 100644 --- a/lib/helper/converter/editor_to_html/frags/header.ex +++ b/lib/helper/converter/editor_to_html/frags/header.ex @@ -5,47 +5,40 @@ defmodule Helper.Converter.EditorToHTML.Frags.Header do see https://editorjs.io/ """ alias Helper.Types, as: T - alias Helper.Utils - alias Helper.Converter.EditorToHTML.Class @class get_in(Class.article(), ["header"]) - @spec get(T.editor_header()) :: T.html() - def get(%{"eyebrowTitle" => eyebrow_title, "footerTitle" => footer_title} = data) do + @spec get(String.t(), T.editor_header()) :: T.html() + def get(id, %{"eyebrowTitle" => eyebrow_title, "footerTitle" => footer_title} = data) do %{"text" => text, "level" => level} = data - anchor_id = Utils.uid(:html, data) - ~s(
+ @spec get(String.t(), T.editor_quote()) :: T.html() + def get(id, %{"mode" => "short", "text" => text} = data) do + ~s() end - def get(%{"mode" => "long", "text" => text, "caption" => caption} = data) + def get(id, %{"mode" => "long", "text" => text, "caption" => caption} = data) when g_none_empty_str(caption) do caption_content = frag(:caption, caption) - anchor_id = Utils.uid(:html, data) - ~s(#{text}+ ~s() end - def get(%{"mode" => "long", "text" => text}) do - ~s(#{text}#{caption_content}+ def get(id, %{"mode" => "long", "text" => text}) do + ~s() end diff --git a/lib/helper/converter/editor_to_html/index.ex b/lib/helper/converter/editor_to_html/index.ex index d2aa75fa5..2d3d7dacd 100644 --- a/lib/helper/converter/editor_to_html/index.ex +++ b/lib/helper/converter/editor_to_html/index.ex @@ -8,19 +8,38 @@ defmodule Helper.Converter.EditorToHTML do alias Helper.Types, as: T alias Helper.Utils - alias Helper.Converter.{EditorToHTML, HtmlSanitizer} - alias EditorToHTML.{Class, Frags, Validator} + alias Helper.Converter.{Article, EditorToHTML, HtmlSanitizer} + alias EditorToHTML.{Class, Frags} # alias EditorToHTML.Assets.{DelimiterIcons} @root_class Class.article() + @spec to_html(Map.t()) :: {:ok, T.html()} + def to_html(editor_map) when is_map(editor_map) do + content = + Enum.reduce(editor_map["blocks"], "", fn block, acc -> + clean_html = block |> parse_block |> HtmlSanitizer.sanitize() + acc <> clean_html + end) + + viewer_class = @root_class["viewer"] + {:ok, ~s(#{text}#{content})} + end + @spec to_html(String.t()) :: {:ok, T.html()} def to_html(string) when is_binary(string) do - with {:ok, parsed} = string_to_json(string), - {:ok, _} <- Validator.is_valid(parsed) do + with {:ok, editor_map} <- Article.to_editor_map(string) do + to_html(editor_map) + end + end + + @doc "used for markdown ast to editor" + def to_html(editor_blocks) when is_list(editor_blocks) do + with {:ok, editor_blocks} <- Article.to_editor_map(editor_blocks) do content = - Enum.reduce(parsed["blocks"], "", fn block, acc -> + Enum.reduce(editor_blocks, "", fn block, acc -> clean_html = block |> parse_block |> HtmlSanitizer.sanitize() + acc <> clean_html end) @@ -29,25 +48,19 @@ defmodule Helper.Converter.EditorToHTML do end end - @doc "used for markdown ast to editor" - def to_html(editor_blocks) when is_list(editor_blocks) do - content = - Enum.reduce(editor_blocks, "", fn block, acc -> - clean_html = block |> Utils.keys_to_strings() |> parse_block |> HtmlSanitizer.sanitize() - acc <> clean_html - end) - - viewer_class = @root_class["viewer"] - {:ok, ~s(#{content})} + defp parse_block(%{"id" => id, "type" => "paragraph", "data" => %{"text" => text}}) do + ~s(#{text}
) end - defp parse_block(%{"type" => "paragraph", "data" => %{"text" => text}}), do: "#{text}
" - - defp parse_block(%{"type" => "header", "data" => data}), do: Frags.Header.get(data) + defp parse_block(%{"id" => id, "type" => "header", "data" => data}) do + Frags.Header.get(id, data) + end - defp parse_block(%{"type" => "quote", "data" => data}), do: Frags.Quote.get(data) + defp parse_block(%{"id" => id, "type" => "quote", "data" => data}) do + Frags.Quote.get(id, data) + end - defp parse_block(%{"type" => "list", "data" => data}) do + defp parse_block(%{"id" => id, "type" => "list", "data" => data}) do %{"items" => items, "mode" => mode} = data list_wrapper_class = get_in(@root_class, ["list", "wrapper"]) @@ -57,11 +70,10 @@ defmodule Helper.Converter.EditorToHTML do acc <> Frags.List.get_item(mode |> String.to_atom(), item) end) - anchor_id = Utils.uid(:html, data) - ~s(#{items_content}) + ~s(#{items_content}) end - defp parse_block(%{"type" => "table", "data" => data}) do + defp parse_block(%{"id" => id, "type" => "table", "data" => data}) do %{"items" => items, "columnCount" => column_count} = data # IO.inspect(column_count, label: "the fuck column_count") @@ -75,9 +87,7 @@ defmodule Helper.Converter.EditorToHTML do table_wrapper_class = get_in(@root_class, ["table", "wrapper"]) - anchor_id = Utils.uid(:html, data) - - ~s(+ ~s(#{rows_content} @@ -86,7 +96,7 @@ defmodule Helper.Converter.EditorToHTML do ) end - defp parse_block(%{"type" => "image", "data" => %{"mode" => "single"} = data}) do + defp parse_block(%{"id" => id, "type" => "image", "data" => %{"mode" => "single"} = data}) do %{"items" => items} = data image_wrapper_class = get_in(@root_class, ["image", "wrapper"]) @@ -94,12 +104,10 @@ defmodule Helper.Converter.EditorToHTML do items_content = Frags.Image.get_item(:single, List.first(items)) caption_content = Frags.Image.get_caption(:html, List.first(items)) - anchor_id = Utils.uid(:html, data) - - ~s(
#{items_content}#{caption_content}) + ~s(#{items_content}#{caption_content}) end - defp parse_block(%{"type" => "image", "data" => %{"mode" => "jiugongge"} = data}) do + defp parse_block(%{"id" => id, "type" => "image", "data" => %{"mode" => "jiugongge"} = data}) do %{"items" => items} = data image_wrapper_class = get_in(@root_class, ["image", "wrapper"]) @@ -110,16 +118,14 @@ defmodule Helper.Converter.EditorToHTML do acc <> Frags.Image.get_item(:jiugongge, item) end) - anchor_id = Utils.uid(:html, data) - - ~s(+ ~s() end - defp parse_block(%{"type" => "image", "data" => %{"mode" => "gallery"} = data}) do + defp parse_block(%{"id" => id, "type" => "image", "data" => %{"mode" => "gallery"} = data}) do %{"items" => items} = data image_wrapper_class = get_in(@root_class, ["image", "wrapper"]) @@ -133,9 +139,7 @@ defmodule Helper.Converter.EditorToHTML do minimap_content = Frags.Image.get_minimap(items) - anchor_id = Utils.uid(:html, data) - - ~s(#{items_content}+ ~s(#{items_content} @@ -145,7 +149,7 @@ defmodule Helper.Converter.EditorToHTML do) end - defp parse_block(%{"type" => "people", "data" => %{"mode" => "gallery"} = data}) do + defp parse_block(%{"id" => id, "type" => "people", "data" => %{"mode" => "gallery"} = data}) do %{"items" => items} = data # set id to each people for switch them @@ -157,9 +161,7 @@ defmodule Helper.Converter.EditorToHTML do previewer_content = Frags.People.get_previewer(:gallery, items) card_content = Frags.People.get_card(:gallery, items) - anchor_id = Utils.uid(:html, data) - - ~s(+ ~s(#{previewer_content} #{card_content} @@ -167,19 +169,16 @@ defmodule Helper.Converter.EditorToHTML do) end - defp parse_block(%{"type" => "code", "data" => data}) do + defp parse_block(%{"id" => id, "type" => "code", "data" => data}) do text = get_in(data, ["text"]) code = text |> Phoenix.HTML.html_escape() |> Phoenix.HTML.safe_to_string() lang = get_in(data, ["lang"]) - "" - # |> IO.inspect(label: "code ret") + ~s(#{code}) end defp parse_block(_block) do undown_block_class = @root_class["unknow_block"] ~s("#{code}[unknow block]") end - - def string_to_json(string), do: Jason.decode(string) end diff --git a/lib/helper/converter/html_sanitizer.ex b/lib/helper/converter/html_sanitizer.ex index 476c014e3..b3996f4ad 100644 --- a/lib/helper/converter/html_sanitizer.ex +++ b/lib/helper/converter/html_sanitizer.ex @@ -14,7 +14,7 @@ defmodule Helper.Converter.HtmlSanitizer do Meta.strip_comments() Meta.allow_tag_with_uri_attributes("a", ["href"], ["http", "https"]) - Meta.allow_tag_with_these_attributes("a", ["name", "title", "class", "data-glightbox"]) + Meta.allow_tag_with_these_attributes("a", ["id", "name", "title", "class", "data-glightbox"]) # Meta.allow_tag_with_these_attributes("strong", []) # Meta.allow_tag_with_these_attributes("em", []) @@ -22,33 +22,42 @@ defmodule Helper.Converter.HtmlSanitizer do Meta.allow_tag_with_these_attributes("i", []) Meta.allow_tag_with_these_attributes("mark", ["class"]) - Meta.allow_tag_with_these_attributes("code", ["class"]) - Meta.allow_tag_with_these_attributes("pre", ["class"]) + Meta.allow_tag_with_these_attributes("code", ["id", "class"]) + Meta.allow_tag_with_these_attributes("pre", ["id", "class"]) # Meta.allow_tag_with_these_attributes("p", []) - Meta.allow_tag_with_these_attributes("h1", ["class"]) - Meta.allow_tag_with_these_attributes("h2", ["class"]) - Meta.allow_tag_with_these_attributes("h3", ["class"]) + Meta.allow_tag_with_these_attributes("h1", ["id", "class"]) + Meta.allow_tag_with_these_attributes("h2", ["id", "class"]) + Meta.allow_tag_with_these_attributes("h3", ["id", "class"]) # Meta.allow_tag_with_these_attributes("h4", ["class"]) # Meta.allow_tag_with_these_attributes("h5", ["class"]) # Meta.allow_tag_with_these_attributes("h6", ["class"]) - Meta.allow_tag_with_these_attributes("p", ["class"]) - Meta.allow_tag_with_these_attributes("img", ["class", "src", "style", "alt", "data-index"]) + Meta.allow_tag_with_these_attributes("p", ["id", "class"]) + + Meta.allow_tag_with_these_attributes("img", [ + "id", + "class", + "src", + "style", + "alt", + "data-index" + ]) + Meta.allow_tag_with_these_attributes("div", ["id", "class", "data-index"]) - Meta.allow_tag_with_these_attributes("ul", ["class"]) - Meta.allow_tag_with_these_attributes("ol", ["class"]) - Meta.allow_tag_with_these_attributes("li", ["class"]) + Meta.allow_tag_with_these_attributes("ul", ["id", "class"]) + Meta.allow_tag_with_these_attributes("ol", ["id", "class"]) + Meta.allow_tag_with_these_attributes("li", ["id", "class"]) # table - Meta.allow_tag_with_these_attributes("table", []) - Meta.allow_tag_with_these_attributes("tbody", []) - Meta.allow_tag_with_these_attributes("tr", []) - Meta.allow_tag_with_these_attributes("th", ["class"]) - Meta.allow_tag_with_these_attributes("td", ["class", "style"]) + Meta.allow_tag_with_these_attributes("table", ["id"]) + Meta.allow_tag_with_these_attributes("tbody", ["id"]) + Meta.allow_tag_with_these_attributes("tr", ["id"]) + Meta.allow_tag_with_these_attributes("th", ["id", "class"]) + Meta.allow_tag_with_these_attributes("td", ["id", "class", "style"]) # blockquote Meta.allow_tag_with_these_attributes("blockquote", ["id", "class"]) - Meta.allow_tag_with_these_attributes("image", ["xlink:href"]) + Meta.allow_tag_with_these_attributes("image", ["id", "xlink:href"]) Meta.allow_tag_with_these_attributes("svg", [ "t", @@ -94,4 +103,9 @@ defmodule Helper.Converter.HtmlSanitizer do # workarround for https://github.com/rrrene/html_sanitize_ex/issues/48 |> String.replace(" viewbox=\"", " viewBox=\"") end + + @doc """ + strip all html tags + """ + def strip_all_tags(text), do: text |> HtmlSanitizeEx.strip_tags() end diff --git a/lib/helper/html.ex b/lib/helper/html.ex index 6087c7cae..328d8d40e 100644 --- a/lib/helper/html.ex +++ b/lib/helper/html.ex @@ -8,12 +8,8 @@ defmodule Helper.HTML do def safe_string(%Ecto.Changeset{valid?: true, changes: changes} = changeset, field) do case Map.has_key?(changes, field) do - true -> - changeset - |> put_change(field, escape_to_safe_string(changes[field])) - - _ -> - changeset + true -> changeset |> put_change(field, escape_to_safe_string(changes[field])) + _ -> changeset end end diff --git a/lib/helper/utils/utils.ex b/lib/helper/utils/utils.ex index 62a978c72..18885476b 100644 --- a/lib/helper/utils/utils.ex +++ b/lib/helper/utils/utils.ex @@ -201,6 +201,10 @@ defmodule Helper.Utils do end end + def uid(str_len \\ 5) do + Nanoid.generate(str_len, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + end + @doc "html uniq id generator for editorjs" @spec uid(:html, map) :: String.t() def uid(:html, %{"id" => id}) when g_none_empty_str(id), do: id diff --git a/lib/helper/validator/schema.ex b/lib/helper/validator/schema.ex index d30417937..e5cabd944 100644 --- a/lib/helper/validator/schema.ex +++ b/lib/helper/validator/schema.ex @@ -93,33 +93,24 @@ defmodule Helper.Validator.Schema do defp match(field, value, type, [{:min, min} | options]) when type in @support_min and g_not_nil(value) and g_pos_int(min) do case Utils.large_than(value, min) do - true -> - match(field, value, type, options) - - false -> - error(field, value, :min, min) + true -> match(field, value, type, options) + false -> error(field, value, :min, min) end end ## starts_with option for string defp match(field, value, type, [{:starts_with, starts} | options]) when is_binary(value) do case String.starts_with?(value, starts) do - true -> - match(field, value, type, options) - - false -> - error(field, value, :starts_with, starts) + true -> match(field, value, type, options) + false -> error(field, value, :starts_with, starts) end end ## item type for list defp match(field, value, type, [{:type, :map} | options]) when is_list(value) do case Enum.all?(value, &is_map(&1)) do - true -> - match(field, value, type, options) - - false -> - error(field, value, :list_type_map) + true -> match(field, value, type, options) + false -> error(field, value, :list_type_map) end end diff --git a/mix.exs b/mix.exs index b4ea986b3..c54d973e2 100644 --- a/mix.exs +++ b/mix.exs @@ -107,7 +107,8 @@ defmodule GroupherServer.Mixfile do # 遵循中文排版指南 # https://github.com/cataska/pangu.ex {:pangu, "~> 0.1.0"}, - {:accessible, "~> 0.3.0"} + {:accessible, "~> 0.3.0"}, + {:floki, "~> 0.30.1"} ] end diff --git a/mix.lock b/mix.lock index 1e80ecb3c..b356ab1b0 100644 --- a/mix.lock +++ b/mix.lock @@ -36,12 +36,14 @@ "excoveralls": {:hex, :excoveralls, "0.14.1", "14140e4ef343f2af2de33d35268c77bc7983d7824cb945e6c2af54235bc2e61f", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "4a588f9f8cf9dc140cc1f3d0ea4d849b2f76d5d8bee66b73c304bb3d3689c8b0"}, "faker": {:hex, :faker, "0.16.0", "1e2cf3e8d60d44a30741fb98118fcac18b2020379c7e00d18f1a005841b2f647", [:mix], [], "hexpm", "fbcb9bf1299dff3c9dd7e50f41802bbc472ffbb84e7656394c8aa913ec315141"}, "file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"}, + "floki": {:hex, :floki, "0.30.1", "75d35526d3a1459920b6e87fdbc2e0b8a3670f965dd0903708d2b267e0904c55", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "e9c03524447d1c4cbfccd672d739b8c18453eee377846b119d4fd71b1a176bb8"}, "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], [], "hexpm"}, "gen_stage": {:hex, :gen_stage, "0.14.2", "6a2a578a510c5bfca8a45e6b27552f613b41cf584b58210f017088d3d17d0b14", [:mix], [], "hexpm", "1f201083ca2ee1ea2b8e1eb6e98d9842ef93f2e5efa2d602740ab0c56c2bc90b"}, "gen_state_machine": {:hex, :gen_state_machine, "2.0.5", "9ac15ec6e66acac994cc442dcc2c6f9796cf380ec4b08267223014be1c728a95", [:mix], [], "hexpm", "5cacd405e72b2609a7e1f891bddb80c53d0b3b7b0036d1648e7382ca108c41c8"}, "gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"}, "guardian": {:hex, :guardian, "2.1.1", "1f02b349f6ba765647cc834036a8d76fa4bd65605342fe3a031df3c99d0d411a", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "189b87ba7ce6b40d6ba029138098b96ffc4ae78f229f5b39539b9141af8bf0f8"}, "hackney": {:hex, :hackney, "1.17.4", "99da4674592504d3fb0cfef0db84c3ba02b4508bae2dff8c0108baa0d6e0977c", [:rebar3], [{:certifi, "~>2.6.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "de16ff4996556c8548d512f4dbe22dd58a587bf3332e7fd362430a7ef3986b16"}, + "html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"}, "html_sanitize_ex": {:hex, :html_sanitize_ex, "1.4.1", "e8a67da405fe9f0d1be121a40a60f70811192033a5b8d00a95dddd807f5e053e", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "68d92656f47cd73598c45ad2394561f025c8c65d146001b955fd7b517858962a"}, "httpoison": {:hex, :httpoison, "1.6.2", "ace7c8d3a361cebccbed19c283c349b3d26991eff73a1eaaa8abae2e3c8089b6", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "aa2c74bd271af34239a3948779612f87df2422c2fdcfdbcec28d9c105f0773fe"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, diff --git a/priv/repo/migrations/20210609043242_add_body_html_to_artilces.exs b/priv/repo/migrations/20210609043242_add_body_html_to_artilces.exs new file mode 100644 index 000000000..a803324fb --- /dev/null +++ b/priv/repo/migrations/20210609043242_add_body_html_to_artilces.exs @@ -0,0 +1,15 @@ +defmodule GroupherServer.Repo.Migrations.AddBodyHtmlToArtilces do + use Ecto.Migration + + def change do + alter(table(:cms_posts), do: modify(:body, :text)) + alter(table(:cms_jobs), do: modify(:body, :text)) + alter(table(:cms_repos), do: modify(:body, :text)) + alter(table(:cms_blogs), do: modify(:body, :text)) + + alter(table(:cms_posts), do: add(:body_html, :text)) + alter(table(:cms_jobs), do: add(:body_html, :text)) + alter(table(:cms_repos), do: add(:body_html, :text)) + alter(table(:cms_blogs), do: add(:body_html, :text)) + end +end diff --git a/priv/repo/migrations/20210609050608_adjust_body_to_artilce_comments.exs b/priv/repo/migrations/20210609050608_adjust_body_to_artilce_comments.exs new file mode 100644 index 000000000..2222f24c0 --- /dev/null +++ b/priv/repo/migrations/20210609050608_adjust_body_to_artilce_comments.exs @@ -0,0 +1,8 @@ +defmodule GroupherServer.Repo.Migrations.AdjustBodyToArtilceComments do + use Ecto.Migration + + def change do + alter(table(:articles_comments), do: modify(:body_html, :text)) + alter(table(:articles_comments), do: add(:body, :text)) + end +end diff --git a/test/groupher_server/accounts/published/published_blogs_test.exs b/test/groupher_server/accounts/published/published_blogs_test.exs index 2ac2b7aed..092a7579b 100644 --- a/test/groupher_server/accounts/published/published_blogs_test.exs +++ b/test/groupher_server/accounts/published/published_blogs_test.exs @@ -76,7 +76,7 @@ defmodule GroupherServer.Test.Accounts.Published.Blog do total_count = 10 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) diff --git a/test/groupher_server/accounts/published/published_jobs_test.exs b/test/groupher_server/accounts/published/published_jobs_test.exs index 115c73cf2..770679371 100644 --- a/test/groupher_server/accounts/published/published_jobs_test.exs +++ b/test/groupher_server/accounts/published/published_jobs_test.exs @@ -76,7 +76,7 @@ defmodule GroupherServer.Test.Accounts.Published.Job do total_count = 10 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) diff --git a/test/groupher_server/accounts/published/published_posts_test.exs b/test/groupher_server/accounts/published/published_posts_test.exs index 71a899069..2579d0aa4 100644 --- a/test/groupher_server/accounts/published/published_posts_test.exs +++ b/test/groupher_server/accounts/published/published_posts_test.exs @@ -76,7 +76,7 @@ defmodule GroupherServer.Test.Accounts.Published.Post do total_count = 10 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) diff --git a/test/groupher_server/accounts/published/published_repos_test.exs b/test/groupher_server/accounts/published/published_repos_test.exs index e72fe3b28..869618fe6 100644 --- a/test/groupher_server/accounts/published/published_repos_test.exs +++ b/test/groupher_server/accounts/published/published_repos_test.exs @@ -76,7 +76,7 @@ defmodule GroupherServer.Test.Accounts.Published.Repo do total_count = 10 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) diff --git a/test/groupher_server/cms/abuse_reports/comment_report_test.exs b/test/groupher_server/cms/abuse_reports/comment_report_test.exs index 6ce9610b7..c88ac0e9b 100644 --- a/test/groupher_server/cms/abuse_reports/comment_report_test.exs +++ b/test/groupher_server/cms/abuse_reports/comment_report_test.exs @@ -16,8 +16,8 @@ defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do describe "[article comment report/unreport]" do test "report a comment should have a abuse report record", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) filter = %{content_type: :article_comment, content_id: comment.id, page: 1, size: 20} {:ok, all_reports} = CMS.paged_reports(filter) @@ -33,9 +33,9 @@ defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do test "different user report a comment should have same report with different report cases", ~m(user user2 post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user) - {:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user2) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) + {:ok, _} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user2) filter = %{content_type: :article_comment, content_id: comment.id, page: 1, size: 20} {:ok, all_reports} = CMS.paged_reports(filter) @@ -52,9 +52,9 @@ defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do end test "same user can not report a comment twice", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) - assert {:error, _} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) + assert {:error, _} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end end end diff --git a/test/groupher_server/cms/articles/blog_test.exs b/test/groupher_server/cms/articles/blog_test.exs index 4343c6145..3c02b7400 100644 --- a/test/groupher_server/cms/articles/blog_test.exs +++ b/test/groupher_server/cms/articles/blog_test.exs @@ -2,9 +2,13 @@ defmodule GroupherServer.Test.Articles.Blog do use GroupherServer.TestTools alias GroupherServer.CMS - alias CMS.Model.{Blog, Community} + alias Helper.Converter.{EditorToHTML, HtmlSanitizer} + + alias EditorToHTML.{Class, Validator} + alias CMS.Model.{Author, Blog, Community} alias Helper.ORM + @root_class Class.article() @last_year Timex.shift(Timex.beginning_of_year(Timex.now()), days: -3, seconds: -1) setup do @@ -18,12 +22,19 @@ defmodule GroupherServer.Test.Articles.Blog do end describe "[cms blogs curd]" do - test "can create a blog with valid attrs", ~m(user community blog_attrs)a do + test "can create blog with valid attrs", ~m(user community blog_attrs)a do + assert {:error, _} = ORM.find_by(Author, user_id: user.id) {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) - {:ok, found} = ORM.find(Blog, blog.id) - assert found.id == blog.id - assert found.title == blog.title + body_map = Jason.decode!(blog.body) + + assert blog.title == blog_attrs.title + assert body_map |> Validator.is_valid() + assert blog.body_html |> String.contains?(~s()) + assert blog.body_html |> String.contains?(~s(List.first() |> get_in(["data", "text"]) + assert blog.digest == paragraph_text |> HtmlSanitizer.strip_all_tags() end test "created blog should have a acitve_at field, same with inserted_at", diff --git a/test/groupher_server/cms/articles/job_test.exs b/test/groupher_server/cms/articles/job_test.exs index aeac499be..d20c13d8d 100644 --- a/test/groupher_server/cms/articles/job_test.exs +++ b/test/groupher_server/cms/articles/job_test.exs @@ -2,9 +2,14 @@ defmodule GroupherServer.Test.Articles.Job do use GroupherServer.TestTools alias GroupherServer.CMS - alias CMS.Model.{Job, Community} + alias Helper.Converter.{EditorToHTML, HtmlSanitizer} + + alias EditorToHTML.{Class, Validator} + alias CMS.Model.{Author, Job, Community} + alias Helper.ORM + @root_class Class.article() @last_year Timex.shift(Timex.beginning_of_year(Timex.now()), days: -3, seconds: -1) setup do @@ -18,12 +23,19 @@ defmodule GroupherServer.Test.Articles.Job do end describe "[cms jobs curd]" do - test "can create a job with valid attrs", ~m(user community job_attrs)a do + test "can create job with valid attrs", ~m(user community job_attrs)a do + assert {:error, _} = ORM.find_by(Author, user_id: user.id) {:ok, job} = CMS.create_article(community, :job, job_attrs, user) - {:ok, found} = ORM.find(Job, job.id) - assert found.id == job.id - assert found.title == job.title + body_map = Jason.decode!(job.body) + + assert job.title == job_attrs.title + assert body_map |> Validator.is_valid() + assert job.body_html |> String.contains?(~s(
)) + assert job.body_html |> String.contains?(~s(List.first() |> get_in(["data", "text"]) + assert job.digest == paragraph_text |> HtmlSanitizer.strip_all_tags() end test "created job should have a acitve_at field, same with inserted_at", diff --git a/test/groupher_server/cms/articles/post_test.exs b/test/groupher_server/cms/articles/post_test.exs index e3668f282..f62ec7dee 100644 --- a/test/groupher_server/cms/articles/post_test.exs +++ b/test/groupher_server/cms/articles/post_test.exs @@ -3,9 +3,12 @@ defmodule GroupherServer.Test.CMS.Articles.Post do alias Helper.ORM alias GroupherServer.CMS + alias Helper.Converter.{EditorToHTML, HtmlSanitizer} + alias EditorToHTML.{Class, Validator} alias CMS.Model.{Author, Community, Post} + @root_class Class.article() @last_year Timex.shift(Timex.beginning_of_year(Timex.now()), days: -3, seconds: -1) setup do @@ -22,10 +25,17 @@ defmodule GroupherServer.Test.CMS.Articles.Post do describe "[cms post curd]" do test "can create post with valid attrs", ~m(user community post_attrs)a do assert {:error, _} = ORM.find_by(Author, user_id: user.id) - {:ok, post} = CMS.create_article(community, :post, post_attrs, user) + body_map = Jason.decode!(post.body) + assert post.title == post_attrs.title + assert body_map |> Validator.is_valid() + assert post.body_html |> String.contains?(~s(
)) + assert post.body_html |> String.contains?(~s(List.first() |> get_in(["data", "text"]) + assert post.digest == paragraph_text |> HtmlSanitizer.strip_all_tags() end test "created post should have a acitve_at field, same with inserted_at", diff --git a/test/groupher_server/cms/articles/repo_test.exs b/test/groupher_server/cms/articles/repo_test.exs index 7d07dc3f8..ea0d68425 100644 --- a/test/groupher_server/cms/articles/repo_test.exs +++ b/test/groupher_server/cms/articles/repo_test.exs @@ -2,10 +2,13 @@ defmodule GroupherServer.Test.Articles.Repo do use GroupherServer.TestTools alias GroupherServer.CMS - alias CMS.Model.{Author, Community, Repo} + alias Helper.Converter.{EditorToHTML} + alias EditorToHTML.{Class, Validator} + alias CMS.Model.{Author, Repo, Community} alias Helper.ORM + @root_class Class.article() @last_year Timex.shift(Timex.beginning_of_year(Timex.now()), days: -3, seconds: -1) setup do @@ -22,10 +25,13 @@ defmodule GroupherServer.Test.Articles.Repo do describe "[cms repo curd]" do test "can create repo with valid attrs", ~m(user community repo_attrs)a do assert {:error, _} = ORM.find_by(Author, user_id: user.id) - {:ok, repo} = CMS.create_article(community, :repo, repo_attrs, user) + body_map = Jason.decode!(repo.body) + assert repo.title == repo_attrs.title + assert body_map |> Validator.is_valid() + assert repo.body_html |> String.contains?(~s(
)) assert repo.contributors |> length !== 0 end @@ -57,6 +63,7 @@ defmodule GroupherServer.Test.Articles.Repo do assert user2.id in created.meta.viewed_user_ids end + @tag :wip test "read repo should contains viewer_has_xxx state", ~m(repo_attrs community user user2)a do {:ok, repo} = CMS.create_article(community, :repo, repo_attrs, user) {:ok, repo} = CMS.read_article(:repo, repo.id, user) diff --git a/test/groupher_server/cms/comments/blog_comment_emotions_test.exs b/test/groupher_server/cms/comments/blog_comment_emotions_test.exs index 2e2771c84..10654a2b9 100644 --- a/test/groupher_server/cms/comments/blog_comment_emotions_test.exs +++ b/test/groupher_server/cms/comments/blog_comment_emotions_test.exs @@ -28,7 +28,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentEmotions do all_comment = Enum.reduce(0..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) @@ -65,9 +65,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentEmotions do describe "[basic article comment emotion]" do test "comment has default emotions after created", ~m(blog user)a do - parent_content = "parent comment" - - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) emotions = parent_comment.emotions |> Map.from_struct() |> Map.delete(:id) @@ -75,8 +73,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentEmotions do end test "can make emotion to comment", ~m(blog user user2)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) @@ -89,8 +86,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentEmotions do end test "can undo emotion to comment", ~m(blog user user2)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) @@ -111,8 +107,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentEmotions do end test "same user make same emotion to same comment.", ~m(blog user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) @@ -125,8 +120,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentEmotions do test "same user same emotion to same comment only have one user_emotion record", ~m(blog user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :heart, user) @@ -147,7 +141,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentEmotions do end test "different user can make same emotions on same comment", ~m(blog user user2 user3)a do - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user2) @@ -163,8 +157,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentEmotions do end test "same user can make differcent emotions on same comment", ~m(blog user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) diff --git a/test/groupher_server/cms/comments/blog_comment_replies_test.exs b/test/groupher_server/cms/comments/blog_comment_replies_test.exs index 84a0299c4..f518c16d2 100644 --- a/test/groupher_server/cms/comments/blog_comment_replies_test.exs +++ b/test/groupher_server/cms/comments/blog_comment_replies_test.exs @@ -20,11 +20,8 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do describe "[basic article comment replies]" do test "exsit comment can be reply", ~m(blog user user2)a do - parent_content = "parent comment" - reply_content = "reply comment" - - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) - {:ok, replyed_comment} = CMS.reply_article_comment(parent_comment.id, reply_content, user2) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, replyed_comment} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) assert replyed_comment.reply_to.id == parent_comment.id {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -33,27 +30,20 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do end test "deleted comment can not be reply", ~m(blog user user2)a do - parent_content = "parent comment" - reply_content = "reply comment" - - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.delete_article_comment(parent_comment) - {:error, _} = CMS.reply_article_comment(parent_comment.id, reply_content, user2) + {:error, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) end test "multi reply should belong to one parent comment", ~m(blog user user2)a do - parent_content = "parent comment" - reply_content_1 = "reply comment 1" - reply_content_2 = "reply comment 2" - - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, replyed_comment_1} = - CMS.reply_article_comment(parent_comment.id, reply_content_1, user2) + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, replyed_comment_2} = - CMS.reply_article_comment(parent_comment.id, reply_content_2, user2) + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -63,11 +53,16 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do test "reply to reply inside a comment should belong same parent comment", ~m(blog user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(replyed_comment_1.id, "reply 2", user2) - {:ok, replyed_comment_3} = CMS.reply_article_comment(replyed_comment_2.id, "reply 3", user) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(replyed_comment_1.id, mock_comment(), user2) + + {:ok, replyed_comment_3} = + CMS.reply_article_comment(replyed_comment_2.id, mock_comment(), user) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -88,11 +83,16 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do test "reply to reply inside a comment should have is_reply_to_others flag in meta", ~m(blog user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(replyed_comment_1.id, mock_comment(), user2) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(replyed_comment_1.id, "reply 2", user2) - {:ok, replyed_comment_3} = CMS.reply_article_comment(replyed_comment_2.id, "reply 3", user) + {:ok, replyed_comment_3} = + CMS.reply_article_comment(replyed_comment_2.id, mock_comment(), user) {:ok, _parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -108,12 +108,12 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do test "comment replies only contains @max_parent_replies_count replies", ~m(blog user)a do total_reply_count = @max_parent_replies_count + 1 - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) reply_comment_list = Enum.reduce(1..total_reply_count, [], fn n, acc -> {:ok, replyed_comment} = - CMS.reply_article_comment(parent_comment.id, "reply_content_#{n}", user) + CMS.reply_article_comment(parent_comment.id, mock_comment("reply_content_#{n}"), user) acc ++ [replyed_comment] end) @@ -128,8 +128,8 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do end test "replyed user should appear in article comment participators", ~m(blog user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent_conent", user) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, article} = ORM.find(Blog, blog.id) @@ -138,14 +138,14 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do end test "replies count should inc by 1 after got replyed", ~m(blog user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) assert parent_comment.replies_count === 0 - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) assert parent_comment.replies_count === 1 - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) assert parent_comment.replies_count === 2 end @@ -153,7 +153,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do describe "[paged article comment replies]" do test "can get paged replies of a parent comment", ~m(blog user)a do - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, paged_replies} = CMS.paged_comment_replies(parent_comment.id, %{page: 1, size: 20}) assert is_valid_pagination?(paged_replies, :raw, :empty) @@ -162,7 +162,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do reply_comment_list = Enum.reduce(1..total_reply_count, [], fn n, acc -> {:ok, replyed_comment} = - CMS.reply_article_comment(parent_comment.id, "reply_content_#{n}", user) + CMS.reply_article_comment(parent_comment.id, mock_comment("reply_content_#{n}"), user) acc ++ [replyed_comment] end) @@ -182,12 +182,11 @@ defmodule GroupherServer.Test.CMS.Comments.BlogCommentReplies do page_number = 1 page_size = 10 - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) - {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, "reply_content_1", user) + {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) - {:ok, reply_comment2} = - CMS.reply_article_comment(parent_comment.id, "reply_content_2", user) + {:ok, reply_comment2} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) {:ok, paged_comments} = CMS.paged_article_comments( diff --git a/test/groupher_server/cms/comments/blog_comment_test.exs b/test/groupher_server/cms/comments/blog_comment_test.exs index 8ec588d73..7b66a3604 100644 --- a/test/groupher_server/cms/comments/blog_comment_test.exs +++ b/test/groupher_server/cms/comments/blog_comment_test.exs @@ -27,8 +27,8 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do describe "[basic article comment]" do test "blog are supported by article comment.", ~m(user blog)a do - {:ok, blog_comment_1} = CMS.create_article_comment(:blog, blog.id, "blog_comment 1", user) - {:ok, blog_comment_2} = CMS.create_article_comment(:blog, blog.id, "blog_comment 2", user) + {:ok, blog_comment_1} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, blog_comment_2} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, blog} = ORM.find(Blog, blog.id, preload: :article_comments) @@ -37,13 +37,13 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "comment should have default meta after create", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) assert comment.meta |> Map.from_struct() |> Map.delete(:id) == @default_comment_meta end test "create comment should update active timestamp of blog", ~m(user blog)a do Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, blog} = ORM.find(Blog, blog.id, preload: :article_comments) assert not is_nil(blog.active_at) @@ -58,7 +58,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do Process.sleep(1000) {:ok, _comment} = - CMS.create_article_comment(:blog, blog.id, "blog comment", blog.author.user) + CMS.create_article_comment(:blog, blog.id, mock_comment(), blog.author.user) {:ok, blog} = ORM.find(Blog, blog.id, preload: :article_comments) @@ -74,7 +74,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do {:ok, blog} = db_insert(:blog, %{inserted_at: inserted_at}) Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, blog} = ORM.find(Blog, blog.id) assert blog.active_at |> DateTime.to_date() == DateTime.utc_now() |> DateTime.to_date() @@ -85,25 +85,26 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do {:ok, blog} = db_insert(:blog, %{inserted_at: inserted_at}) Process.sleep(3000) - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, blog} = ORM.find(Blog, blog.id) assert blog.active_at |> DateTime.to_unix() !== DateTime.utc_now() |> DateTime.to_unix() end test "comment can be updated", ~m(blog user)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) - {:ok, updated_comment} = CMS.update_article_comment(comment, "updated content") + {:ok, updated_comment} = + CMS.update_article_comment(comment, mock_comment("updated content")) - assert updated_comment.body_html == "updated content" + assert updated_comment.body_html |> String.contains?(~s(updated content)) end end describe "[article comment floor]" do test "comment will have a floor number after created", ~m(user blog)a do - {:ok, blog_comment} = CMS.create_article_comment(:blog, blog.id, "comment", user) - {:ok, blog_comment2} = CMS.create_article_comment(:blog, blog.id, "comment2", user) + {:ok, blog_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, blog_comment2} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, blog_comment} = ORM.find(ArticleComment, blog_comment.id) {:ok, blog_comment2} = ORM.find(ArticleComment, blog_comment2.id) @@ -115,9 +116,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do describe "[article comment participator for blog]" do test "blog will have participator after comment created", ~m(user blog)a do - blog_comment_1 = "blog_comment 1" - - {:ok, _} = CMS.create_article_comment(:blog, blog.id, blog_comment_1, user) + {:ok, _} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, blog} = ORM.find(Blog, blog.id) @@ -126,10 +125,8 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "psot participator will not contains same user", ~m(user blog)a do - blog_comment_1 = "blog_comment 1" - - {:ok, _} = CMS.create_article_comment(:blog, blog.id, blog_comment_1, user) - {:ok, _} = CMS.create_article_comment(:blog, blog.id, blog_comment_1, user) + {:ok, _} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, _} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, blog} = ORM.find(Blog, blog.id) @@ -138,10 +135,8 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do test "recent comment user should appear at first of the psot participators", ~m(user user2 blog)a do - blog_comment_1 = "blog_comment 1" - - {:ok, _} = CMS.create_article_comment(:blog, blog.id, blog_comment_1, user) - {:ok, _} = CMS.create_article_comment(:blog, blog.id, blog_comment_1, user2) + {:ok, _} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, _} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user2) {:ok, blog} = ORM.find(Blog, blog.id) @@ -153,8 +148,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do describe "[article comment upvotes]" do test "user can upvote a blog comment", ~m(user blog)a do - comment = "blog_comment" - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) @@ -165,8 +159,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "article author upvote blog comment will have flag", ~m(blog user)a do - comment = "blog_comment" - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, author_user} = ORM.find(User, blog.author.user.id) CMS.upvote_article_comment(comment.id, author_user) @@ -176,8 +169,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "user upvote blog comment will add id to upvoted_user_ids", ~m(blog user)a do - comment = "blog_comment" - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, comment} = CMS.upvote_article_comment(comment.id, user) assert user.id in comment.meta.upvoted_user_ids @@ -185,8 +177,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do test "user undo upvote blog comment will remove id from upvoted_user_ids", ~m(blog user user2)a do - comment = "blog_comment" - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _comment} = CMS.upvote_article_comment(comment.id, user) {:ok, comment} = CMS.upvote_article_comment(comment.id, user2) @@ -200,16 +191,14 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "user upvote a already-upvoted comment fails", ~m(user blog)a do - comment = "blog_comment" - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) {:error, _} = CMS.upvote_article_comment(comment.id, user) end test "upvote comment should inc the comment's upvotes_count", ~m(user user2 blog)a do - comment = "blog_comment" - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert comment.upvotes_count == 0 @@ -221,8 +210,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "user can undo upvote a blog comment", ~m(user blog)a do - content = "blog_comment" - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, content, user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) {:ok, comment} = ORM.find(ArticleComment, comment.id, preload: :upvotes) @@ -233,8 +221,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "user can undo upvote a blog comment with no upvote", ~m(user blog)a do - content = "blog_comment" - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, content, user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, comment} = CMS.undo_upvote_article_comment(comment.id, user) assert 0 == comment.upvotes_count @@ -245,7 +232,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do describe "[article comment fold/unfold]" do test "user can fold a comment", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert not comment.is_folded @@ -256,7 +243,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "user can unfold a comment", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _comment} = CMS.fold_article_comment(comment.id, user) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -270,7 +257,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do describe "[article comment pin/unpin]" do test "user can pin a comment", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert not comment.is_pinned @@ -285,7 +272,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "user can unpin a comment", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _comment} = CMS.pin_article_comment(comment.id) {:ok, comment} = CMS.undo_pin_article_comment(comment.id) @@ -295,7 +282,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "pinned comments has a limit for each article", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) Enum.reduce(0..(@pinned_comment_limit - 1), [], fn _, _acc -> {:ok, _comment} = CMS.pin_article_comment(comment.id) @@ -308,17 +295,17 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do describe "[article comment report/unreport]" do # # test "user can report a comment", ~m(user blog)a do - # {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + # {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) - # {:ok, comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + # {:ok, comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) # end # # test "user can unreport a comment", ~m(user blog)a do - # {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) - # {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + # {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + # {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) # {:ok, _comment} = CMS.undo_report_article_comment(comment.id, user) @@ -326,10 +313,10 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do # end test "can undo a report with other user report it too", ~m(user user2 blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user2) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user2) filter = %{content_type: :article_comment, content_id: comment.id, page: 1, size: 20} {:ok, all_reports} = CMS.paged_reports(filter) @@ -353,13 +340,13 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "report user < @report_threshold_for_fold will not fold comment", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) assert not comment.is_folded Enum.reduce(1..(@report_threshold_for_fold - 1), [], fn _, _acc -> {:ok, user} = db_insert(:user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -367,13 +354,13 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "report user > @report_threshold_for_fold will cause comment fold", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) assert not comment.is_folded Enum.reduce(1..(@report_threshold_for_fold + 1), [], fn _, _acc -> {:ok, user} = db_insert(:user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -389,13 +376,13 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do Enum.reduce(1..total_count, [], fn _, acc -> {:ok, new_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", new_user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), new_user) acc ++ [comment] end) - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, results} = CMS.paged_article_comments_participators(thread, blog.id, %{page: 1, size: page_size}) @@ -411,7 +398,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) @@ -440,13 +427,13 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do page_size = 5 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) - {:ok, random_comment_1} = CMS.create_article_comment(:blog, blog.id, "pin commment", user) - {:ok, random_comment_2} = CMS.create_article_comment(:blog, blog.id, "pin commment2", user) + {:ok, random_comment_1} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, random_comment_2} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, pined_comment_1} = CMS.pin_article_comment(random_comment_1.id) {:ok, pined_comment_2} = CMS.pin_article_comment(random_comment_2.id) @@ -472,13 +459,13 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do page_size = 5 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) - {:ok, random_comment_1} = CMS.create_article_comment(:blog, blog.id, "pin commment", user) - {:ok, random_comment_2} = CMS.create_article_comment(:blog, blog.id, "pin commment2", user) + {:ok, random_comment_1} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, random_comment_2} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, pined_comment_1} = CMS.pin_article_comment(random_comment_1.id) {:ok, pined_comment_2} = CMS.pin_article_comment(random_comment_2.id) @@ -505,7 +492,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) @@ -542,7 +529,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do all_folded_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) CMS.fold_article_comment(comment.id, user) acc ++ [comment] @@ -573,7 +560,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) @@ -596,11 +583,11 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end test "delete comment still update article's comments_count field", ~m(user blog)a do - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, blog} = ORM.find(Blog, blog.id) @@ -617,7 +604,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) @@ -634,38 +621,38 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do describe "[article comment info]" do test "author of the article comment a comment should have flag", ~m(user blog)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) assert not comment.is_article_author author_user = blog.author.user - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", author_user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), author_user) assert comment.is_article_author end end describe "[lock/unlock blog comment]" do test "locked blog can not be comment", ~m(user blog)a do - {:ok, _} = CMS.create_article_comment(:blog, blog.id, "comment", user) + {:ok, _} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.lock_article_comment(:blog, blog.id) - {:error, reason} = CMS.create_article_comment(:blog, blog.id, "comment", user) + {:error, reason} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) assert reason |> is_error?(:article_comment_locked) {:ok, _} = CMS.undo_lock_article_comment(:blog, blog.id) - {:ok, _} = CMS.create_article_comment(:blog, blog.id, "comment", user) + {:ok, _} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) end test "locked blog can not by reply", ~m(user blog)a do - {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, "parent_conent", user) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:ok, parent_comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) {:ok, _} = CMS.lock_article_comment(:blog, blog.id) - {:error, reason} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:error, reason} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) assert reason |> is_error?(:article_comment_locked) {:ok, _} = CMS.undo_lock_article_comment(:blog, blog.id) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) end end end diff --git a/test/groupher_server/cms/comments/job_comment_emotions_test.exs b/test/groupher_server/cms/comments/job_comment_emotions_test.exs index 53c674d4f..0d4086f27 100644 --- a/test/groupher_server/cms/comments/job_comment_emotions_test.exs +++ b/test/groupher_server/cms/comments/job_comment_emotions_test.exs @@ -28,7 +28,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do all_comment = Enum.reduce(0..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) @@ -65,9 +65,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do describe "[basic article comment emotion]" do test "comment has default emotions after created", ~m(job user)a do - parent_content = "parent comment" - - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) emotions = parent_comment.emotions |> Map.from_struct() |> Map.delete(:id) @@ -75,8 +73,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do end test "can make emotion to comment", ~m(job user user2)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) @@ -89,8 +86,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do end test "can undo emotion to comment", ~m(job user user2)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) @@ -111,8 +107,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do end test "same user make same emotion to same comment.", ~m(job user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) @@ -125,8 +120,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do test "same user same emotion to same comment only have one user_emotion record", ~m(job user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :heart, user) @@ -147,7 +141,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do end test "different user can make same emotions on same comment", ~m(job user user2 user3)a do - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user2) @@ -163,8 +157,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do end test "same user can make differcent emotions on same comment", ~m(job user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) diff --git a/test/groupher_server/cms/comments/job_comment_replies_test.exs b/test/groupher_server/cms/comments/job_comment_replies_test.exs index 72c6b55ca..ecbac42fb 100644 --- a/test/groupher_server/cms/comments/job_comment_replies_test.exs +++ b/test/groupher_server/cms/comments/job_comment_replies_test.exs @@ -20,11 +20,8 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do describe "[basic article comment replies]" do test "exsit comment can be reply", ~m(job user user2)a do - parent_content = "parent comment" - reply_content = "reply comment" - - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) - {:ok, replyed_comment} = CMS.reply_article_comment(parent_comment.id, reply_content, user2) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, replyed_comment} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) assert replyed_comment.reply_to.id == parent_comment.id {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -33,27 +30,20 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do end test "deleted comment can not be reply", ~m(job user user2)a do - parent_content = "parent comment" - reply_content = "reply comment" - - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.delete_article_comment(parent_comment) - {:error, _} = CMS.reply_article_comment(parent_comment.id, reply_content, user2) + {:error, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) end test "multi reply should belong to one parent comment", ~m(job user user2)a do - parent_content = "parent comment" - reply_content_1 = "reply comment 1" - reply_content_2 = "reply comment 2" - - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, replyed_comment_1} = - CMS.reply_article_comment(parent_comment.id, reply_content_1, user2) + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, replyed_comment_2} = - CMS.reply_article_comment(parent_comment.id, reply_content_2, user2) + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -63,11 +53,16 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do test "reply to reply inside a comment should belong same parent comment", ~m(job user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(replyed_comment_1.id, "reply 2", user2) - {:ok, replyed_comment_3} = CMS.reply_article_comment(replyed_comment_2.id, "reply 3", user) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(replyed_comment_1.id, mock_comment(), user2) + + {:ok, replyed_comment_3} = + CMS.reply_article_comment(replyed_comment_2.id, mock_comment(), user) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -88,11 +83,16 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do test "reply to reply inside a comment should have is_reply_to_others flag in meta", ~m(job user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(replyed_comment_1.id, mock_comment(), user2) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(replyed_comment_1.id, "reply 2", user2) - {:ok, replyed_comment_3} = CMS.reply_article_comment(replyed_comment_2.id, "reply 3", user) + {:ok, replyed_comment_3} = + CMS.reply_article_comment(replyed_comment_2.id, mock_comment(), user) {:ok, _parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -108,12 +108,12 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do test "comment replies only contains @max_parent_replies_count replies", ~m(job user)a do total_reply_count = @max_parent_replies_count + 1 - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) reply_comment_list = Enum.reduce(1..total_reply_count, [], fn n, acc -> {:ok, replyed_comment} = - CMS.reply_article_comment(parent_comment.id, "reply_content_#{n}", user) + CMS.reply_article_comment(parent_comment.id, mock_comment("reply_content_#{n}"), user) acc ++ [replyed_comment] end) @@ -128,8 +128,8 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do end test "replyed user should appear in article comment participators", ~m(job user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent_conent", user) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, article} = ORM.find(Job, job.id) @@ -138,14 +138,14 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do end test "replies count should inc by 1 after got replyed", ~m(job user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) assert parent_comment.replies_count === 0 - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) assert parent_comment.replies_count === 1 - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) assert parent_comment.replies_count === 2 end @@ -153,7 +153,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do describe "[paged article comment replies]" do test "can get paged replies of a parent comment", ~m(job user)a do - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, paged_replies} = CMS.paged_comment_replies(parent_comment.id, %{page: 1, size: 20}) assert is_valid_pagination?(paged_replies, :raw, :empty) @@ -162,7 +162,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do reply_comment_list = Enum.reduce(1..total_reply_count, [], fn n, acc -> {:ok, replyed_comment} = - CMS.reply_article_comment(parent_comment.id, "reply_content_#{n}", user) + CMS.reply_article_comment(parent_comment.id, mock_comment("reply_content_#{n}"), user) acc ++ [replyed_comment] end) @@ -182,12 +182,11 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do page_number = 1 page_size = 10 - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) - {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, "reply_content_1", user) + {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) - {:ok, reply_comment2} = - CMS.reply_article_comment(parent_comment.id, "reply_content_2", user) + {:ok, reply_comment2} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) {:ok, paged_comments} = CMS.paged_article_comments( diff --git a/test/groupher_server/cms/comments/job_comment_test.exs b/test/groupher_server/cms/comments/job_comment_test.exs index 0be65e808..6a6fe7341 100644 --- a/test/groupher_server/cms/comments/job_comment_test.exs +++ b/test/groupher_server/cms/comments/job_comment_test.exs @@ -27,8 +27,8 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do describe "[basic article comment]" do test "job are supported by article comment.", ~m(user job)a do - {:ok, job_comment_1} = CMS.create_article_comment(:job, job.id, "job_comment 1", user) - {:ok, job_comment_2} = CMS.create_article_comment(:job, job.id, "job_comment 2", user) + {:ok, job_comment_1} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, job_comment_2} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, job} = ORM.find(Job, job.id, preload: :article_comments) @@ -37,13 +37,13 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "comment should have default meta after create", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) assert comment.meta |> Map.from_struct() |> Map.delete(:id) == @default_comment_meta end test "create comment should update active timestamp of job", ~m(user job)a do Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, job} = ORM.find(Job, job.id, preload: :article_comments) assert not is_nil(job.active_at) @@ -57,7 +57,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "job comment", job.author.user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), job.author.user) {:ok, job} = ORM.find(Job, job.id, preload: :article_comments) @@ -73,7 +73,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do {:ok, job} = db_insert(:job, %{inserted_at: inserted_at}) Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, job} = ORM.find(Job, job.id) assert job.active_at |> DateTime.to_date() == DateTime.utc_now() |> DateTime.to_date() @@ -84,25 +84,26 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do {:ok, job} = db_insert(:job, %{inserted_at: inserted_at}) Process.sleep(3000) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, job} = ORM.find(Job, job.id) assert job.active_at |> DateTime.to_unix() !== DateTime.utc_now() |> DateTime.to_unix() end test "comment can be updated", ~m(job user)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) - {:ok, updated_comment} = CMS.update_article_comment(comment, "updated content") + {:ok, updated_comment} = + CMS.update_article_comment(comment, mock_comment("updated content")) - assert updated_comment.body_html == "updated content" + assert updated_comment.body_html |> String.contains?(~s(updated content)) end end describe "[article comment floor]" do test "comment will have a floor number after created", ~m(user job)a do - {:ok, job_comment} = CMS.create_article_comment(:job, job.id, "comment", user) - {:ok, job_comment2} = CMS.create_article_comment(:job, job.id, "comment2", user) + {:ok, job_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, job_comment2} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, job_comment} = ORM.find(ArticleComment, job_comment.id) {:ok, job_comment2} = ORM.find(ArticleComment, job_comment2.id) @@ -114,9 +115,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do describe "[article comment participator for job]" do test "job will have participator after comment created", ~m(user job)a do - job_comment_1 = "job_comment 1" - - {:ok, _} = CMS.create_article_comment(:job, job.id, job_comment_1, user) + {:ok, _} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, job} = ORM.find(Job, job.id) @@ -125,10 +124,8 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "psot participator will not contains same user", ~m(user job)a do - job_comment_1 = "job_comment 1" - - {:ok, _} = CMS.create_article_comment(:job, job.id, job_comment_1, user) - {:ok, _} = CMS.create_article_comment(:job, job.id, job_comment_1, user) + {:ok, _} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, job} = ORM.find(Job, job.id) @@ -137,10 +134,8 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do test "recent comment user should appear at first of the psot participators", ~m(user user2 job)a do - job_comment_1 = "job_comment 1" - - {:ok, _} = CMS.create_article_comment(:job, job.id, job_comment_1, user) - {:ok, _} = CMS.create_article_comment(:job, job.id, job_comment_1, user2) + {:ok, _} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _} = CMS.create_article_comment(:job, job.id, mock_comment(), user2) {:ok, job} = ORM.find(Job, job.id) @@ -152,8 +147,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do describe "[article comment upvotes]" do test "user can upvote a job comment", ~m(user job)a do - comment = "job_comment" - {:ok, comment} = CMS.create_article_comment(:job, job.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) @@ -164,8 +158,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "article author upvote job comment will have flag", ~m(job user)a do - comment = "job_comment" - {:ok, comment} = CMS.create_article_comment(:job, job.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, author_user} = ORM.find(User, job.author.user.id) CMS.upvote_article_comment(comment.id, author_user) @@ -175,8 +168,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "user upvote job comment will add id to upvoted_user_ids", ~m(job user)a do - comment = "job_comment" - {:ok, comment} = CMS.create_article_comment(:job, job.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, comment} = CMS.upvote_article_comment(comment.id, user) assert user.id in comment.meta.upvoted_user_ids @@ -184,8 +176,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do test "user undo upvote job comment will remove id from upvoted_user_ids", ~m(job user user2)a do - comment = "job_comment" - {:ok, comment} = CMS.create_article_comment(:job, job.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _comment} = CMS.upvote_article_comment(comment.id, user) {:ok, comment} = CMS.upvote_article_comment(comment.id, user2) @@ -199,16 +190,14 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "user upvote a already-upvoted comment fails", ~m(user job)a do - comment = "job_comment" - {:ok, comment} = CMS.create_article_comment(:job, job.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) {:error, _} = CMS.upvote_article_comment(comment.id, user) end test "upvote comment should inc the comment's upvotes_count", ~m(user user2 job)a do - comment = "job_comment" - {:ok, comment} = CMS.create_article_comment(:job, job.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert comment.upvotes_count == 0 @@ -220,8 +209,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "user can undo upvote a job comment", ~m(user job)a do - content = "job_comment" - {:ok, comment} = CMS.create_article_comment(:job, job.id, content, user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) {:ok, comment} = ORM.find(ArticleComment, comment.id, preload: :upvotes) @@ -232,8 +220,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "user can undo upvote a job comment with no upvote", ~m(user job)a do - content = "job_comment" - {:ok, comment} = CMS.create_article_comment(:job, job.id, content, user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, comment} = CMS.undo_upvote_article_comment(comment.id, user) assert 0 == comment.upvotes_count @@ -244,7 +231,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do describe "[article comment fold/unfold]" do test "user can fold a comment", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert not comment.is_folded @@ -255,7 +242,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "user can unfold a comment", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _comment} = CMS.fold_article_comment(comment.id, user) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -269,7 +256,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do describe "[article comment pin/unpin]" do test "user can pin a comment", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert not comment.is_pinned @@ -284,7 +271,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "user can unpin a comment", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _comment} = CMS.pin_article_comment(comment.id) {:ok, comment} = CMS.undo_pin_article_comment(comment.id) @@ -294,7 +281,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "pinned comments has a limit for each article", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) Enum.reduce(0..(@pinned_comment_limit - 1), [], fn _, _acc -> {:ok, _comment} = CMS.pin_article_comment(comment.id) @@ -307,17 +294,17 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do describe "[article comment report/unreport]" do # # test "user can report a comment", ~m(user job)a do - # {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + # {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) - # {:ok, comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + # {:ok, comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) # end # # test "user can unreport a comment", ~m(user job)a do - # {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) - # {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + # {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + # {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) # {:ok, _comment} = CMS.undo_report_article_comment(comment.id, user) @@ -325,10 +312,10 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do # end test "can undo a report with other user report it too", ~m(user user2 job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user2) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user2) filter = %{content_type: :article_comment, content_id: comment.id, page: 1, size: 20} {:ok, all_reports} = CMS.paged_reports(filter) @@ -352,13 +339,13 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "report user < @report_threshold_for_fold will not fold comment", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) assert not comment.is_folded Enum.reduce(1..(@report_threshold_for_fold - 1), [], fn _, _acc -> {:ok, user} = db_insert(:user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -366,13 +353,13 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "report user > @report_threshold_for_fold will cause comment fold", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) assert not comment.is_folded Enum.reduce(1..(@report_threshold_for_fold + 1), [], fn _, _acc -> {:ok, user} = db_insert(:user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -388,13 +375,13 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do Enum.reduce(1..total_count, [], fn _, acc -> {:ok, new_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", new_user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), new_user) acc ++ [comment] end) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, results} = CMS.paged_article_comments_participators(thread, job.id, %{page: 1, size: page_size}) @@ -410,7 +397,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) @@ -439,13 +426,13 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do page_size = 5 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) - {:ok, random_comment_1} = CMS.create_article_comment(:job, job.id, "pin commment", user) - {:ok, random_comment_2} = CMS.create_article_comment(:job, job.id, "pin commment2", user) + {:ok, random_comment_1} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, random_comment_2} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, pined_comment_1} = CMS.pin_article_comment(random_comment_1.id) {:ok, pined_comment_2} = CMS.pin_article_comment(random_comment_2.id) @@ -471,13 +458,13 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do page_size = 5 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) - {:ok, random_comment_1} = CMS.create_article_comment(:job, job.id, "pin commment", user) - {:ok, random_comment_2} = CMS.create_article_comment(:job, job.id, "pin commment2", user) + {:ok, random_comment_1} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, random_comment_2} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, pined_comment_1} = CMS.pin_article_comment(random_comment_1.id) {:ok, pined_comment_2} = CMS.pin_article_comment(random_comment_2.id) @@ -504,7 +491,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) @@ -541,7 +528,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do all_folded_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) CMS.fold_article_comment(comment.id, user) acc ++ [comment] @@ -572,7 +559,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) @@ -595,11 +582,11 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end test "delete comment still update article's comments_count field", ~m(user job)a do - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user) - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, job} = ORM.find(Job, job.id) @@ -616,7 +603,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) @@ -633,38 +620,38 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do describe "[article comment info]" do test "author of the article comment a comment should have flag", ~m(user job)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) assert not comment.is_article_author author_user = job.author.user - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", author_user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), author_user) assert comment.is_article_author end end describe "[lock/unlock job comment]" do test "locked job can not be comment", ~m(user job)a do - {:ok, _} = CMS.create_article_comment(:job, job.id, "comment", user) + {:ok, _} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.lock_article_comment(:job, job.id) - {:error, reason} = CMS.create_article_comment(:job, job.id, "comment", user) + {:error, reason} = CMS.create_article_comment(:job, job.id, mock_comment(), user) assert reason |> is_error?(:article_comment_locked) {:ok, _} = CMS.undo_lock_article_comment(:job, job.id) - {:ok, _} = CMS.create_article_comment(:job, job.id, "comment", user) + {:ok, _} = CMS.create_article_comment(:job, job.id, mock_comment(), user) end test "locked job can not by reply", ~m(user job)a do - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent_conent", user) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) {:ok, _} = CMS.lock_article_comment(:job, job.id) - {:error, reason} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:error, reason} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) assert reason |> is_error?(:article_comment_locked) {:ok, _} = CMS.undo_lock_article_comment(:job, job.id) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) end end end diff --git a/test/groupher_server/cms/comments/post_comment_emotions_test.exs b/test/groupher_server/cms/comments/post_comment_emotions_test.exs index a4aa2cfb7..fd0d0a4a0 100644 --- a/test/groupher_server/cms/comments/post_comment_emotions_test.exs +++ b/test/groupher_server/cms/comments/post_comment_emotions_test.exs @@ -28,7 +28,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do all_comment = Enum.reduce(0..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) @@ -65,9 +65,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do describe "[basic article comment emotion]" do test "comment has default emotions after created", ~m(post user)a do - parent_content = "parent comment" - - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) emotions = parent_comment.emotions |> Map.from_struct() |> Map.delete(:id) @@ -75,8 +73,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do end test "can make emotion to comment", ~m(post user user2)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) @@ -89,8 +86,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do end test "can undo emotion to comment", ~m(post user user2)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) @@ -111,8 +107,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do end test "same user make same emotion to same comment.", ~m(post user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) @@ -125,8 +120,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do test "same user same emotion to same comment only have one user_emotion record", ~m(post user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :heart, user) @@ -147,7 +141,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do end test "different user can make same emotions on same comment", ~m(post user user2 user3)a do - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user2) @@ -163,8 +157,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do end test "same user can make differcent emotions on same comment", ~m(post user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) diff --git a/test/groupher_server/cms/comments/post_comment_replies_test.exs b/test/groupher_server/cms/comments/post_comment_replies_test.exs index 25dedd971..3ef1ee014 100644 --- a/test/groupher_server/cms/comments/post_comment_replies_test.exs +++ b/test/groupher_server/cms/comments/post_comment_replies_test.exs @@ -20,11 +20,8 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do describe "[basic article comment replies]" do test "exsit comment can be reply", ~m(post user user2)a do - parent_content = "parent comment" - reply_content = "reply comment" - - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) - {:ok, replyed_comment} = CMS.reply_article_comment(parent_comment.id, reply_content, user2) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, replyed_comment} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) assert replyed_comment.reply_to.id == parent_comment.id {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -33,27 +30,20 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do end test "deleted comment can not be reply", ~m(post user user2)a do - parent_content = "parent comment" - reply_content = "reply comment" - - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.delete_article_comment(parent_comment) - {:error, _} = CMS.reply_article_comment(parent_comment.id, reply_content, user2) + {:error, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) end test "multi reply should belong to one parent comment", ~m(post user user2)a do - parent_content = "parent comment" - reply_content_1 = "reply comment 1" - reply_content_2 = "reply comment 2" - - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, replyed_comment_1} = - CMS.reply_article_comment(parent_comment.id, reply_content_1, user2) + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, replyed_comment_2} = - CMS.reply_article_comment(parent_comment.id, reply_content_2, user2) + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -63,11 +53,16 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do test "reply to reply inside a comment should belong same parent comment", ~m(post user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(replyed_comment_1.id, "reply 2", user2) - {:ok, replyed_comment_3} = CMS.reply_article_comment(replyed_comment_2.id, "reply 3", user) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(replyed_comment_1.id, mock_comment(), user2) + + {:ok, replyed_comment_3} = + CMS.reply_article_comment(replyed_comment_2.id, mock_comment(), user) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -88,11 +83,16 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do test "reply to reply inside a comment should have is_reply_to_others flag in meta", ~m(post user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(replyed_comment_1.id, mock_comment(), user2) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(replyed_comment_1.id, "reply 2", user2) - {:ok, replyed_comment_3} = CMS.reply_article_comment(replyed_comment_2.id, "reply 3", user) + {:ok, replyed_comment_3} = + CMS.reply_article_comment(replyed_comment_2.id, mock_comment(), user) {:ok, _parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -108,12 +108,12 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do test "comment replies only contains @max_parent_replies_count replies", ~m(post user)a do total_reply_count = @max_parent_replies_count + 1 - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) reply_comment_list = Enum.reduce(1..total_reply_count, [], fn n, acc -> {:ok, replyed_comment} = - CMS.reply_article_comment(parent_comment.id, "reply_content_#{n}", user) + CMS.reply_article_comment(parent_comment.id, mock_comment("reply_content_#{n}"), user) acc ++ [replyed_comment] end) @@ -128,8 +128,8 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do end test "replyed user should appear in article comment participators", ~m(post user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent_conent", user) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, article} = ORM.find(Post, post.id) @@ -138,14 +138,14 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do end test "replies count should inc by 1 after got replyed", ~m(post user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert parent_comment.replies_count === 0 - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) assert parent_comment.replies_count === 1 - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) assert parent_comment.replies_count === 2 end @@ -153,7 +153,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do describe "[paged article comment replies]" do test "can get paged replies of a parent comment", ~m(post user)a do - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, paged_replies} = CMS.paged_comment_replies(parent_comment.id, %{page: 1, size: 20}) assert is_valid_pagination?(paged_replies, :raw, :empty) @@ -162,7 +162,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do reply_comment_list = Enum.reduce(1..total_reply_count, [], fn n, acc -> {:ok, replyed_comment} = - CMS.reply_article_comment(parent_comment.id, "reply_content_#{n}", user) + CMS.reply_article_comment(parent_comment.id, mock_comment("reply_content_#{n}"), user) acc ++ [replyed_comment] end) @@ -182,12 +182,11 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do page_number = 1 page_size = 10 - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) - {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, "reply_content_1", user) + {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) - {:ok, reply_comment2} = - CMS.reply_article_comment(parent_comment.id, "reply_content_2", user) + {:ok, reply_comment2} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) {:ok, paged_comments} = CMS.paged_article_comments( diff --git a/test/groupher_server/cms/comments/post_comment_test.exs b/test/groupher_server/cms/comments/post_comment_test.exs index 3f68d7931..cfc714305 100644 --- a/test/groupher_server/cms/comments/post_comment_test.exs +++ b/test/groupher_server/cms/comments/post_comment_test.exs @@ -27,8 +27,8 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do describe "[basic article comment]" do test "post are supported by article comment.", ~m(user post)a do - {:ok, post_comment_1} = CMS.create_article_comment(:post, post.id, "post_comment 1", user) - {:ok, post_comment_2} = CMS.create_article_comment(:post, post.id, "post_comment 2", user) + {:ok, post_comment_1} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, post_comment_2} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, post} = ORM.find(Post, post.id, preload: :article_comments) @@ -37,13 +37,13 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "comment should have default meta after create", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert comment.meta |> Map.from_struct() |> Map.delete(:id) == @default_comment_meta end test "create comment should update active timestamp of post", ~m(user post)a do Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, post} = ORM.find(Post, post.id, preload: :article_comments) assert not is_nil(post.active_at) @@ -58,7 +58,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do Process.sleep(1000) {:ok, _comment} = - CMS.create_article_comment(:post, post.id, "post comment", post.author.user) + CMS.create_article_comment(:post, post.id, mock_comment(), post.author.user) {:ok, post} = ORM.find(Post, post.id, preload: :article_comments) @@ -74,7 +74,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do {:ok, post} = db_insert(:post, %{inserted_at: inserted_at}) Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, post} = ORM.find(Post, post.id) assert post.active_at |> DateTime.to_date() == DateTime.utc_now() |> DateTime.to_date() @@ -85,25 +85,26 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do {:ok, post} = db_insert(:post, %{inserted_at: inserted_at}) Process.sleep(3000) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, post} = ORM.find(Post, post.id) assert post.active_at |> DateTime.to_unix() !== DateTime.utc_now() |> DateTime.to_unix() end test "comment can be updated", ~m(post user)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) - {:ok, updated_comment} = CMS.update_article_comment(comment, "updated content") + {:ok, updated_comment} = + CMS.update_article_comment(comment, mock_comment("updated content")) - assert updated_comment.body_html == "updated content" + assert updated_comment.body_html |> String.contains?(~s(updated content)) end end describe "[article comment floor]" do test "comment will have a floor number after created", ~m(user post)a do - {:ok, post_comment} = CMS.create_article_comment(:post, post.id, "comment", user) - {:ok, post_comment2} = CMS.create_article_comment(:post, post.id, "comment2", user) + {:ok, post_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, post_comment2} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, post_comment} = ORM.find(ArticleComment, post_comment.id) {:ok, post_comment2} = ORM.find(ArticleComment, post_comment2.id) @@ -115,9 +116,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do describe "[article comment participator for post]" do test "post will have participator after comment created", ~m(user post)a do - post_comment_1 = "post_comment 1" - - {:ok, _} = CMS.create_article_comment(:post, post.id, post_comment_1, user) + {:ok, _} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, post} = ORM.find(Post, post.id) @@ -126,10 +125,8 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "psot participator will not contains same user", ~m(user post)a do - post_comment_1 = "post_comment 1" - - {:ok, _} = CMS.create_article_comment(:post, post.id, post_comment_1, user) - {:ok, _} = CMS.create_article_comment(:post, post.id, post_comment_1, user) + {:ok, _} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, post} = ORM.find(Post, post.id) @@ -138,10 +135,8 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do test "recent comment user should appear at first of the psot participators", ~m(user user2 post)a do - post_comment_1 = "post_comment 1" - - {:ok, _} = CMS.create_article_comment(:post, post.id, post_comment_1, user) - {:ok, _} = CMS.create_article_comment(:post, post.id, post_comment_1, user2) + {:ok, _} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _} = CMS.create_article_comment(:post, post.id, mock_comment(), user2) {:ok, post} = ORM.find(Post, post.id) @@ -153,8 +148,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do describe "[article comment upvotes]" do test "user can upvote a post comment", ~m(user post)a do - comment = "post_comment" - {:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) @@ -165,8 +159,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "article author upvote post comment will have flag", ~m(post user)a do - comment = "post_comment" - {:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, author_user} = ORM.find(User, post.author.user.id) CMS.upvote_article_comment(comment.id, author_user) @@ -176,8 +169,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "user upvote post comment will add id to upvoted_user_ids", ~m(post user)a do - comment = "post_comment" - {:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, comment} = CMS.upvote_article_comment(comment.id, user) assert user.id in comment.meta.upvoted_user_ids @@ -185,8 +177,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do test "user undo upvote post comment will remove id from upvoted_user_ids", ~m(post user user2)a do - comment = "post_comment" - {:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _comment} = CMS.upvote_article_comment(comment.id, user) {:ok, comment} = CMS.upvote_article_comment(comment.id, user2) @@ -200,16 +191,14 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "user upvote a already-upvoted comment fails", ~m(user post)a do - comment = "post_comment" - {:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) {:error, _} = CMS.upvote_article_comment(comment.id, user) end test "upvote comment should inc the comment's upvotes_count", ~m(user user2 post)a do - comment = "post_comment" - {:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert comment.upvotes_count == 0 @@ -221,8 +210,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "user can undo upvote a post comment", ~m(user post)a do - content = "post_comment" - {:ok, comment} = CMS.create_article_comment(:post, post.id, content, user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) {:ok, comment} = ORM.find(ArticleComment, comment.id, preload: :upvotes) @@ -233,8 +221,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "user can undo upvote a post comment with no upvote", ~m(user post)a do - content = "post_comment" - {:ok, comment} = CMS.create_article_comment(:post, post.id, content, user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, comment} = CMS.undo_upvote_article_comment(comment.id, user) assert 0 == comment.upvotes_count @@ -245,7 +232,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do describe "[article comment fold/unfold]" do test "user can fold a comment", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert not comment.is_folded @@ -256,7 +243,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "user can unfold a comment", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _comment} = CMS.fold_article_comment(comment.id, user) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -270,7 +257,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do describe "[article comment pin/unpin]" do test "user can pin a comment", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert not comment.is_pinned @@ -285,7 +272,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "user can unpin a comment", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _comment} = CMS.pin_article_comment(comment.id) {:ok, comment} = CMS.undo_pin_article_comment(comment.id) @@ -295,7 +282,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "pinned comments has a limit for each article", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) Enum.reduce(0..(@pinned_comment_limit - 1), [], fn _, _acc -> {:ok, _comment} = CMS.pin_article_comment(comment.id) @@ -308,17 +295,17 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do describe "[article comment report/unreport]" do # # test "user can report a comment", ~m(user post)a do - # {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + # {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) - # {:ok, comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + # {:ok, comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) # end # # test "user can unreport a comment", ~m(user post)a do - # {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) - # {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + # {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + # {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) # {:ok, _comment} = CMS.undo_report_article_comment(comment.id, user) @@ -326,10 +313,10 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do # end test "can undo a report with other user report it too", ~m(user user2 post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user2) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user2) filter = %{content_type: :article_comment, content_id: comment.id, page: 1, size: 20} {:ok, all_reports} = CMS.paged_reports(filter) @@ -353,13 +340,13 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "report user < @report_threshold_for_fold will not fold comment", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert not comment.is_folded Enum.reduce(1..(@report_threshold_for_fold - 1), [], fn _, _acc -> {:ok, user} = db_insert(:user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -367,13 +354,13 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "report user > @report_threshold_for_fold will cause comment fold", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert not comment.is_folded Enum.reduce(1..(@report_threshold_for_fold + 1), [], fn _, _acc -> {:ok, user} = db_insert(:user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -389,13 +376,13 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do Enum.reduce(1..total_count, [], fn _, acc -> {:ok, new_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", new_user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), new_user) acc ++ [comment] end) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, results} = CMS.paged_article_comments_participators(thread, post.id, %{page: 1, size: page_size}) @@ -411,7 +398,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) @@ -440,13 +427,13 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do page_size = 5 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) - {:ok, random_comment_1} = CMS.create_article_comment(:post, post.id, "pin commment", user) - {:ok, random_comment_2} = CMS.create_article_comment(:post, post.id, "pin commment2", user) + {:ok, random_comment_1} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, random_comment_2} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, pined_comment_1} = CMS.pin_article_comment(random_comment_1.id) {:ok, pined_comment_2} = CMS.pin_article_comment(random_comment_2.id) @@ -472,13 +459,13 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do page_size = 5 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) - {:ok, random_comment_1} = CMS.create_article_comment(:post, post.id, "pin commment", user) - {:ok, random_comment_2} = CMS.create_article_comment(:post, post.id, "pin commment2", user) + {:ok, random_comment_1} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, random_comment_2} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, pined_comment_1} = CMS.pin_article_comment(random_comment_1.id) {:ok, pined_comment_2} = CMS.pin_article_comment(random_comment_2.id) @@ -505,7 +492,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) @@ -542,7 +529,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do all_folded_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) CMS.fold_article_comment(comment.id, user) acc ++ [comment] @@ -573,7 +560,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) @@ -596,11 +583,11 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end test "delete comment still update article's comments_count field", ~m(user post)a do - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, post} = ORM.find(Post, post.id) @@ -617,7 +604,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) @@ -634,38 +621,38 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do describe "[article comment info]" do test "author of the article comment a comment should have flag", ~m(user post)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert not comment.is_article_author author_user = post.author.user - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", author_user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), author_user) assert comment.is_article_author end end describe "[lock/unlock post comment]" do test "locked post can not be comment", ~m(user post)a do - {:ok, _} = CMS.create_article_comment(:post, post.id, "comment", user) + {:ok, _} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.lock_article_comment(:post, post.id) - {:error, reason} = CMS.create_article_comment(:post, post.id, "comment", user) + {:error, reason} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert reason |> is_error?(:article_comment_locked) {:ok, _} = CMS.undo_lock_article_comment(:post, post.id) - {:ok, _} = CMS.create_article_comment(:post, post.id, "comment", user) + {:ok, _} = CMS.create_article_comment(:post, post.id, mock_comment(), user) end test "locked post can not by reply", ~m(user post)a do - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent_conent", user) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) {:ok, _} = CMS.lock_article_comment(:post, post.id) - {:error, reason} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:error, reason} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) assert reason |> is_error?(:article_comment_locked) {:ok, _} = CMS.undo_lock_article_comment(:post, post.id) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) end end @@ -673,7 +660,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do test "create comment for normal post should have default qa flags", ~m(user community)a do post_attrs = mock_attrs(:post, %{community_id: community.id}) {:ok, post} = CMS.create_article(community, :post, post_attrs, user) - {:ok, post_comment} = CMS.create_article_comment(:post, post.id, "comment", user) + {:ok, post_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert not post_comment.is_for_question assert not post_comment.is_solution @@ -683,7 +670,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do post_attrs = mock_attrs(:post, %{community_id: community.id, is_question: true}) {:ok, post} = CMS.create_article(community, :post, post_attrs, user) - {:ok, post_comment} = CMS.create_article_comment(:post, post.id, "comment", user) + {:ok, post_comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert post_comment.is_for_question end @@ -692,9 +679,9 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do ~m(user community)a do post_attrs = mock_attrs(:post, %{community_id: community.id, is_question: true}) {:ok, post} = CMS.create_article(community, :post, post_attrs, user) - {:ok, comment1} = CMS.create_article_comment(:post, post.id, "comment", user) - {:ok, comment2} = CMS.create_article_comment(:post, post.id, "comment", user) - {:ok, comment3} = CMS.create_article_comment(:post, post.id, "comment", user) + {:ok, comment1} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, comment2} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, comment3} = CMS.create_article_comment(:post, post.id, mock_comment(), user) assert comment1.is_for_question assert comment2.is_for_question @@ -718,7 +705,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment} = CMS.create_article_comment(:post, post.id, "comment", post_author) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) {:ok, comment} = CMS.mark_comment_solution(comment.id, post_author) assert comment.is_solution @@ -736,7 +723,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do post_author = post.author.user {:ok, random_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:post, post.id, "comment", post_author) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) {:error, reason} = CMS.mark_comment_solution(comment.id, random_user) reason |> is_error?(:require_questioner) @@ -748,7 +735,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment} = CMS.create_article_comment(:post, post.id, "comment", post_author) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) {:ok, comment} = CMS.mark_comment_solution(comment.id, post_author) {:ok, comment} = CMS.undo_mark_comment_solution(comment.id, post_author) @@ -767,7 +754,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do post_author = post.author.user {:ok, random_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:post, post.id, "comment", post_author) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) {:error, reason} = CMS.undo_mark_comment_solution(comment.id, random_user) reason |> is_error?(:require_questioner) @@ -780,8 +767,8 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment1} = CMS.create_article_comment(:post, post.id, "comment", post_author) - {:ok, comment2} = CMS.create_article_comment(:post, post.id, "comment", post_author) + {:ok, comment1} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) + {:ok, comment2} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) {:ok, _comment} = CMS.mark_comment_solution(comment1.id, post_author) {:ok, comment2} = CMS.mark_comment_solution(comment2.id, post_author) @@ -801,13 +788,16 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author) + {:ok, comment} = + CMS.create_article_comment(:post, post.id, mock_comment("solution"), post_author) + {:ok, comment} = CMS.mark_comment_solution(comment.id, post_author) {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) - assert post.solution_digest == "solution" + assert post.solution_digest |> String.contains?(~s(String.contains?(~s(solution
)) - {:ok, _comment} = CMS.update_article_comment(comment, "new solution") + {:ok, _comment} = CMS.update_article_comment(comment, mock_comment("new solution")) {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) assert post.solution_digest == "new solution" end diff --git a/test/groupher_server/cms/comments/repo_comment_emotions_test.exs b/test/groupher_server/cms/comments/repo_comment_emotions_test.exs index b15ac96a5..763db442d 100644 --- a/test/groupher_server/cms/comments/repo_comment_emotions_test.exs +++ b/test/groupher_server/cms/comments/repo_comment_emotions_test.exs @@ -28,7 +28,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentEmotions do all_comment = Enum.reduce(0..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) @@ -65,9 +65,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentEmotions do describe "[basic article comment emotion]" do test "comment has default emotions after created", ~m(repo user)a do - parent_content = "parent comment" - - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) emotions = parent_comment.emotions |> Map.from_struct() |> Map.delete(:id) @@ -75,8 +73,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentEmotions do end test "can make emotion to comment", ~m(repo user user2)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) @@ -89,8 +86,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentEmotions do end test "can undo emotion to comment", ~m(repo user user2)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user2) @@ -111,8 +107,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentEmotions do end test "same user make same emotion to same comment.", ~m(repo user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) @@ -125,8 +120,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentEmotions do test "same user same emotion to same comment only have one user_emotion record", ~m(repo user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :heart, user) @@ -147,7 +141,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentEmotions do end test "different user can make same emotions on same comment", ~m(repo user user2 user3)a do - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :beer, user2) @@ -163,8 +157,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentEmotions do end test "same user can make differcent emotions on same comment", ~m(repo user)a do - parent_content = "parent comment" - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) {:ok, _} = CMS.emotion_to_comment(parent_comment.id, :downvote, user) diff --git a/test/groupher_server/cms/comments/repo_comment_replies_test.exs b/test/groupher_server/cms/comments/repo_comment_replies_test.exs index 478502970..6a8c283cc 100644 --- a/test/groupher_server/cms/comments/repo_comment_replies_test.exs +++ b/test/groupher_server/cms/comments/repo_comment_replies_test.exs @@ -20,11 +20,8 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do describe "[basic article comment replies]" do test "exsit comment can be reply", ~m(repo user user2)a do - parent_content = "parent comment" - reply_content = "reply comment" - - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) - {:ok, replyed_comment} = CMS.reply_article_comment(parent_comment.id, reply_content, user2) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, replyed_comment} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) assert replyed_comment.reply_to.id == parent_comment.id {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -33,27 +30,20 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do end test "deleted comment can not be reply", ~m(repo user user2)a do - parent_content = "parent comment" - reply_content = "reply comment" - - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.delete_article_comment(parent_comment) - {:error, _} = CMS.reply_article_comment(parent_comment.id, reply_content, user2) + {:error, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) end test "multi reply should belong to one parent comment", ~m(repo user user2)a do - parent_content = "parent comment" - reply_content_1 = "reply comment 1" - reply_content_2 = "reply comment 2" - - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, parent_content, user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, replyed_comment_1} = - CMS.reply_article_comment(parent_comment.id, reply_content_1, user2) + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, replyed_comment_2} = - CMS.reply_article_comment(parent_comment.id, reply_content_2, user2) + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -63,11 +53,16 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do test "reply to reply inside a comment should belong same parent comment", ~m(repo user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(replyed_comment_1.id, "reply 2", user2) - {:ok, replyed_comment_3} = CMS.reply_article_comment(replyed_comment_2.id, "reply 3", user) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(replyed_comment_1.id, mock_comment(), user2) + + {:ok, replyed_comment_3} = + CMS.reply_article_comment(replyed_comment_2.id, mock_comment(), user) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -88,11 +83,16 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do test "reply to reply inside a comment should have is_reply_to_others flag in meta", ~m(repo user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent comment", user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(replyed_comment_1.id, mock_comment(), user2) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(replyed_comment_1.id, "reply 2", user2) - {:ok, replyed_comment_3} = CMS.reply_article_comment(replyed_comment_2.id, "reply 3", user) + {:ok, replyed_comment_3} = + CMS.reply_article_comment(replyed_comment_2.id, mock_comment(), user) {:ok, _parent_comment} = ORM.find(ArticleComment, parent_comment.id) @@ -108,12 +108,12 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do test "comment replies only contains @max_parent_replies_count replies", ~m(repo user)a do total_reply_count = @max_parent_replies_count + 1 - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) reply_comment_list = Enum.reduce(1..total_reply_count, [], fn n, acc -> {:ok, replyed_comment} = - CMS.reply_article_comment(parent_comment.id, "reply_content_#{n}", user) + CMS.reply_article_comment(parent_comment.id, mock_comment("reply_content_#{n}"), user) acc ++ [replyed_comment] end) @@ -128,8 +128,8 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do end test "replyed user should appear in article comment participators", ~m(repo user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent_conent", user) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, article} = ORM.find(Repo, repo.id) @@ -138,14 +138,14 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do end test "replies count should inc by 1 after got replyed", ~m(repo user user2)a do - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) assert parent_comment.replies_count === 0 - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) assert parent_comment.replies_count === 1 - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user2) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) {:ok, parent_comment} = ORM.find(ArticleComment, parent_comment.id) assert parent_comment.replies_count === 2 end @@ -153,7 +153,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do describe "[paged article comment replies]" do test "can get paged replies of a parent comment", ~m(repo user)a do - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, paged_replies} = CMS.paged_comment_replies(parent_comment.id, %{page: 1, size: 20}) assert is_valid_pagination?(paged_replies, :raw, :empty) @@ -162,7 +162,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do reply_comment_list = Enum.reduce(1..total_reply_count, [], fn n, acc -> {:ok, replyed_comment} = - CMS.reply_article_comment(parent_comment.id, "reply_content_#{n}", user) + CMS.reply_article_comment(parent_comment.id, mock_comment("reply_content_#{n}"), user) acc ++ [replyed_comment] end) @@ -182,12 +182,11 @@ defmodule GroupherServer.Test.CMS.Comments.RepoCommentReplies do page_number = 1 page_size = 10 - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent_conent", user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) - {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, "reply_content_1", user) + {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) - {:ok, reply_comment2} = - CMS.reply_article_comment(parent_comment.id, "reply_content_2", user) + {:ok, reply_comment2} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) {:ok, paged_comments} = CMS.paged_article_comments( diff --git a/test/groupher_server/cms/comments/repo_comment_test.exs b/test/groupher_server/cms/comments/repo_comment_test.exs index 573b633b7..8d080b1bb 100644 --- a/test/groupher_server/cms/comments/repo_comment_test.exs +++ b/test/groupher_server/cms/comments/repo_comment_test.exs @@ -27,8 +27,8 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do describe "[basic article comment]" do test "repo are supported by article comment.", ~m(user repo)a do - {:ok, repo_comment_1} = CMS.create_article_comment(:repo, repo.id, "repo_comment 1", user) - {:ok, repo_comment_2} = CMS.create_article_comment(:repo, repo.id, "repo_comment 2", user) + {:ok, repo_comment_1} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, repo_comment_2} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, repo} = ORM.find(Repo, repo.id, preload: :article_comments) @@ -37,13 +37,13 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "comment should have default meta after create", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) assert comment.meta |> Map.from_struct() |> Map.delete(:id) == @default_comment_meta end test "create comment should update active timestamp of repo", ~m(user repo)a do Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, repo} = ORM.find(Repo, repo.id, preload: :article_comments) assert not is_nil(repo.active_at) @@ -58,7 +58,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do Process.sleep(1000) {:ok, _comment} = - CMS.create_article_comment(:repo, repo.id, "repo comment", repo.author.user) + CMS.create_article_comment(:repo, repo.id, mock_comment(), repo.author.user) {:ok, repo} = ORM.find(Repo, repo.id, preload: :article_comments) @@ -74,7 +74,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do {:ok, repo} = db_insert(:repo, %{inserted_at: inserted_at}) Process.sleep(1000) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, repo} = ORM.find(Repo, repo.id) assert repo.active_at |> DateTime.to_date() == DateTime.utc_now() |> DateTime.to_date() @@ -85,25 +85,26 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do {:ok, repo} = db_insert(:repo, %{inserted_at: inserted_at}) Process.sleep(3000) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, repo} = ORM.find(Repo, repo.id) assert repo.active_at |> DateTime.to_unix() !== DateTime.utc_now() |> DateTime.to_unix() end test "comment can be updated", ~m(repo user)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) - {:ok, updated_comment} = CMS.update_article_comment(comment, "updated content") + {:ok, updated_comment} = + CMS.update_article_comment(comment, mock_comment("updated content")) - assert updated_comment.body_html == "updated content" + assert updated_comment.body_html |> String.contains?(~s(updated content)) end end describe "[article comment floor]" do test "comment will have a floor number after created", ~m(user repo)a do - {:ok, repo_comment} = CMS.create_article_comment(:repo, repo.id, "comment", user) - {:ok, repo_comment2} = CMS.create_article_comment(:repo, repo.id, "comment2", user) + {:ok, repo_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, repo_comment2} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, repo_comment} = ORM.find(ArticleComment, repo_comment.id) {:ok, repo_comment2} = ORM.find(ArticleComment, repo_comment2.id) @@ -115,9 +116,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do describe "[article comment participator for repo]" do test "repo will have participator after comment created", ~m(user repo)a do - repo_comment_1 = "repo_comment 1" - - {:ok, _} = CMS.create_article_comment(:repo, repo.id, repo_comment_1, user) + {:ok, _} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, repo} = ORM.find(Repo, repo.id) @@ -126,10 +125,8 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "psot participator will not contains same user", ~m(user repo)a do - repo_comment_1 = "repo_comment 1" - - {:ok, _} = CMS.create_article_comment(:repo, repo.id, repo_comment_1, user) - {:ok, _} = CMS.create_article_comment(:repo, repo.id, repo_comment_1, user) + {:ok, _} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, repo} = ORM.find(Repo, repo.id) @@ -138,10 +135,8 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do test "recent comment user should appear at first of the psot participators", ~m(user user2 repo)a do - repo_comment_1 = "repo_comment 1" - - {:ok, _} = CMS.create_article_comment(:repo, repo.id, repo_comment_1, user) - {:ok, _} = CMS.create_article_comment(:repo, repo.id, repo_comment_1, user2) + {:ok, _} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user2) {:ok, repo} = ORM.find(Repo, repo.id) @@ -153,8 +148,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do describe "[article comment upvotes]" do test "user can upvote a repo comment", ~m(user repo)a do - comment = "repo_comment" - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) @@ -165,8 +159,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "article author upvote repo comment will have flag", ~m(repo user)a do - comment = "repo_comment" - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, author_user} = ORM.find(User, repo.author.user.id) CMS.upvote_article_comment(comment.id, author_user) @@ -176,8 +169,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "user upvote repo comment will add id to upvoted_user_ids", ~m(repo user)a do - comment = "repo_comment" - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, comment} = CMS.upvote_article_comment(comment.id, user) assert user.id in comment.meta.upvoted_user_ids @@ -185,8 +177,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do test "user undo upvote repo comment will remove id from upvoted_user_ids", ~m(repo user user2)a do - comment = "repo_comment" - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _comment} = CMS.upvote_article_comment(comment.id, user) {:ok, comment} = CMS.upvote_article_comment(comment.id, user2) @@ -200,16 +191,14 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "user upvote a already-upvoted comment fails", ~m(user repo)a do - comment = "repo_comment" - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) {:error, _} = CMS.upvote_article_comment(comment.id, user) end test "upvote comment should inc the comment's upvotes_count", ~m(user user2 repo)a do - comment = "repo_comment" - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, comment, user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert comment.upvotes_count == 0 @@ -221,8 +210,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "user can undo upvote a repo comment", ~m(user repo)a do - content = "repo_comment" - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, content, user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) CMS.upvote_article_comment(comment.id, user) {:ok, comment} = ORM.find(ArticleComment, comment.id, preload: :upvotes) @@ -233,8 +221,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "user can undo upvote a repo comment with no upvote", ~m(user repo)a do - content = "repo_comment" - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, content, user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, comment} = CMS.undo_upvote_article_comment(comment.id, user) assert 0 == comment.upvotes_count @@ -245,7 +232,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do describe "[article comment fold/unfold]" do test "user can fold a comment", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert not comment.is_folded @@ -256,7 +243,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "user can unfold a comment", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _comment} = CMS.fold_article_comment(comment.id, user) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -270,7 +257,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do describe "[article comment pin/unpin]" do test "user can pin a comment", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, comment} = ORM.find(ArticleComment, comment.id) assert not comment.is_pinned @@ -285,7 +272,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "user can unpin a comment", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _comment} = CMS.pin_article_comment(comment.id) {:ok, comment} = CMS.undo_pin_article_comment(comment.id) @@ -295,7 +282,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "pinned comments has a limit for each article", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) Enum.reduce(0..(@pinned_comment_limit - 1), [], fn _, _acc -> {:ok, _comment} = CMS.pin_article_comment(comment.id) @@ -308,17 +295,17 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do describe "[article comment report/unreport]" do # # test "user can report a comment", ~m(user repo)a do - # {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + # {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) - # {:ok, comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + # {:ok, comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) # end # # test "user can unreport a comment", ~m(user repo)a do - # {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) - # {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + # {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + # {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) # {:ok, comment} = ORM.find(ArticleComment, comment.id) # {:ok, _comment} = CMS.undo_report_article_comment(comment.id, user) @@ -326,10 +313,10 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do # end test "can undo a report with other user report it too", ~m(user user2 repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user2) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user2) filter = %{content_type: :article_comment, content_id: comment.id, page: 1, size: 20} {:ok, all_reports} = CMS.paged_reports(filter) @@ -353,13 +340,13 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "report user < @report_threshold_for_fold will not fold comment", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) assert not comment.is_folded Enum.reduce(1..(@report_threshold_for_fold - 1), [], fn _, _acc -> {:ok, user} = db_insert(:user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -367,13 +354,13 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "report user > @report_threshold_for_fold will cause comment fold", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) assert not comment.is_folded Enum.reduce(1..(@report_threshold_for_fold + 1), [], fn _, _acc -> {:ok, user} = db_insert(:user) - {:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, _comment} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) end) {:ok, comment} = ORM.find(ArticleComment, comment.id) @@ -389,13 +376,13 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do Enum.reduce(1..total_count, [], fn _, acc -> {:ok, new_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", new_user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), new_user) acc ++ [comment] end) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, results} = CMS.paged_article_comments_participators(thread, repo.id, %{page: 1, size: page_size}) @@ -411,7 +398,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) @@ -440,13 +427,13 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do page_size = 5 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) - {:ok, random_comment_1} = CMS.create_article_comment(:repo, repo.id, "pin commment", user) - {:ok, random_comment_2} = CMS.create_article_comment(:repo, repo.id, "pin commment2", user) + {:ok, random_comment_1} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, random_comment_2} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, pined_comment_1} = CMS.pin_article_comment(random_comment_1.id) {:ok, pined_comment_2} = CMS.pin_article_comment(random_comment_2.id) @@ -472,13 +459,13 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do page_size = 5 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) - {:ok, random_comment_1} = CMS.create_article_comment(:repo, repo.id, "pin commment", user) - {:ok, random_comment_2} = CMS.create_article_comment(:repo, repo.id, "pin commment2", user) + {:ok, random_comment_1} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, random_comment_2} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, pined_comment_1} = CMS.pin_article_comment(random_comment_1.id) {:ok, pined_comment_2} = CMS.pin_article_comment(random_comment_2.id) @@ -505,7 +492,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) @@ -542,7 +529,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do all_folded_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) CMS.fold_article_comment(comment.id, user) acc ++ [comment] @@ -573,7 +560,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) @@ -596,11 +583,11 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end test "delete comment still update article's comments_count field", ~m(user repo)a do - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, repo} = ORM.find(Repo, repo.id) @@ -617,7 +604,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do all_comments = Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) @@ -634,38 +621,38 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do describe "[article comment info]" do test "author of the article comment a comment should have flag", ~m(user repo)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) assert not comment.is_article_author author_user = repo.author.user - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", author_user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), author_user) assert comment.is_article_author end end describe "[lock/unlock repo comment]" do test "locked repo can not be comment", ~m(user repo)a do - {:ok, _} = CMS.create_article_comment(:repo, repo.id, "comment", user) + {:ok, _} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.lock_article_comment(:repo, repo.id) - {:error, reason} = CMS.create_article_comment(:repo, repo.id, "comment", user) + {:error, reason} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) assert reason |> is_error?(:article_comment_locked) {:ok, _} = CMS.undo_lock_article_comment(:repo, repo.id) - {:ok, _} = CMS.create_article_comment(:repo, repo.id, "comment", user) + {:ok, _} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) end test "locked repo can not by reply", ~m(user repo)a do - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent_conent", user) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) {:ok, _} = CMS.lock_article_comment(:repo, repo.id) - {:error, reason} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:error, reason} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) assert reason |> is_error?(:article_comment_locked) {:ok, _} = CMS.undo_lock_article_comment(:repo, repo.id) - {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, mock_comment(), user) end end end diff --git a/test/groupher_server_web/mutation/cms/articles/blog_test.exs b/test/groupher_server_web/mutation/cms/articles/blog_test.exs index 051c3242e..2fbc893db 100644 --- a/test/groupher_server_web/mutation/cms/articles/blog_test.exs +++ b/test/groupher_server_web/mutation/cms/articles/blog_test.exs @@ -72,13 +72,27 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do user_conn = simu_conn(:user, user) {:ok, community} = db_insert(:community) - blog_attr = mock_attrs(:blog, %{body: assert_v(:xss_string)}) + blog_attr = mock_attrs(:blog, %{body: mock_xss_string()}) variables = blog_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key created = user_conn |> mutation_result(@create_blog_query, variables, "createBlog") {:ok, blog} = ORM.find(Blog, created["id"]) - assert blog.body == assert_v(:xss_safe_string) + assert not String.contains?(blog.body_html, "script") + end + + test "create blog should excape xss attracts 2" do + {:ok, user} = db_insert(:user) + user_conn = simu_conn(:user, user) + + {:ok, community} = db_insert(:community) + + blog_attr = mock_attrs(:blog, %{body: mock_xss_string(:safe)}) + variables = blog_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key + created = user_conn |> mutation_result(@create_blog_query, variables, "createBlog") + {:ok, blog} = ORM.find(Blog, created["id"]) + + assert String.contains?(blog.body_html, "<script>blackmail</script>") end @query """ @@ -87,6 +101,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do id title body + bodyHtml articleTags { id } @@ -100,7 +115,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do variables = %{ id: blog.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) @@ -112,13 +127,13 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do variables = %{ id: blog.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } updated = owner_conn |> mutation_result(@query, variables, "updateBlog") assert updated["title"] == variables.title - assert updated["body"] == variables.body + assert updated["bodyHtml"] |> String.contains?(~s(updated body #{unique_num})) end test "login user with auth passport update a blog", ~m(blog)a do @@ -131,7 +146,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do variables = %{ id: blog.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } updated = rule_conn |> mutation_result(@query, variables, "updateBlog") @@ -145,7 +160,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do variables = %{ id: blog.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) diff --git a/test/groupher_server_web/mutation/cms/articles/job_test.exs b/test/groupher_server_web/mutation/cms/articles/job_test.exs index dc7185ce9..1e3f6ad57 100644 --- a/test/groupher_server_web/mutation/cms/articles/job_test.exs +++ b/test/groupher_server_web/mutation/cms/articles/job_test.exs @@ -77,13 +77,27 @@ defmodule GroupherServer.Test.Mutation.Articles.Job do user_conn = simu_conn(:user, user) {:ok, community} = db_insert(:community) - job_attr = mock_attrs(:job, %{body: assert_v(:xss_string)}) + job_attr = mock_attrs(:job, %{body: mock_xss_string()}) variables = job_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key created = user_conn |> mutation_result(@create_job_query, variables, "createJob") {:ok, job} = ORM.find(Job, created["id"]) - assert job.body == assert_v(:xss_safe_string) + assert not String.contains?(job.body_html, "script") + end + + test "create job should excape xss attracts 2" do + {:ok, user} = db_insert(:user) + user_conn = simu_conn(:user, user) + + {:ok, community} = db_insert(:community) + + job_attr = mock_attrs(:job, %{body: mock_xss_string(:safe)}) + variables = job_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key + created = user_conn |> mutation_result(@create_job_query, variables, "createJob") + {:ok, job} = ORM.find(Job, created["id"]) + + assert String.contains?(job.body_html, "<script>blackmail</script>") end test "can create job with mentionUsers" do @@ -120,6 +134,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Job do id title body + bodyHtml articleTags { id } @@ -132,7 +147,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Job do variables = %{ id: job.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) @@ -144,13 +159,13 @@ defmodule GroupherServer.Test.Mutation.Articles.Job do variables = %{ id: job.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } updated = owner_conn |> mutation_result(@query, variables, "updateJob") assert updated["title"] == variables.title - assert updated["body"] == variables.body + assert updated["bodyHtml"] |> String.contains?(~s(updated body #{unique_num})) end test "login user with auth passport update a job", ~m(job)a do @@ -163,7 +178,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Job do variables = %{ id: job.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } updated = rule_conn |> mutation_result(@query, variables, "updateJob") @@ -177,7 +192,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Job do variables = %{ id: job.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) diff --git a/test/groupher_server_web/mutation/cms/articles/post_test.exs b/test/groupher_server_web/mutation/cms/articles/post_test.exs index 00af5cf9d..8ddf617fa 100644 --- a/test/groupher_server_web/mutation/cms/articles/post_test.exs +++ b/test/groupher_server_web/mutation/cms/articles/post_test.exs @@ -68,13 +68,27 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do user_conn = simu_conn(:user, user) {:ok, community} = db_insert(:community) - post_attr = mock_attrs(:post, %{body: assert_v(:xss_string)}) - variables = post_attr |> Map.merge(%{communityId: community.id}) + post_attr = mock_attrs(:post, %{body: mock_xss_string()}) + variables = post_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key + created = user_conn |> mutation_result(@create_post_query, variables, "createPost") + {:ok, post} = ORM.find(Post, created["id"]) + + assert not String.contains?(post.body_html, "script") + end + + test "create post should excape xss attracts 2" do + {:ok, user} = db_insert(:user) + user_conn = simu_conn(:user, user) + + {:ok, community} = db_insert(:community) + + post_attr = mock_attrs(:post, %{body: mock_xss_string(:safe)}) + variables = post_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key created = user_conn |> mutation_result(@create_post_query, variables, "createPost") {:ok, post} = ORM.find(Post, created["id"]) - assert post.body == assert_v(:xss_safe_string) + assert String.contains?(post.body_html, "<script>blackmail</script>") end # NOTE: this test is IMPORTANT, cause json_codec: Jason in router will cause @@ -171,6 +185,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do id title body + bodyHtml copyRight meta { isEdited @@ -185,13 +200,14 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do } } """ + test "update a post without login user fails", ~m(guest_conn post)a do unique_num = System.unique_integer([:positive, :monotonic]) variables = %{ id: post.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) @@ -203,14 +219,15 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do variables = %{ id: post.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}", + # body: mock_rich_text("updated body #{unique_num}"),, + body: mock_rich_text("updated body #{unique_num}"), copyRight: "translate" } updated_post = owner_conn |> mutation_result(@query, variables, "updatePost") assert updated_post["title"] == variables.title - assert updated_post["body"] == variables.body + assert updated_post["bodyHtml"] |> String.contains?(~s(updated body #{unique_num})) assert updated_post["copyRight"] == variables.copyRight end @@ -221,7 +238,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do variables = %{ id: post.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } updated_post = owner_conn |> mutation_result(@query, variables, "updatePost") @@ -241,7 +258,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do variables = %{ id: post.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } updated_post = rule_conn |> mutation_result(@query, variables, "updatePost") @@ -255,7 +272,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do variables = %{ id: post.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) diff --git a/test/groupher_server_web/mutation/cms/articles/repo_test.exs b/test/groupher_server_web/mutation/cms/articles/repo_test.exs index b3217eb16..bd6be0065 100644 --- a/test/groupher_server_web/mutation/cms/articles/repo_test.exs +++ b/test/groupher_server_web/mutation/cms/articles/repo_test.exs @@ -160,13 +160,13 @@ defmodule GroupherServer.Test.Mutation.Articles.Repo do user_conn = simu_conn(:user, user) {:ok, community} = db_insert(:community) - repo_attr = mock_attrs(:repo, %{readme: assert_v(:xss_string)}) + repo_attr = mock_attrs(:repo, %{body: mock_xss_string()}) variables = repo_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key created = user_conn |> mutation_result(@create_repo_query, variables, "createRepo") {:ok, repo} = ORM.find(Repo, created["id"]) - assert repo.readme == assert_v(:xss_safe_string) + assert not String.contains?(repo.body_html, "script") end test "unauth user update git-repo fails", ~m(user_conn guest_conn repo)a do @@ -175,7 +175,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Repo do variables = %{ id: repo.id, title: "updated title #{unique_num}", - body: "updated body #{unique_num}" + body: mock_rich_text("updated body #{unique_num}") } rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) diff --git a/test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs index 94f63e5e6..7fb808968 100644 --- a/test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs @@ -20,64 +20,67 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do describe "[article comment CURD]" do @write_comment_query """ - mutation($thread: Thread!, $id: ID!, $content: String!) { - createArticleComment(thread: $thread,id: $id, content: $content) { + mutation($thread: Thread!, $id: ID!, $body: String!) { + createArticleComment(thread: $thread,id: $id, body: $body) { id bodyHtml } } """ test "write article comment to a exsit blog", ~m(blog user_conn)a do - comment = "a test comment" - variables = %{thread: "BLOG", id: blog.id, content: comment} + variables = %{thread: "BLOG", id: blog.id, body: mock_comment()} result = user_conn |> mutation_result(@write_comment_query, variables, "createArticleComment") - assert result["bodyHtml"] == comment + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(comment
)) end @reply_comment_query """ - mutation($id: ID!, $content: String!) { - replyArticleComment(id: $id, content: $content) { + mutation($id: ID!, $body: String!) { + replyArticleComment(id: $id, body: $body) { id bodyHtml } } """ test "login user can reply to a comment", ~m(blog user user_conn)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "commment", user) - variables = %{id: comment.id, content: "reply content"} + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + variables = %{id: comment.id, body: mock_comment("reply comment")} result = user_conn |> mutation_result(@reply_comment_query, variables, "replyArticleComment") - assert result["bodyHtml"] == "reply content" + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(reply comment
)) end @update_comment_query """ - mutation($id: ID!, $content: String!) { - updateArticleComment(id: $id, content: $content) { + mutation($id: ID!, $body: String!) { + updateArticleComment(id: $id, body: $body) { id bodyHtml } } """ + test "only owner can update a exsit comment", ~m(blog user guest_conn user_conn owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) - variables = %{id: comment.id, content: "updated comment"} + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) + variables = %{id: comment.id, body: mock_comment("updated comment")} assert user_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:passport)) assert guest_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:account_login)) - updated = + result = owner_conn |> mutation_result(@update_comment_query, variables, "updateArticleComment") - assert updated["bodyHtml"] == "updated comment" + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(updated comment
)) end @delete_comment_query """ @@ -90,7 +93,7 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do """ test "only owner can delete a exsit comment", ~m(blog user guest_conn user_conn owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) variables = %{id: comment.id} assert user_conn |> mutation_get_error?(@delete_comment_query, variables, ecode(:passport)) @@ -118,7 +121,7 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do """ test "login user can upvote a exsit blog comment", ~m(blog user guest_conn user_conn)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) variables = %{id: comment.id} assert guest_conn @@ -143,7 +146,7 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do """ test "login user can undo upvote a exsit blog comment", ~m(blog user guest_conn user_conn)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) variables = %{id: comment.id} user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment") @@ -176,7 +179,7 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do } """ test "login user can emotion to a comment", ~m(blog user user_conn)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) variables = %{id: comment.id, emotion: "BEER"} comment = @@ -202,7 +205,7 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do } """ test "login user can undo emotion to a comment", ~m(blog user owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(comment.id, :beer, user) variables = %{id: comment.id, emotion: "BEER"} diff --git a/test/groupher_server_web/mutation/cms/comments/job_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/job_comment_test.exs index 7e581e5d9..67e92ea72 100644 --- a/test/groupher_server_web/mutation/cms/comments/job_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/job_comment_test.exs @@ -20,64 +20,67 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do describe "[article comment CURD]" do @write_comment_query """ - mutation($thread: Thread!, $id: ID!, $content: String!) { - createArticleComment(thread: $thread,id: $id, content: $content) { + mutation($thread: Thread!, $id: ID!, $body: String!) { + createArticleComment(thread: $thread,id: $id, body: $body) { id bodyHtml } } """ test "write article comment to a exsit job", ~m(job user_conn)a do - comment = "a test comment" - variables = %{thread: "JOB", id: job.id, content: comment} + variables = %{thread: "JOB", id: job.id, body: mock_comment()} result = user_conn |> mutation_result(@write_comment_query, variables, "createArticleComment") - assert result["bodyHtml"] == comment + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(comment
)) end @reply_comment_query """ - mutation($id: ID!, $content: String!) { - replyArticleComment(id: $id, content: $content) { + mutation($id: ID!, $body: String!) { + replyArticleComment(id: $id, body: $body) { id bodyHtml } } """ test "login user can reply to a comment", ~m(job user user_conn)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) - variables = %{id: comment.id, content: "reply content"} + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + variables = %{id: comment.id, body: mock_comment("reply comment")} result = user_conn |> mutation_result(@reply_comment_query, variables, "replyArticleComment") - assert result["bodyHtml"] == "reply content" + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(reply comment
)) end @update_comment_query """ - mutation($id: ID!, $content: String!) { - updateArticleComment(id: $id, content: $content) { + mutation($id: ID!, $body: String!) { + updateArticleComment(id: $id, body: $body) { id bodyHtml } } """ + test "only owner can update a exsit comment", ~m(job user guest_conn user_conn owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) - variables = %{id: comment.id, content: "updated comment"} + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + variables = %{id: comment.id, body: mock_comment("updated comment")} assert user_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:passport)) assert guest_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:account_login)) - updated = + result = owner_conn |> mutation_result(@update_comment_query, variables, "updateArticleComment") - assert updated["bodyHtml"] == "updated comment" + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(updated comment
)) end @delete_comment_query """ @@ -90,7 +93,7 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do """ test "only owner can delete a exsit comment", ~m(job user guest_conn user_conn owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) variables = %{id: comment.id} assert user_conn |> mutation_get_error?(@delete_comment_query, variables, ecode(:passport)) @@ -118,7 +121,7 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do """ test "login user can upvote a exsit job comment", ~m(job user guest_conn user_conn)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) variables = %{id: comment.id} assert guest_conn @@ -143,7 +146,7 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do """ test "login user can undo upvote a exsit job comment", ~m(job user guest_conn user_conn)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) variables = %{id: comment.id} user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment") @@ -176,7 +179,7 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do } """ test "login user can emotion to a comment", ~m(job user user_conn)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) variables = %{id: comment.id, emotion: "BEER"} comment = @@ -202,7 +205,7 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do } """ test "login user can undo emotion to a comment", ~m(job user owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(comment.id, :beer, user) variables = %{id: comment.id, emotion: "BEER"} diff --git a/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs index ae12d58b8..32cb628de 100644 --- a/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs @@ -20,64 +20,67 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do describe "[article comment CURD]" do @write_comment_query """ - mutation($thread: Thread!, $id: ID!, $content: String!) { - createArticleComment(thread: $thread,id: $id, content: $content) { + mutation($thread: Thread!, $id: ID!, $body: String!) { + createArticleComment(thread: $thread,id: $id, body: $body) { id bodyHtml } } """ test "write article comment to a exsit post", ~m(post user_conn)a do - comment = "a test comment" - variables = %{thread: "POST", id: post.id, content: comment} + variables = %{thread: "POST", id: post.id, body: mock_comment()} result = user_conn |> mutation_result(@write_comment_query, variables, "createArticleComment") - assert result["bodyHtml"] == comment + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(comment
)) end @reply_comment_query """ - mutation($id: ID!, $content: String!) { - replyArticleComment(id: $id, content: $content) { + mutation($id: ID!, $body: String!) { + replyArticleComment(id: $id, body: $body) { id bodyHtml } } """ test "login user can reply to a comment", ~m(post user user_conn)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) - variables = %{id: comment.id, content: "reply content"} + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + variables = %{id: comment.id, body: mock_comment("reply comment")} result = user_conn |> mutation_result(@reply_comment_query, variables, "replyArticleComment") - assert result["bodyHtml"] == "reply content" + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(reply comment
)) end @update_comment_query """ - mutation($id: ID!, $content: String!) { - updateArticleComment(id: $id, content: $content) { + mutation($id: ID!, $body: String!) { + updateArticleComment(id: $id, body: $body) { id bodyHtml } } """ + test "only owner can update a exsit comment", ~m(post user guest_conn user_conn owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) - variables = %{id: comment.id, content: "updated comment"} + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + variables = %{id: comment.id, body: mock_comment("updated comment")} assert user_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:passport)) assert guest_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:account_login)) - updated = + result = owner_conn |> mutation_result(@update_comment_query, variables, "updateArticleComment") - assert updated["bodyHtml"] == "updated comment" + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(updated comment
)) end @delete_comment_query """ @@ -90,7 +93,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do """ test "only owner can delete a exsit comment", ~m(post user guest_conn user_conn owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) variables = %{id: comment.id} assert user_conn |> mutation_get_error?(@delete_comment_query, variables, ecode(:passport)) @@ -118,7 +121,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do """ test "login user can upvote a exsit post comment", ~m(post user guest_conn user_conn)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) variables = %{id: comment.id} assert guest_conn @@ -143,7 +146,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do """ test "login user can undo upvote a exsit post comment", ~m(post user guest_conn user_conn)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) variables = %{id: comment.id} user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment") @@ -176,7 +179,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do } """ test "login user can emotion to a comment", ~m(post user user_conn)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) variables = %{id: comment.id, emotion: "BEER"} comment = @@ -202,7 +205,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do } """ test "login user can undo emotion to a comment", ~m(post user owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(comment.id, :beer, user) variables = %{id: comment.id, emotion: "BEER"} @@ -288,7 +291,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do test "questioner can mark a post comment as solution", ~m(post)a do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) questioner_conn = simu_conn(:user, post_author) @@ -303,7 +306,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do test "other user can not mark a post comment as solution", ~m(guest_conn user_conn post)a do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) variables = %{id: comment.id} assert user_conn |> mutation_get_error?(@query, variables, ecode(:require_questioner)) @@ -323,7 +326,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do test "questioner can undo mark a post comment as solution", ~m(post)a do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) {:ok, comment} = CMS.mark_comment_solution(comment.id, post_author) questioner_conn = simu_conn(:user, post_author) @@ -339,7 +342,7 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do ~m(guest_conn user_conn post)a do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), post_author) variables = %{id: comment.id} assert user_conn |> mutation_get_error?(@query, variables, ecode(:require_questioner)) diff --git a/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs index 18d12dcab..9cd8e13cb 100644 --- a/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs @@ -20,64 +20,67 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do describe "[article comment CURD]" do @write_comment_query """ - mutation($thread: Thread!, $id: ID!, $content: String!) { - createArticleComment(thread: $thread,id: $id, content: $content) { + mutation($thread: Thread!, $id: ID!, $body: String!) { + createArticleComment(thread: $thread,id: $id, body: $body) { id bodyHtml } } """ test "write article comment to a exsit repo", ~m(repo user_conn)a do - comment = "a test comment" - variables = %{thread: "REPO", id: repo.id, content: comment} + variables = %{thread: "REPO", id: repo.id, body: mock_comment()} result = user_conn |> mutation_result(@write_comment_query, variables, "createArticleComment") - assert result["bodyHtml"] == comment + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(comment
)) end @reply_comment_query """ - mutation($id: ID!, $content: String!) { - replyArticleComment(id: $id, content: $content) { + mutation($id: ID!, $body: String!) { + replyArticleComment(id: $id, body: $body) { id bodyHtml } } """ test "login user can reply to a comment", ~m(repo user user_conn)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) - variables = %{id: comment.id, content: "reply content"} + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + variables = %{id: comment.id, body: mock_comment("reply comment")} result = user_conn |> mutation_result(@reply_comment_query, variables, "replyArticleComment") - assert result["bodyHtml"] == "reply content" + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(reply comment
)) end @update_comment_query """ - mutation($id: ID!, $content: String!) { - updateArticleComment(id: $id, content: $content) { + mutation($id: ID!, $body: String!) { + updateArticleComment(id: $id, body: $body) { id bodyHtml } } """ + test "only owner can update a exsit comment", ~m(repo user guest_conn user_conn owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) - variables = %{id: comment.id, content: "updated comment"} + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + variables = %{id: comment.id, body: mock_comment("updated comment")} assert user_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:passport)) assert guest_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:account_login)) - updated = + result = owner_conn |> mutation_result(@update_comment_query, variables, "updateArticleComment") - assert updated["bodyHtml"] == "updated comment" + assert result["bodyHtml"] |> String.contains?(~s(String.contains?(~s(updated comment
)) end @delete_comment_query """ @@ -90,7 +93,7 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do """ test "only owner can delete a exsit comment", ~m(repo user guest_conn user_conn owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) variables = %{id: comment.id} assert user_conn |> mutation_get_error?(@delete_comment_query, variables, ecode(:passport)) @@ -118,7 +121,7 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do """ test "login user can upvote a exsit repo comment", ~m(repo user guest_conn user_conn)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) variables = %{id: comment.id} assert guest_conn @@ -143,7 +146,7 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do """ test "login user can undo upvote a exsit repo comment", ~m(repo user guest_conn user_conn)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) variables = %{id: comment.id} user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment") @@ -176,7 +179,7 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do } """ test "login user can emotion to a comment", ~m(repo user user_conn)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) variables = %{id: comment.id, emotion: "BEER"} comment = @@ -202,7 +205,7 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do } """ test "login user can undo emotion to a comment", ~m(repo user owner_conn)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) {:ok, _} = CMS.emotion_to_comment(comment.id, :beer, user) variables = %{id: comment.id, emotion: "BEER"} diff --git a/test/groupher_server_web/mutation/statistics/statistics_test.exs b/test/groupher_server_web/mutation/statistics/statistics_test.exs index 060235f87..4fbe0b2c2 100644 --- a/test/groupher_server_web/mutation/statistics/statistics_test.exs +++ b/test/groupher_server_web/mutation/statistics/statistics_test.exs @@ -189,16 +189,17 @@ defmodule GroupherServer.Test.Mutation.Statistics do end @write_comment_query """ - mutation($thread: Thread!, $id: ID!, $content: String!) { - createArticleComment(thread: $thread, id: $id, content: $content) { + mutation($thread: Thread!, $id: ID!, $body: String!) { + createArticleComment(thread: $thread, id: $id, body: $body) { id bodyHtml } } """ + test "user should have contribute list after create a comment", ~m(user_conn user)a do {:ok, post} = db_insert(:post) - variables = %{thread: "POST", id: post.id, content: "comment"} + variables = %{thread: "POST", id: post.id, body: mock_comment()} user_conn |> mutation_result(@write_comment_query, variables, "createArticleComment") {:ok, contributes} = ORM.find_by(UserContribute, user_id: user.id) diff --git a/test/groupher_server_web/query/accounts/published/published_blogs_test.exs b/test/groupher_server_web/query/accounts/published/published_blogs_test.exs index 4e66fa87e..3fbf1bbde 100644 --- a/test/groupher_server_web/query/accounts/published/published_blogs_test.exs +++ b/test/groupher_server_web/query/accounts/published/published_blogs_test.exs @@ -78,7 +78,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Blogs do test "user can get paged published comments on blog", ~m(guest_conn user blog)a do pub_comments = Enum.reduce(1..@publish_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:blog, blog.id, "comment", user) + {:ok, comment} = CMS.create_article_comment(:blog, blog.id, mock_comment(), user) acc ++ [comment] end) diff --git a/test/groupher_server_web/query/accounts/published/published_jobs_test.exs b/test/groupher_server_web/query/accounts/published/published_jobs_test.exs index 7da20bf96..3e3f36634 100644 --- a/test/groupher_server_web/query/accounts/published/published_jobs_test.exs +++ b/test/groupher_server_web/query/accounts/published/published_jobs_test.exs @@ -79,7 +79,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do test "user can get paged published comments on job", ~m(guest_conn user job)a do pub_comments = Enum.reduce(1..@publish_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:job, job.id, "comment", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) acc ++ [comment] end) diff --git a/test/groupher_server_web/query/accounts/published/published_posts_test.exs b/test/groupher_server_web/query/accounts/published/published_posts_test.exs index 7bf490ad5..3ee2a0557 100644 --- a/test/groupher_server_web/query/accounts/published/published_posts_test.exs +++ b/test/groupher_server_web/query/accounts/published/published_posts_test.exs @@ -79,7 +79,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do test "user can get paged published comments on post", ~m(guest_conn user post)a do pub_comments = Enum.reduce(1..@publish_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:post, post.id, "comment", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) acc ++ [comment] end) diff --git a/test/groupher_server_web/query/accounts/published/published_repos_test.exs b/test/groupher_server_web/query/accounts/published/published_repos_test.exs index 19227f564..72e43b730 100644 --- a/test/groupher_server_web/query/accounts/published/published_repos_test.exs +++ b/test/groupher_server_web/query/accounts/published/published_repos_test.exs @@ -79,7 +79,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Repos do test "user can get paged published comments on repo", ~m(guest_conn user repo)a do pub_comments = Enum.reduce(1..@publish_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "comment", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) acc ++ [comment] end) diff --git a/test/groupher_server_web/query/cms/abuse_reports/job_report_test.exs b/test/groupher_server_web/query/cms/abuse_reports/job_report_test.exs index 794f9c77a..4041f6ee1 100644 --- a/test/groupher_server_web/query/cms/abuse_reports/job_report_test.exs +++ b/test/groupher_server_web/query/cms/abuse_reports/job_report_test.exs @@ -92,8 +92,8 @@ defmodule GroupherServer.Test.Query.AbuseReports.JobReport do end test "support article_comment", ~m(guest_conn job user)a do - {:ok, comment} = CMS.create_article_comment(:job, job.id, "comment", user) - {:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) variables = %{filter: %{content_type: "ARTICLE_COMMENT", page: 1, size: 10}} results = guest_conn |> query_result(@query, variables, "pagedAbuseReports") @@ -102,7 +102,7 @@ defmodule GroupherServer.Test.Query.AbuseReports.JobReport do report_case = get_in(report, ["reportCases"]) assert is_list(report_case) - assert get_in(report, ["articleComment", "bodyHtml"]) == "comment" + assert get_in(report, ["articleComment", "bodyHtml"]) |> String.contains?(~s(comment)) assert get_in(report, ["articleComment", "id"]) == to_string(comment.id) assert not is_nil(get_in(report, ["articleComment", "author", "login"])) end diff --git a/test/groupher_server_web/query/cms/abuse_reports/post_report_test.exs b/test/groupher_server_web/query/cms/abuse_reports/post_report_test.exs index 49cd0a058..0edd5de10 100644 --- a/test/groupher_server_web/query/cms/abuse_reports/post_report_test.exs +++ b/test/groupher_server_web/query/cms/abuse_reports/post_report_test.exs @@ -92,8 +92,8 @@ defmodule GroupherServer.Test.Query.AbuseReports.PostReport do end test "support article_comment", ~m(guest_conn post user)a do - {:ok, comment} = CMS.create_article_comment(:post, post.id, "comment", user) - {:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) variables = %{filter: %{content_type: "ARTICLE_COMMENT", page: 1, size: 10}} results = guest_conn |> query_result(@query, variables, "pagedAbuseReports") @@ -102,7 +102,7 @@ defmodule GroupherServer.Test.Query.AbuseReports.PostReport do report_case = get_in(report, ["reportCases"]) assert is_list(report_case) - assert get_in(report, ["articleComment", "bodyHtml"]) == "comment" + assert get_in(report, ["articleComment", "bodyHtml"]) |> String.contains?(~s(comment)) assert get_in(report, ["articleComment", "id"]) == to_string(comment.id) assert not is_nil(get_in(report, ["articleComment", "author", "login"])) end diff --git a/test/groupher_server_web/query/cms/abuse_reports/repo_report_test.exs b/test/groupher_server_web/query/cms/abuse_reports/repo_report_test.exs index 5788c1c17..975e1f5fc 100644 --- a/test/groupher_server_web/query/cms/abuse_reports/repo_report_test.exs +++ b/test/groupher_server_web/query/cms/abuse_reports/repo_report_test.exs @@ -92,8 +92,8 @@ defmodule GroupherServer.Test.Query.AbuseReports.RepoReport do end test "support article_comment", ~m(guest_conn repo user)a do - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "comment", user) - {:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _} = CMS.report_article_comment(comment.id, mock_comment(), "attr", user) variables = %{filter: %{content_type: "ARTICLE_COMMENT", page: 1, size: 10}} results = guest_conn |> query_result(@query, variables, "pagedAbuseReports") @@ -102,7 +102,7 @@ defmodule GroupherServer.Test.Query.AbuseReports.RepoReport do report_case = get_in(report, ["reportCases"]) assert is_list(report_case) - assert get_in(report, ["articleComment", "bodyHtml"]) == "comment" + assert get_in(report, ["articleComment", "bodyHtml"]) |> String.contains?(~s(comment)) assert get_in(report, ["articleComment", "id"]) == to_string(comment.id) assert not is_nil(get_in(report, ["articleComment", "author", "login"])) end diff --git a/test/groupher_server_web/query/cms/comments/job_comment_test.exs b/test/groupher_server_web/query/cms/comments/job_comment_test.exs index b4d763d86..3a1fbe550 100644 --- a/test/groupher_server_web/query/cms/comments/job_comment_test.exs +++ b/test/groupher_server_web/query/cms/comments/job_comment_test.exs @@ -34,17 +34,16 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do test "guest user can get comment participators after comment created", ~m(guest_conn job user user2)a do - comment = "test comment" total_count = 5 thread = :job Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, comment, user) + {:ok, comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) acc ++ [comment] end) - {:ok, _} = CMS.create_article_comment(thread, job.id, comment, user2) + {:ok, _} = CMS.create_article_comment(thread, job.id, mock_comment(), user2) variables = %{id: job.id} results = guest_conn |> query_result(@query, variables, "job") @@ -128,13 +127,19 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do all_comments = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, job.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) random_comment = all_comments |> Enum.at(Enum.random(0..(total_count - 1))) - {:ok, replyed_comment_1} = CMS.reply_article_comment(random_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(random_comment.id, "reply 2", user2) + + {:ok, replyed_comment_1} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) variables = %{id: job.id, thread: "JOB", filter: %{page: 1, size: page_size}} results = guest_conn |> query_result(@query, variables, "pagedArticleComments") @@ -161,14 +166,19 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do all_comments = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, job.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) random_comment = all_comments |> Enum.at(Enum.random(0..(total_count - 1))) - {:ok, replyed_comment_1} = CMS.reply_article_comment(random_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(random_comment.id, "reply 2", user2) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) variables = %{ id: job.id, @@ -193,14 +203,20 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do thread = :job Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, job.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) - {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent_content", user) + {:ok, parent_comment} = + CMS.create_article_comment(:job, job.id, mock_comment("parent_comment"), user) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(parent_comment.id, "reply 2", user2) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) variables = %{id: job.id, thread: "JOB", filter: %{page: 1, size: 10}, mode: "TIMELINE"} results = guest_conn |> query_result(@query, variables, "pagedArticleComments") @@ -223,12 +239,11 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do end test "guest user can get paged comment for job", ~m(guest_conn job user)a do - comment = "test comment" total_count = 30 thread = :job Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, value} = CMS.create_article_comment(thread, job.id, comment, user) + {:ok, value} = CMS.create_article_comment(thread, job.id, mock_comment(), user) acc ++ [value] end) @@ -246,17 +261,17 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do thread = :job Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "test comment", user) + {:ok, comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) acc ++ [comment] end) - {:ok, comment} = CMS.create_article_comment(thread, job.id, "pinned comment", user) + {:ok, comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) {:ok, pinned_comment} = CMS.pin_article_comment(comment.id) Process.sleep(1000) - {:ok, comment} = CMS.create_article_comment(thread, job.id, "pinned comment 2", user) + {:ok, comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) {:ok, pinned_comment2} = CMS.pin_article_comment(comment.id) variables = %{id: job.id, thread: "JOB", filter: %{page: 1, size: 10}} @@ -274,7 +289,7 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do page_size = 10 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "test comment", user) + {:ok, comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) Process.sleep(1000) acc ++ [comment] end) @@ -290,11 +305,11 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do page_size = 10 thread = :job - {:ok, comment} = CMS.create_article_comment(thread, job.id, "test comment 1", user) + {:ok, comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) Process.sleep(1000) - {:ok, _comment2} = CMS.create_article_comment(thread, job.id, "test comment 2", user) + {:ok, _comment2} = CMS.create_article_comment(thread, job.id, mock_comment(), user) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, job.id, "test comment 3", user) + {:ok, comment3} = CMS.create_article_comment(thread, job.id, mock_comment(), user) variables = %{ id: job.id, @@ -313,11 +328,11 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do page_size = 10 thread = :job - {:ok, comment} = CMS.create_article_comment(thread, job.id, "test comment 1", user) + {:ok, comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) Process.sleep(1000) - {:ok, _comment2} = CMS.create_article_comment(thread, job.id, "test comment 2", user) + {:ok, _comment2} = CMS.create_article_comment(thread, job.id, mock_comment(), user) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, job.id, "test comment 3", user) + {:ok, comment3} = CMS.create_article_comment(thread, job.id, mock_comment(), user) variables = %{ id: job.id, @@ -337,17 +352,17 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do page_size = 10 thread = :job - {:ok, comment} = CMS.create_article_comment(thread, job.id, "parent comment 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, "reply 2", user2) + {:ok, comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, mock_comment(), user2) Process.sleep(1000) - {:ok, comment2} = CMS.create_article_comment(thread, job.id, "test comment 2", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, "reply 2", user2) + {:ok, comment2} = CMS.create_article_comment(thread, job.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, mock_comment(), user2) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, job.id, "test comment 3", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, "reply 2", user2) + {:ok, comment3} = CMS.create_article_comment(thread, job.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, mock_comment(), user2) variables = %{ id: job.id, @@ -368,7 +383,9 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, job.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -398,7 +415,9 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do all_comments = Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, job.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) @@ -406,7 +425,7 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do {:ok, _} = CMS.upvote_article_comment(random_comment.id, author_user) {:ok, author_comment} = - CMS.create_article_comment(thread, job.id, "test comment", author_user) + CMS.create_article_comment(thread, job.id, mock_comment(), author_user) {:ok, _} = CMS.upvote_article_comment(author_comment.id, author_user) @@ -434,7 +453,9 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, job.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -484,7 +505,9 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, job.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -509,7 +532,9 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do all_comments = Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, job.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, job.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) @@ -549,13 +574,13 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do Enum.reduce(1..total_count, [], fn _, acc -> {:ok, new_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", new_user) + {:ok, comment} = CMS.create_article_comment(:job, job.id, mock_comment(), new_user) acc ++ [comment] end) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:job, job.id, mock_comment(), user) variables = %{id: job.id, thread: thread, filter: %{page: 1, size: page_size}} @@ -618,22 +643,22 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do """ test "guest user can get paged replies", ~m(guest_conn job user user2)a do - comment = "test comment" total_count = 2 page_size = 10 thread = :job author_user = job.author.user - {:ok, parent_comment} = CMS.create_article_comment(thread, job.id, comment, user) + {:ok, parent_comment} = CMS.create_article_comment(thread, job.id, mock_comment(), user) Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, "reply #{i}", user2) + {:ok, reply_comment} = + CMS.reply_article_comment(parent_comment.id, mock_comment("reply #{i}"), user2) acc ++ [reply_comment] end) {:ok, author_reply_comment} = - CMS.reply_article_comment(parent_comment.id, "author reply", author_user) + CMS.reply_article_comment(parent_comment.id, mock_comment("author reply"), author_user) variables = %{id: parent_comment.id, filter: %{page: 1, size: page_size}} results = guest_conn |> query_result(@query, variables, "pagedCommentReplies") diff --git a/test/groupher_server_web/query/cms/comments/post_comment_test.exs b/test/groupher_server_web/query/cms/comments/post_comment_test.exs index 941b7bd7c..c3a4e540c 100644 --- a/test/groupher_server_web/query/cms/comments/post_comment_test.exs +++ b/test/groupher_server_web/query/cms/comments/post_comment_test.exs @@ -34,20 +34,18 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do } } """ - test "guest user can get comment participators after comment created", ~m(guest_conn post user user2)a do - comment = "test comment" total_count = 5 thread = :post Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, comment, user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) acc ++ [comment] end) - {:ok, _} = CMS.create_article_comment(thread, post.id, comment, user2) + {:ok, _} = CMS.create_article_comment(thread, post.id, mock_comment(), user2) variables = %{id: post.id} results = guest_conn |> query_result(@query, variables, "post") @@ -131,13 +129,19 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do all_comments = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) random_comment = all_comments |> Enum.at(Enum.random(0..(total_count - 1))) - {:ok, replyed_comment_1} = CMS.reply_article_comment(random_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(random_comment.id, "reply 2", user2) + + {:ok, replyed_comment_1} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) variables = %{id: post.id, thread: "POST", filter: %{page: 1, size: page_size}} results = guest_conn |> query_result(@query, variables, "pagedArticleComments") @@ -164,14 +168,19 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do all_comments = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) random_comment = all_comments |> Enum.at(Enum.random(0..(total_count - 1))) - {:ok, replyed_comment_1} = CMS.reply_article_comment(random_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(random_comment.id, "reply 2", user2) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) variables = %{ id: post.id, @@ -196,14 +205,20 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do thread = :post Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) - {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent_content", user) + {:ok, parent_comment} = + CMS.create_article_comment(:post, post.id, mock_comment("parent_comment"), user) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(parent_comment.id, "reply 2", user2) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) variables = %{id: post.id, thread: "POST", filter: %{page: 1, size: 10}, mode: "TIMELINE"} results = guest_conn |> query_result(@query, variables, "pagedArticleComments") @@ -226,12 +241,11 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do end test "guest user can get paged comment for post", ~m(guest_conn post user)a do - comment = "test comment" total_count = 30 thread = :post Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, value} = CMS.create_article_comment(thread, post.id, comment, user) + {:ok, value} = CMS.create_article_comment(thread, post.id, mock_comment(), user) acc ++ [value] end) @@ -249,17 +263,17 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do thread = :post Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) acc ++ [comment] end) - {:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) {:ok, pinned_comment} = CMS.pin_article_comment(comment.id) Process.sleep(1000) - {:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment 2", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) {:ok, pinned_comment2} = CMS.pin_article_comment(comment.id) variables = %{id: post.id, thread: "POST", filter: %{page: 1, size: 10}} @@ -282,7 +296,7 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do thread = :post Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) acc ++ [comment] end) @@ -290,16 +304,18 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do {:ok, post} = ORM.find(Post, post.id, preload: [author: :user]) post_author = post.author.user - {:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) {:ok, _pinned_comment} = CMS.pin_article_comment(comment.id) Process.sleep(1000) - {:ok, comment} = CMS.create_article_comment(thread, post.id, "solution", post_author) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("solution"), post_author) + {:ok, solution_comment} = CMS.mark_comment_solution(comment.id, post_author) Process.sleep(1000) - {:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment 2", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) {:ok, _pinned_comment2} = CMS.pin_article_comment(comment.id) variables = %{id: post.id, thread: "POST", filter: %{page: 1, size: 10}} @@ -315,7 +331,7 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do page_size = 10 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) Process.sleep(1000) acc ++ [comment] end) @@ -331,11 +347,11 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do page_size = 10 thread = :post - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment 1", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) Process.sleep(1000) - {:ok, _comment2} = CMS.create_article_comment(thread, post.id, "test comment 2", user) + {:ok, _comment2} = CMS.create_article_comment(thread, post.id, mock_comment(), user) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, post.id, "test comment 3", user) + {:ok, comment3} = CMS.create_article_comment(thread, post.id, mock_comment(), user) variables = %{ id: post.id, @@ -354,11 +370,11 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do page_size = 10 thread = :post - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment 1", user) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) Process.sleep(1000) - {:ok, _comment2} = CMS.create_article_comment(thread, post.id, "test comment 2", user) + {:ok, _comment2} = CMS.create_article_comment(thread, post.id, mock_comment(), user) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, post.id, "test comment 3", user) + {:ok, comment3} = CMS.create_article_comment(thread, post.id, mock_comment(), user) variables = %{ id: post.id, @@ -378,17 +394,17 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do page_size = 10 thread = :post - {:ok, comment} = CMS.create_article_comment(thread, post.id, "parent comment 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, "reply 2", user2) + {:ok, comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, mock_comment(), user2) Process.sleep(1000) - {:ok, comment2} = CMS.create_article_comment(thread, post.id, "test comment 2", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, "reply 2", user2) + {:ok, comment2} = CMS.create_article_comment(thread, post.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, mock_comment(), user2) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, post.id, "test comment 3", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, "reply 2", user2) + {:ok, comment3} = CMS.create_article_comment(thread, post.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, mock_comment(), user2) variables = %{ id: post.id, @@ -409,7 +425,9 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -439,7 +457,9 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do all_comments = Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) @@ -447,7 +467,7 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do {:ok, _} = CMS.upvote_article_comment(random_comment.id, author_user) {:ok, author_comment} = - CMS.create_article_comment(thread, post.id, "test comment", author_user) + CMS.create_article_comment(thread, post.id, mock_comment(), author_user) {:ok, _} = CMS.upvote_article_comment(author_comment.id, author_user) @@ -475,7 +495,9 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -525,7 +547,9 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -550,7 +574,9 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do all_comments = Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, post.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, post.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) @@ -590,13 +616,13 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do Enum.reduce(1..total_count, [], fn _, acc -> {:ok, new_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", new_user) + {:ok, comment} = CMS.create_article_comment(:post, post.id, mock_comment(), new_user) acc ++ [comment] end) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:post, post.id, mock_comment(), user) variables = %{id: post.id, thread: thread, filter: %{page: 1, size: page_size}} @@ -659,22 +685,22 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do """ test "guest user can get paged replies", ~m(guest_conn post user user2)a do - comment = "test comment" total_count = 2 page_size = 10 thread = :post author_user = post.author.user - {:ok, parent_comment} = CMS.create_article_comment(thread, post.id, comment, user) + {:ok, parent_comment} = CMS.create_article_comment(thread, post.id, mock_comment(), user) Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, "reply #{i}", user2) + {:ok, reply_comment} = + CMS.reply_article_comment(parent_comment.id, mock_comment("reply #{i}"), user2) acc ++ [reply_comment] end) {:ok, author_reply_comment} = - CMS.reply_article_comment(parent_comment.id, "author reply", author_user) + CMS.reply_article_comment(parent_comment.id, mock_comment("author reply"), author_user) variables = %{id: parent_comment.id, filter: %{page: 1, size: page_size}} results = guest_conn |> query_result(@query, variables, "pagedCommentReplies") diff --git a/test/groupher_server_web/query/cms/comments/repo_comment_test.exs b/test/groupher_server_web/query/cms/comments/repo_comment_test.exs index 83e6abf4d..f8fec8db9 100644 --- a/test/groupher_server_web/query/cms/comments/repo_comment_test.exs +++ b/test/groupher_server_web/query/cms/comments/repo_comment_test.exs @@ -32,17 +32,16 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do """ test "guest user can get comment participators after comment created", ~m(guest_conn repo user user2)a do - comment = "test comment" total_count = 5 thread = :repo Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, comment, user) + {:ok, comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) acc ++ [comment] end) - {:ok, _} = CMS.create_article_comment(thread, repo.id, comment, user2) + {:ok, _} = CMS.create_article_comment(thread, repo.id, mock_comment(), user2) variables = %{id: repo.id} results = guest_conn |> query_result(@query, variables, "repo") @@ -126,13 +125,19 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do all_comments = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, repo.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) random_comment = all_comments |> Enum.at(Enum.random(0..(total_count - 1))) - {:ok, replyed_comment_1} = CMS.reply_article_comment(random_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(random_comment.id, "reply 2", user2) + + {:ok, replyed_comment_1} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) variables = %{id: repo.id, thread: "REPO", filter: %{page: 1, size: page_size}} results = guest_conn |> query_result(@query, variables, "pagedArticleComments") @@ -159,14 +164,19 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do all_comments = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, repo.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) random_comment = all_comments |> Enum.at(Enum.random(0..(total_count - 1))) - {:ok, replyed_comment_1} = CMS.reply_article_comment(random_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(random_comment.id, "reply 2", user2) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(random_comment.id, mock_comment(), user2) variables = %{ id: repo.id, @@ -191,14 +201,20 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do thread = :repo Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, repo.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) - {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent_content", user) + {:ok, parent_comment} = + CMS.create_article_comment(:repo, repo.id, mock_comment("parent_comment"), user) - {:ok, replyed_comment_1} = CMS.reply_article_comment(parent_comment.id, "reply 1", user2) - {:ok, replyed_comment_2} = CMS.reply_article_comment(parent_comment.id, "reply 2", user2) + {:ok, replyed_comment_1} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) + + {:ok, replyed_comment_2} = + CMS.reply_article_comment(parent_comment.id, mock_comment(), user2) variables = %{id: repo.id, thread: "REPO", filter: %{page: 1, size: 10}, mode: "TIMELINE"} results = guest_conn |> query_result(@query, variables, "pagedArticleComments") @@ -221,12 +237,11 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do end test "guest user can get paged comment for repo", ~m(guest_conn repo user)a do - comment = "test comment" total_count = 30 thread = :repo Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, value} = CMS.create_article_comment(thread, repo.id, comment, user) + {:ok, value} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) acc ++ [value] end) @@ -244,17 +259,17 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do thread = :repo Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "test comment", user) + {:ok, comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) acc ++ [comment] end) - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "pinned comment", user) + {:ok, comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) {:ok, pinned_comment} = CMS.pin_article_comment(comment.id) Process.sleep(1000) - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "pinned comment 2", user) + {:ok, comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) {:ok, pinned_comment2} = CMS.pin_article_comment(comment.id) variables = %{id: repo.id, thread: "REPO", filter: %{page: 1, size: 10}} @@ -272,7 +287,7 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do page_size = 10 Enum.reduce(1..total_count, [], fn _, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "test comment", user) + {:ok, comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) Process.sleep(1000) acc ++ [comment] end) @@ -288,11 +303,11 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do page_size = 10 thread = :repo - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "test comment 1", user) + {:ok, comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) Process.sleep(1000) - {:ok, _comment2} = CMS.create_article_comment(thread, repo.id, "test comment 2", user) + {:ok, _comment2} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, repo.id, "test comment 3", user) + {:ok, comment3} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) variables = %{ id: repo.id, @@ -311,11 +326,11 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do page_size = 10 thread = :repo - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "test comment 1", user) + {:ok, comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) Process.sleep(1000) - {:ok, _comment2} = CMS.create_article_comment(thread, repo.id, "test comment 2", user) + {:ok, _comment2} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, repo.id, "test comment 3", user) + {:ok, comment3} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) variables = %{ id: repo.id, @@ -335,17 +350,17 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do page_size = 10 thread = :repo - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "parent comment 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, "reply 2", user2) + {:ok, comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment.id, mock_comment(), user2) Process.sleep(1000) - {:ok, comment2} = CMS.create_article_comment(thread, repo.id, "test comment 2", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, "reply 2", user2) + {:ok, comment2} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment2.id, mock_comment(), user2) Process.sleep(1000) - {:ok, comment3} = CMS.create_article_comment(thread, repo.id, "test comment 3", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, "reply 1", user) - {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, "reply 2", user2) + {:ok, comment3} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, mock_comment(), user) + {:ok, _reply_comment} = CMS.reply_article_comment(comment3.id, mock_comment(), user2) variables = %{ id: repo.id, @@ -366,7 +381,9 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, repo.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -396,7 +413,9 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do all_comments = Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, repo.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) @@ -404,7 +423,7 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do {:ok, _} = CMS.upvote_article_comment(random_comment.id, author_user) {:ok, author_comment} = - CMS.create_article_comment(thread, repo.id, "test comment", author_user) + CMS.create_article_comment(thread, repo.id, mock_comment(), author_user) {:ok, _} = CMS.upvote_article_comment(author_comment.id, author_user) @@ -432,7 +451,9 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, repo.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -482,7 +503,9 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do all_comment = Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "test comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, repo.id, mock_comment("comment #{i}"), user) + Process.sleep(1000) acc ++ [comment] end) @@ -507,7 +530,9 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do all_comments = Enum.reduce(0..total_count, [], fn i, acc -> - {:ok, comment} = CMS.create_article_comment(thread, repo.id, "comment #{i}", user) + {:ok, comment} = + CMS.create_article_comment(thread, repo.id, mock_comment("comment #{i}"), user) + acc ++ [comment] end) @@ -547,13 +572,13 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do Enum.reduce(1..total_count, [], fn _, acc -> {:ok, new_user} = db_insert(:user) - {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", new_user) + {:ok, comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), new_user) acc ++ [comment] end) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) - {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, mock_comment(), user) variables = %{id: repo.id, thread: thread, filter: %{page: 1, size: page_size}} @@ -616,22 +641,22 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do """ test "guest user can get paged replies", ~m(guest_conn repo user user2)a do - comment = "test comment" total_count = 2 page_size = 10 thread = :repo author_user = repo.author.user - {:ok, parent_comment} = CMS.create_article_comment(thread, repo.id, comment, user) + {:ok, parent_comment} = CMS.create_article_comment(thread, repo.id, mock_comment(), user) Enum.reduce(1..total_count, [], fn i, acc -> - {:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, "reply #{i}", user2) + {:ok, reply_comment} = + CMS.reply_article_comment(parent_comment.id, mock_comment("reply #{i}"), user2) acc ++ [reply_comment] end) {:ok, author_reply_comment} = - CMS.reply_article_comment(parent_comment.id, "author reply", author_user) + CMS.reply_article_comment(parent_comment.id, mock_comment("author reply"), author_user) variables = %{id: parent_comment.id, filter: %{page: 1, size: page_size}} results = guest_conn |> query_result(@query, variables, "pagedCommentReplies") diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_jobs_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_jobs_test.exs index e01927370..c2dd9f0f9 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_jobs_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_jobs_test.exs @@ -416,7 +416,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedJobs do assert first_job["id"] !== to_string(job_last_week.id) Process.sleep(1500) - {:ok, _comment} = CMS.create_article_comment(:job, job_last_week.id, "comment", user) + {:ok, _comment} = CMS.create_article_comment(:job, job_last_week.id, mock_comment(), user) results = guest_conn |> query_result(@query, variables, "pagedJobs") entries = results["entries"] @@ -428,7 +428,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedJobs do test "comment on very old job have no effect", ~m(guest_conn job_last_year user)a do variables = %{filter: %{page: 1, size: 20}} - {:ok, _comment} = CMS.create_article_comment(:job, job_last_year.id, "comment", user) + {:ok, _comment} = CMS.create_article_comment(:job, job_last_year.id, mock_comment(), user) results = guest_conn |> query_result(@query, variables, "pagedJobs") entries = results["entries"] @@ -444,7 +444,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedJobs do CMS.create_article_comment( :job, job_last_week.id, - "comment", + mock_comment(), job_last_week.author.user ) diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs index 1369c7095..c9e9c1e0e 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs @@ -315,7 +315,6 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do } } """ - test "latest commented post should appear on top", ~m(guest_conn post_last_week user)a do variables = %{filter: %{page: 1, size: 20}} results = guest_conn |> query_result(@query, variables, "pagedPosts") @@ -324,7 +323,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do assert first_post["id"] !== to_string(post_last_week.id) Process.sleep(1500) - {:ok, _comment} = CMS.create_article_comment(:post, post_last_week.id, "comment", user) + {:ok, _comment} = CMS.create_article_comment(:post, post_last_week.id, mock_comment(), user) results = guest_conn |> query_result(@query, variables, "pagedPosts") entries = results["entries"] @@ -336,7 +335,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do test "comment on very old post have no effect", ~m(guest_conn post_last_year user)a do variables = %{filter: %{page: 1, size: 20}} - {:ok, _comment} = CMS.create_article_comment(:post, post_last_year.id, "comment", user) + {:ok, _comment} = CMS.create_article_comment(:post, post_last_year.id, mock_comment(), user) results = guest_conn |> query_result(@query, variables, "pagedPosts") entries = results["entries"] @@ -352,7 +351,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do CMS.create_article_comment( :post, post_last_week.id, - "comment", + mock_comment(), post_last_week.author.user ) diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_repos_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_repos_test.exs index 798c50a50..50c1258d8 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_repos_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_repos_test.exs @@ -361,7 +361,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedRepos do assert first_repo["id"] !== to_string(repo_last_week.id) Process.sleep(1500) - {:ok, _comment} = CMS.create_article_comment(:repo, repo_last_week.id, "comment", user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo_last_week.id, mock_comment(), user) results = guest_conn |> query_result(@query, variables, "pagedRepos") entries = results["entries"] @@ -373,7 +373,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedRepos do test "comment on very old repo have no effect", ~m(guest_conn repo_last_year user)a do variables = %{filter: %{page: 1, size: 20}} - {:ok, _comment} = CMS.create_article_comment(:repo, repo_last_year.id, "comment", user) + {:ok, _comment} = CMS.create_article_comment(:repo, repo_last_year.id, mock_comment(), user) results = guest_conn |> query_result(@query, variables, "pagedRepos") entries = results["entries"] @@ -389,7 +389,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedRepos do CMS.create_article_comment( :repo, repo_last_week.id, - "comment", + mock_comment(), repo_last_week.author.user ) diff --git a/test/helper/converter/article_test.exs b/test/helper/converter/article_test.exs new file mode 100644 index 000000000..9842090c2 --- /dev/null +++ b/test/helper/converter/article_test.exs @@ -0,0 +1,37 @@ +defmodule GroupherServer.Test.Helper.Converter.Article do + @moduledoc false + + use GroupherServerWeb.ConnCase, async: true + + alias Helper.Converter.{Article, EditorToHTML} + + describe "[snaitizer test]" do + test "parse_body should return valid format" do + body = """ + { + "time": 11, + "blocks": [ + { + "id" : "FLHF-eF_x4", + "type" : "paragraph", + "data" : { + "text" : "this is a paragraph." + } + } + ], + "version": "2.20" + } + """ + + {:ok, %{body: body, body_html: body_html}} = Article.parse_body(body) + {:ok, body_map} = Jason.decode(body) + + assert body_html |> String.contains?("List.first() + + assert EditorToHTML.Validator.is_valid(body_map) + assert p_block["id"] |> String.starts_with?("block-") + end + end +end diff --git a/test/helper/converter/editor_to_html_test/header_test.exs b/test/helper/converter/editor_to_html_test/header_test.exs index 7c8602f22..9454fe66d 100644 --- a/test/helper/converter/editor_to_html_test/header_test.exs +++ b/test/helper/converter/editor_to_html_test/header_test.exs @@ -11,11 +11,12 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Header do @class get_in(@root_class, ["header"]) describe "[header block unit]" do - defp set_data(data) do + defp set_data(data, id \\ "") do %{ "time" => 1_567_250_876_713, "blocks" => [ %{ + "id" => id, "type" => "header", "data" => data } @@ -82,18 +83,20 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Header do test "edit exsit block will not change id value" do editor_json = - set_data(%{ - "id" => "exist", - "text" => "header content", - "level" => 1, - "eyebrowTitle" => "eyebrow title content", - "footerTitle" => "footer title content" - }) + set_data( + %{ + "text" => "header content", + "level" => 1, + "eyebrowTitle" => "eyebrow title content", + "footerTitle" => "footer title content" + }, + "block-id-exist" + ) {:ok, editor_string} = Jason.encode(editor_json) {:ok, converted} = Parser.to_html(editor_string) - assert Utils.str_occurence(converted, ~s(id="exist")) == 1 + assert converted |> String.contains?(~s(
1_567_250_876_713, "blocks" => [ %{ + "id" => id, "type" => "image", "data" => %{ - "id" => id, "mode" => mode, "items" => items } @@ -166,13 +166,13 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do "caption" => "this is a caption 1" } end), - "exsit" + "block-id-exsit" ) {:ok, editor_string} = Jason.encode(editor_json) {:ok, converted} = Parser.to_html(editor_string) - assert Utils.str_occurence(converted, "id=\"exsit\"") == 1 + assert Utils.str_occurence(converted, ~s(id="block-id-exsit")) == 1 end test "invalid mode parse should raise error message" do diff --git a/test/helper/converter/editor_to_html_test/index_test.exs b/test/helper/converter/editor_to_html_test/index_test.exs index 240311aab..bfa214eaf 100644 --- a/test/helper/converter/editor_to_html_test/index_test.exs +++ b/test/helper/converter/editor_to_html_test/index_test.exs @@ -25,13 +25,6 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do }) describe "[basic convert]" do - test "basic string_json parse should work" do - string = ~S({"time":1566184478687,"blocks":[{}],"version":"2.15.0"}) - {:ok, converted} = Parser.string_to_json(string) - - assert converted["version"] == "2.15.0" - end - @editor_json %{ "time" => 1_567_250_876_713, "blocks" => [], @@ -52,6 +45,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do } {:ok, editor_string} = Jason.encode(editor_json) + {:error, error} = Parser.to_html(editor_string) assert error == [ @@ -101,15 +95,6 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do assert String.contains?(error, "undown block: 1") end - - test "real-world editor.js data should work" do - {:ok, converted} = Parser.string_to_json(@real_editor_data) - - assert not Enum.empty?(converted["blocks"]) - assert converted["blocks"] |> is_list - assert converted["version"] |> is_binary - assert converted["time"] |> is_integer - end end describe "[secure issues]" do @@ -130,8 +115,8 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do {:ok, editor_string} = Jason.encode(editor_json) {:ok, converted} = Parser.to_html(editor_string) - assert converted == - ~s() + assert converted |> String.contains?(~s(evel script
)) + assert converted |> String.contains?(~s(1_567_250_876_713, @@ -149,8 +134,12 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do {:ok, editor_string} = Jason.encode(editor_json) {:ok, converted} = Parser.to_html(editor_string) - assert converted == - ~s(
) + assert converted |> String.contains?(~s(Editor.js is an element <script>evel script</script>
)) + + assert converted + |> String.contains?( + ~s(Editor.js is an element <script>evel script</script>) + ) end end end diff --git a/test/helper/converter/editor_to_html_test/list_test.exs b/test/helper/converter/editor_to_html_test/list_test.exs index 07e1bae07..182d163de 100644 --- a/test/helper/converter/editor_to_html_test/list_test.exs +++ b/test/helper/converter/editor_to_html_test/list_test.exs @@ -16,9 +16,9 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do "time" => 1_567_250_876_713, "blocks" => [ %{ + "id" => id, "type" => "list", "data" => %{ - "id" => id, "mode" => mode, "items" => items } @@ -121,13 +121,13 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do "一个带着中文的很长的句子。一个带着中文的很长的句子。一个带着中文的很长的句子。一个带着中文的很长的句子。一个带着中文的很长的句子。一个带着中文的很长的句子。一个带着中文的很长的句子。一个带着中文的很长的句子。一个带着中文的很长的句子。" } ], - "exsit" + "block-id-exist" ) {:ok, editor_string} = Jason.encode(editor_json) {:ok, converted} = Parser.to_html(editor_string) - assert Utils.str_occurence(converted, "id=\"exsit\"") == 1 + assert Utils.str_occurence(converted, ~s(id="block-id-exist")) == 1 end test "basic checklist parse should work" do diff --git a/test/helper/converter/editor_to_html_test/paragraph_test.exs b/test/helper/converter/editor_to_html_test/paragraph_test.exs index a5f2ce506..f69eb121f 100644 --- a/test/helper/converter/editor_to_html_test/paragraph_test.exs +++ b/test/helper/converter/editor_to_html_test/paragraph_test.exs @@ -26,7 +26,9 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Paragraph do {:ok, editor_string} = Jason.encode(@editor_json) {:ok, converted} = Parser.to_html(editor_string) - assert converted == ~s() + assert converted |> String.contains?(~s(paragraph content
)) + assert converted |> String.contains?(~s(1_567_250_876_713, "blocks" => [ %{ + "id" => id, "type" => "table", "data" => %{ - "id" => id, "columnCount" => column_count, "items" => items } @@ -122,13 +122,13 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Table do "width" => "180px" } ], - "exist" + "block-id-exist" ) {:ok, editor_string} = Jason.encode(editor_json) {:ok, converted} = Parser.to_html(editor_string) - assert Utils.str_occurence(converted, ~s(id="exist")) == 1 + assert Utils.str_occurence(converted, ~s(id="block-id-exist")) == 1 end test "invalid table field parse should raise error message" do diff --git a/test/helper/converter/md_to_editor_test.exs b/test/helper/converter/md_to_editor_test.exs index ecc23c98d..0d0b6a20a 100644 --- a/test/helper/converter/md_to_editor_test.exs +++ b/test/helper/converter/md_to_editor_test.exs @@ -231,6 +231,7 @@ defmodule GroupherServer.Test.Helper.Converter.MdToEditor do ] end + # TODO: inline code parse test "complex ast parser should work" do markdown = """ @@ -249,10 +250,12 @@ defmodule GroupherServer.Test.Helper.Converter.MdToEditor do {:ok, html} = EditorToHTML.to_html(editor_blocks) - viewer_class = @root_class["viewer"] - - assert html == - ~s(
) + assert html |> String.contains?(~s(hello
this is a basic markdown text
delete me
italic me
My
in-line-code-contentis best)) + assert html |> String.contains?(~s(String.contains?(~s(italic me)) + assert html |> String.contains?(~s(
alert(\"hello,world\")" - - def assert_v(:xss_safe_string), - # "<script>alert("hello,world")</script>" - do: "" def is_valid_kv?(obj, key, :list) when is_map(obj) do obj = map_key_stringify(obj) diff --git a/test/support/factory.ex b/test/support/factory.ex index b2021d94f..e6086ea3e 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -28,16 +28,47 @@ defmodule GroupherServer.Support.Factory do @default_article_meta CMS.Model.Embeds.ArticleMeta.default_meta() @default_emotions CMS.Model.Embeds.ArticleCommentEmotion.default_emotions() + # simulate editor.js fmt rich text + def mock_rich_text(text \\ "text") do + """ + { + "time": 111, + "blocks": [ + { + "id": "lldjfiek", + "type": "paragraph", + "data": { + "text": "#{text}" + } + } + ], + "version": "2.22.0" + } + """ + end + + def mock_xss_string(:safe) do + mock_rich_text("<script>blackmail</script>") + end + + def mock_xss_string(text \\ "blackmail") do + mock_rich_text("") + end + + def mock_comment(text \\ "comment") do + mock_rich_text(text) + end + defp mock_meta(:post) do - body = Faker.Lorem.sentence(%Range{first: 80, last: 120}) + text = Faker.Lorem.sentence(%Range{first: 80, last: 120}) %{ meta: @default_article_meta, - title: String.slice(body, 1, 49), - body: body, - digest: String.slice(body, 1, 150), - solution_digest: String.slice(body, 1, 150), - length: String.length(body), + title: String.slice(text, 1, 49), + body: mock_rich_text(), + digest: String.slice(text, 1, 150), + solution_digest: String.slice(text, 1, 150), + length: String.length(text), author: mock(:author), views: Enum.random(0..2000), original_community: mock(:community), @@ -123,16 +154,16 @@ defmodule GroupherServer.Support.Factory do end defp mock_meta(:job) do - body = Faker.Lorem.sentence(%Range{first: 80, last: 120}) + text = Faker.Lorem.sentence(%Range{first: 80, last: 120}) %{ meta: @default_article_meta, - title: String.slice(body, 1, 49), + title: String.slice(text, 1, 49), company: Faker.Company.name(), - body: body, + body: mock_rich_text(), desc: "活少, 美女多", - digest: String.slice(body, 1, 150), - length: String.length(body), + digest: String.slice(text, 1, 150), + length: String.length(text), author: mock(:author), views: Enum.random(0..2000), original_community: mock(:community), @@ -145,14 +176,14 @@ defmodule GroupherServer.Support.Factory do end defp mock_meta(:blog) do - body = Faker.Lorem.sentence(%Range{first: 80, last: 120}) + text = Faker.Lorem.sentence(%Range{first: 80, last: 120}) %{ meta: @default_article_meta, - title: String.slice(body, 1, 49), - body: String.slice(body, 1, 49), - digest: String.slice(body, 1, 150), - length: String.length(body), + title: String.slice(text, 1, 49), + body: mock_rich_text(), + digest: String.slice(text, 1, 150), + length: String.length(text), author: mock(:author), views: Enum.random(0..2000), original_community: mock(:community), @@ -165,9 +196,9 @@ defmodule GroupherServer.Support.Factory do end defp mock_meta(:comment) do - body = Faker.Lorem.sentence(%Range{first: 30, last: 80}) + # text = Faker.Lorem.sentence(%Range{first: 30, last: 80}) - %{body: body} + %{body: mock_rich_text()} end defp mock_meta(:mention) do