File size: 4,663 Bytes
6778ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
defmodule PlausibleWeb.Live.PluginsAPISettingsTest do
  use PlausibleWeb.ConnCase, async: true
  import Phoenix.LiveViewTest

  alias Plausible.Plugins.API.Tokens

  describe "GET /:domain/settings/integrations" do
    setup [:create_user, :log_in, :create_site]

    test "does not display the Plugins API section by default", %{conn: conn, site: site} do
      conn = get(conn, "/#{site.domain}/settings/integrations")
      resp = html_response(conn, 200)

      refute resp =~ "Plugin Tokens"
    end

    test "does display the Plugins API section on ?new_token=....", %{
      conn: conn,
      site: site
    } do
      conn = get(conn, "/#{site.domain}/settings/integrations?new_token=test")
      resp = html_response(conn, 200)

      assert resp =~ "Plugin tokens"
    end

    test "does display the Plugins API section when there are tokens already created", %{
      conn: conn,
      site: site
    } do
      {:ok, _, _} = Tokens.create(site, "test")
      conn = get(conn, "/#{site.domain}/settings/integrations")
      resp = html_response(conn, 200)

      assert resp =~ "Plugin tokens"
    end

    test "lists tokens with revoke actions", %{conn: conn, site: site} do
      {:ok, t1, _} = Tokens.create(site, "test-token-1")
      {:ok, t2, _} = Tokens.create(site, "test-token-2")
      {:ok, _, _} = Tokens.create(build(:site), "test-token-3")

      conn = get(conn, "/#{site.domain}/settings/integrations")
      resp = html_response(conn, 200)

      assert resp =~ "test-token-1"
      assert resp =~ "test-token-2"

      assert resp =~ "Last used"
      assert resp =~ "Not yet"

      assert resp =~ "**********" <> t1.hint
      assert resp =~ "**********" <> t2.hint
      refute resp =~ "test-token-3"

      assert element_exists?(
               resp,
               ~s/button[phx-click="revoke-token"][phx-value-token-id="#{t1.id}"]#revoke-token-#{t1.id}/
             )

      assert element_exists?(
               resp,
               ~s/button[phx-click="revoke-token"][phx-value-token-id="#{t2.id}"]#revoke-token-#{t2.id}/
             )
    end

    test "create token button is rendered", %{conn: conn, site: site} do
      conn = get(conn, "/#{site.domain}/settings/integrations?new_token=WordPress")
      resp = html_response(conn, 200)

      assert element_exists?(resp, ~s/button[phx-click="create-token"]/)
    end
  end

  describe "Plugins.API.Settings live view" do
    setup [:create_user, :log_in, :create_site]

    test "create token when invoked via URL", %{conn: conn, site: site} do
      {lv, html} =
        get_liveview(conn, site, with_html?: true, query_params: "?new_token=WordPress")

      assert element_exists?(html, "#token-form")
      assert text_of_element(html, "label[for=token_description]") == "Description"
      assert element_exists?(html, "input[value=WordPress]#token_description")

      assert element_exists?(
               html,
               ~s/div#token-form form[phx-submit="generate-token"]/
             )

      html =
        lv
        |> find_live_child("token-form")
        |> element("form")
        |> render_submit()

      assert text_of_element(html, "label[for=token-clipboard]") == "Plugin Token"
      assert element_exists?(html, "input#token-clipboard")
      assert element_exists?(html, ~s/button[phx-click="close-token-modal"]/)

      assert Tokens.any?(site)
    end

    test "adds token and shows it", %{conn: conn, site: site} do
      refute Tokens.any?(site)

      lv = get_liveview(conn, site, query_params: "?new_token=WordPress")

      lv
      |> find_live_child("token-form")
      |> element("form")
      |> render_submit()

      assert Tokens.any?(site)

      html = render(lv)
      assert text_of_element(html, "label[for=token-clipboard]") == "Plugin Token"
      assert element_exists?(html, "input#token-clipboard")
      assert text_of_element(html, "span.token-description") == "WordPress"
    end

    test "fails to add token with no description", %{conn: conn, site: site} do
      {:ok, _, _} = Tokens.create(site, "test")

      lv = get_liveview(conn, site)

      lv |> render_click("create-token")

      lv
      |> find_live_child("token-form")
      |> element("form")
      |> render_submit()

      assert [_] = Tokens.list(site)
    end
  end

  defp get_liveview(conn, site, opts \\ []) do
    query_params = Keyword.get(opts, :query_params, "")
    conn = assign(conn, :live_module, PlausibleWeb.Live.Plugins.API.Settings)
    {:ok, lv, html} = live(conn, "/#{site.domain}/settings/integrations#{query_params}")

    if Keyword.get(opts, :with_html?) do
      {lv, html}
    else
      lv
    end
  end
end