~mlb/linkhut

15f8217d60baba0067df343cecedf87ae83a2cfb — Matías Larre Borges 21 days ago f2536ee
Fix bug in rendering responses from posts API
M assets/css/app.scss => assets/css/app.scss +1 -0
@@ 270,6 270,7 @@ form:not([itemprop="signout"]) {
button, input:not([type=checkbox]) {
    -moz-appearance: none;
    -webkit-appearance: none;
    appearance: none;

    &::-moz-focus-inner {
        border: 0;

M lib/linkhut_web/templates/layout/_session.html.heex => lib/linkhut_web/templates/layout/_session.html.heex +0 -1
@@ 10,7 10,6 @@
    </li>
    <li><%= link("Settings", to: Routes.profile_path(@conn, :show)) %></li>
    <li>
      
      <%= form_for :nil, Routes.session_path(@conn, :delete), [method: :delete, itemprop: "signout"], fn _f -> %>
        <%= submit("Log out") %>
      <% end %>

M lib/linkhut_web/views/api/posts_view.ex => lib/linkhut_web/views/api/posts_view.ex +2 -1
@@ 146,7 146,7 @@ defmodule LinkhutWeb.Api.PostsView do
      description: link.title,
      extended: link.notes,
      hash: md5(link.url),
      others: link.shares,
      others: max(0, link.savers - 1),
      tag: Enum.join(link.tags, " "),
      time: DateTime.to_iso8601(link.inserted_at)
    }


@@ 167,6 167,7 @@ defmodule LinkhutWeb.Api.PostsView do
      description: link.title,
      extended: link.notes,
      hash: md5(link.url),
      others: max(0, link.savers - 1),
      tags: Enum.join(link.tags, " "),
      shared: if(link.is_private, do: "no", else: "yes"),
      time: DateTime.to_iso8601(link.inserted_at),

A test/linkhut_web/controllers/api/posts_controller_test.exs => test/linkhut_web/controllers/api/posts_controller_test.exs +66 -0
@@ 0,0 1,66 @@
defmodule LinkhutWeb.Api.PostsControllerTest do
  use LinkhutWeb.ConnCase

  test "/v1/posts/update - Unauthenticated", %{conn: conn} do
    conn = get(conn, Routes.api_posts_path(conn, :update))

    assert text_response(conn, 401) == "Unauthenticated"
  end

  test "/v1/posts/update [JSON] - empty", %{conn: conn} do
    user = insert(:user)
    token = insert(:access_token, resource_owner_id: user.id, scopes: "posts:read")

    conn =
      conn
      |> Plug.Conn.put_req_header("authorization", "Bearer #{token.token}")
      |> Plug.Conn.put_req_header("accept", "application/json")
      |> get(Routes.api_posts_path(conn, :update))

    assert json_response(conn, 200) == %{"update_time" => "1970-01-01T00:00:00Z"}
  end

  test "/v1/posts/update [JSON]", %{conn: conn} do
    user = insert(:user)
    link = insert(:link, user_id: user.id)
    token = insert(:access_token, resource_owner_id: user.id, scopes: "posts:read")

    conn =
      conn
      |> Plug.Conn.put_req_header("authorization", "Bearer #{token.token}")
      |> Plug.Conn.put_req_header("accept", "application/json")
      |> get(Routes.api_posts_path(conn, :update))

    assert json_response(conn, 200) == %{"update_time" => DateTime.to_iso8601(link.inserted_at)}
  end

  test "/v1/posts/get [JSON]", %{conn: conn} do
    user = insert(:user)
    link = insert(:link, user_id: user.id)
    md5 = :crypto.hash(:md5, link.url) |> Base.encode16(case: :lower)
    token = insert(:access_token, resource_owner_id: user.id, scopes: "posts:read")

    conn =
      conn
      |> Plug.Conn.put_req_header("authorization", "Bearer #{token.token}")
      |> Plug.Conn.put_req_header("accept", "application/json")
      |> get(Routes.api_posts_path(conn, :get))

    assert json_response(conn, 200) == %{
             "posts" => [
               %{
                 "description" => link.title,
                 "extended" => link.notes,
                 "hash" => md5,
                 "href" => link.url,
                 "meta" => nil,
                 "others" => 0,
                 "shared" => "yes",
                 "tags" => "test auto-generated",
                 "time" => DateTime.to_iso8601(link.inserted_at),
                 "toread" => "no"
               }
             ]
           }
  end
end

M test/support/factory.ex => test/support/factory.ex +8 -0
@@ 20,6 20,14 @@ defmodule Linkhut.Factory do
    }
  end

  def access_token_factory do
    %Linkhut.Oauth.AccessToken{
      token: sequence("token-"),
      scopes: &"#{&1}",
      resource_owner_id: build(:user).id
    }
  end

  def link_factory do
    %Linkhut.Links.Link{
      user_id: build(:user).id,