Browse Source

Update tests.

jackyalcine 6 months ago
parent
commit
a939139235
Signed by: Jacky Alciné <yo@jacky.wtf> GPG Key ID: 537A4F904B15268D

+ 3
- 0
.gitmodules View File

@@ -0,0 +1,3 @@
1
+[submodule "docs/api"]
2
+	path = docs/api
3
+	url = https://github.com/lord/slate

+ 2
- 1
.lvimrc View File

@@ -11,6 +11,7 @@ let s:command_prefix = 'docker-compose run ' .
11 11
       \ '-e CANONICAL_HOST="koype_vim_test" '.
12 12
       \ '-e CANONICAL_SCHEME="http" '.
13 13
       \ '-e MIX_ENV="test" ' .
14
+      \ '-e DOC=1 ' .
14 15
       \ '-e OBJECT_STORAGE_BUCKET="koype-test" '.
15 16
       \ '-e TEST_HOST="http://koype_vim_test" ' .
16 17
       \ '--name="koype_vim_test" '.
@@ -26,6 +27,6 @@ let g:test#custom_transformations = {'docker-compose': function('DockerComposeTr
26 27
 let g:test#transformation = 'docker-compose'
27 28
 let g:test#elixir#exunit#options = {
28 29
       \ 'file': '--seed=0 --max-cases=3 --slowest=5 --include acceptance:true',
29
-      \ 'suite': '--slowest=10 --include acceptance:true',
30
+      \ 'suite': '--include acceptance:true',
30 31
       \ 'nearest': '--include skip:true --include acceptance:true'
31 32
       \ }

+ 0
- 1
.tool-versions View File

@@ -1,4 +1,3 @@
1 1
 elixir 1.7.4
2 2
 erlang 21.2.5
3 3
 nodejs 10.14.0
4
-nodejs 10.14.0

+ 1
- 0
docs/api

@@ -0,0 +1 @@
1
+Subproject commit de496848c1e7b7116371ae25944aced1c22c47a9

+ 2
- 1
lib/http.ex View File

@@ -66,7 +66,8 @@ defmodule Koype.Http do
66 66
       [
67 67
         timeout: Keyword.get(opts, :timeout, Koype.Http.timeout()),
68 68
         headers:
69
-          Keyword.get(opts, :headers, %{})
69
+          opts
70
+          |> Keyword.get(:headers, %{})
70 71
           |> Map.put("user-agent", @fake_human_agent <> " " <> @user_agent)
71 72
           |> Map.to_list() || nil,
72 73
         body: Keyword.get(opts, :body, nil),

+ 20
- 16
lib/page/parser/endpoint.ex View File

@@ -16,7 +16,7 @@ defmodule Koype.Page.EndpointParser do
16 16
     case do_request(method, parser_url) do
17 17
       {:ok, %Koype.Http.Response{code: code, body: body} = _resp} when code < 400 ->
18 18
         json_body = Jason.decode!(body)
19
-        mf2_body = json_body[parser_info.extraction_path]
19
+        mf2_body = Map.get(json_body, parser_info.extraction_path, %{})
20 20
 
21 21
         Logger.info("Fetched useful response from parser.",
22 22
           name: parser_info.name,
@@ -24,21 +24,25 @@ defmodule Koype.Page.EndpointParser do
24 24
           id: parser_info.id
25 25
         )
26 26
 
27
-        structure = %Koype.Page.Structure{
28
-          author: Map.get(mf2_body, "author", [url]),
29
-          name: Map.get(mf2_body, "name", []) |> List.wrap() |> List.first(),
30
-          url: Map.get(mf2_body, "url", [url]),
31
-          content: Map.get(mf2_body, "content", [%{}]),
32
-          summary: Map.get(mf2_body, "summary", []),
33
-          media: [
34
-            photo: mf2_body["photo"],
35
-            video: mf2_body["video"],
36
-            audio: mf2_body["audio"]
37
-          ],
38
-          raw: mf2_body
39
-        }
40
-
41
-        {:ok, structure}
27
+        if mf2_body != %{} do
28
+          structure = %Koype.Page.Structure{
29
+            author: Map.get(mf2_body, "author", [url]),
30
+            name: Map.get(mf2_body, "name", []) |> List.wrap() |> List.first(),
31
+            url: Map.get(mf2_body, "url", [url]),
32
+            content: Map.get(mf2_body, "content", [%{}]),
33
+            summary: Map.get(mf2_body, "summary", []),
34
+            media: [
35
+              photo: mf2_body["photo"],
36
+              video: mf2_body["video"],
37
+              audio: mf2_body["audio"]
38
+            ],
39
+            raw: mf2_body
40
+          }
41
+
42
+          {:ok, structure}
43
+        else
44
+          {:error, reason: :no_information, json: json_body, key: parser_info.extraction_path}
45
+        end
42 46
 
43 47
       {:ok, %Koype.Http.Response{} = resp} ->
44 48
         Logger.warn("The endpoint provided failed out.",

+ 1
- 1
lib/repo/entry.ex View File

@@ -119,7 +119,7 @@ defmodule Koype.Repo.Entry do
119 119
   def published(entries, :future),
120 120
     do:
121 121
       from(e in with_post_status("published", entries),
122
-        where: not is_nil(e.published_at) and e.published_at > fragment("datetime('now')")
122
+        where: not is_nil(e.published_at) and e.published_at >= fragment("datetime('now')")
123 123
       )
124 124
 
125 125
   def published(entries, :past),

+ 4
- 13
lib/storage/base.ex View File

@@ -52,22 +52,13 @@ defmodule Koype.Storage.Base do
52 52
 
53 53
       def validate(args)
54 54
 
55
-      def validate({%{filename: filename} = _file, _}) do
56
-        file_extension = filename |> Path.extname() |> String.downcase()
57
-
58
-        if @extension_whitelist != [] do
59
-          Enum.member?(@extension_whitelist, file_extension)
60
-        end
61
-      end
55
+      def validate({%{filename: filename} = _file, _}), do: do_validate_extension_of_filename(filename)
56
+      def validate({%{file_name: filename} = _file, _}), do: do_validate_extension_of_filename(filename)
62 57
 
63
-      def validate({%{file_name: filename} = _file, _}) do
58
+      defp do_validate_extension_of_filename(filename) do
64 59
         file_extension = filename |> Path.extname() |> String.downcase()
65 60
 
66
-        if @extension_whitelist != [] do
67
-          Enum.member?(@extension_whitelist, file_extension)
68
-        end
69
-
70
-        true
61
+        Enum.member?(@extension_whitelist, String.trim_leading(file_extension, "."))
71 62
       end
72 63
 
73 64
       def path(value, version \\ :original)

+ 1
- 0
lib/storage/image.ex View File

@@ -19,5 +19,6 @@
19 19
 defmodule Koype.Storage.Image do
20 20
   use Koype.Storage.Base,
21 21
     directory: "images",
22
+  extension_whitelist: ~w(webp png gif jpg jpeg bmp),
22 23
     storage_unit: :image
23 24
 end

+ 1
- 1
priv/static/swagger.json View File

@@ -20,7 +20,7 @@
20 20
   "swagger": "2.0",
21 21
   "paths": {},
22 22
   "info": {
23
-    "version": "0.0.12+dev",
23
+    "version": "0.0.12+test",
24 24
     "title": "Koype",
25 25
     "license": {
26 26
       "name": "AGPLv3"

+ 2
- 2
test/integration/controllers/indie/micropub_controller_test.exs View File

@@ -69,7 +69,7 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
69 69
         |> post(@route, body)
70 70
         |> doc(
71 71
           description: "Creates a new form-encoded post",
72
-          operation_id: "micropub_form_encoded_post"
72
+          operation_id: "micropub_form_encoded_post_with_syndication_targets"
73 73
         )
74 74
 
75 75
       assert text_response(conn, :created)
@@ -114,7 +114,7 @@ defmodule Koype.Web.Indie.MicropubControllerTest do
114 114
         })
115 115
         |> doc(
116 116
           description: "Creates a new entry and categories with JSON",
117
-          operation_id: "micropub_json_post"
117
+          operation_id: "micropub_json_post_with_categories"
118 118
         )
119 119
 
120 120
       assert text_response(conn, :created)

+ 5
- 0
test/integration/controllers/indie/token_api_controller_test.exs View File

@@ -59,6 +59,7 @@ defmodule Koype.Web.Indie.TokenApiControllerTest do
59 59
       conn =
60 60
         build_conn()
61 61
         |> post(@route, Map.take(payload, ~w(client_id redirect_uri code me)))
62
+        |> doc(operation_id: "token_api_confirms_code")
62 63
 
63 64
       assert resp = json_response(conn, 200)
64 65
       assert %{"me" => ^me} = resp
@@ -67,6 +68,7 @@ defmodule Koype.Web.Indie.TokenApiControllerTest do
67 68
 
68 69
     test "400 code is invalid", %{invalid_payload: payload} do
69 70
       conn = post(build_conn(), @route, payload)
71
+        |> doc(operation_id: "token_api_invalid_code")
70 72
       assert resp = json_response(conn, :forbidden)
71 73
       assert resp["message"] =~ "not have any scopes"
72 74
     end
@@ -76,6 +78,7 @@ defmodule Koype.Web.Indie.TokenApiControllerTest do
76 78
       :ok = IndieWeb.Auth.Scope.persist!(code, context[:scope])
77 79
       IndieWeb.Auth.Code.persist(code, context[:client_id], context[:redirect_uri])
78 80
       conn = post(build_conn(), @route, Map.merge(context[:payload], %{"code" => code}))
81
+        |> doc(operation_id: "token_api_scope_missing_for_code")
79 82
       assert json_response(conn, :forbidden)
80 83
       assert json_response(conn, :forbidden)["message"] =~ "was not valid"
81 84
     end
@@ -98,6 +101,7 @@ defmodule Koype.Web.Indie.TokenApiControllerTest do
98 101
             }
99 102
           )
100 103
         )
104
+        |> doc(operation_id: "token_api_client_id_mismatch")
101 105
 
102 106
       assert resp = json_response(conn, :forbidden)
103 107
       assert resp["message"] =~ "did not match the provided client ID"
@@ -121,6 +125,7 @@ defmodule Koype.Web.Indie.TokenApiControllerTest do
121 125
             }
122 126
           )
123 127
         )
128
+        |> doc(operation_id: "token_api_redirect_uri_mismatch")
124 129
 
125 130
       assert resp = json_response(conn, :forbidden)
126 131
       assert resp["message"] =~ "did not match the provided redirect URI"

+ 2
- 3
test/test_helper.exs View File

@@ -5,9 +5,8 @@ end)
5 5
 
6 6
 Bureaucrat.start(
7 7
   env_var: "DOC",
8
-  writer: Bureaucrat.SwaggerSlateMarkdownWriter,
9
-  default_path: "doc/api/source/index.html.md",
10
-  paths: [{Koype.Web, "web/controllers"}],
8
+  writer: Bureaucrat.MarkdownWriter,
9
+  default_path: "docs/api/source/index.html.md",
11 10
   swagger: "priv/static/swagger.json" |> File.read!() |> Jason.decode!(),
12 11
   json_library: Jason
13 12
 )

+ 0
- 2
test/unit/content_test.exs View File

@@ -33,13 +33,11 @@ defmodule Koype.ContentTest do
33 33
                "I tagged this **##{tag_name}** {: .p-category} as a test."
34 34
     end
35 35
 
36
-    @tag skip: true
37 36
     test "converts text into Markdown with emoji" do
38 37
       assert Subject.to(:markdown, "I :cry: often.") ==
39 38
                "I <span data-emoji-code=\"cry\" title=\"cry\">&#x1F62F;</span> often."
40 39
     end
41 40
 
42
-    @tag skip: true
43 41
     test "converts text into Markdown with group-tag" do
44 42
       assert Subject.to(:markdown, "I tagged this +group a test.") ==
45 43
                "I tagged this [+group](/groups/post) {: .u-category .h-card} as a test."

+ 1
- 0
test/unit/indieweb/mf2_test.exs View File

@@ -42,6 +42,7 @@ defmodule IndieWeb.MF2Test do
42 42
       }
43 43
     ]
44 44
   }
45
+
45 46
   @empty_mf2 %{
46 47
     "items" => []
47 48
   }

+ 3
- 2
test/unit/indieweb/micropub/content_test.exs View File

@@ -47,8 +47,9 @@ defmodule IndieWeb.Micropub.ContentTest do
47 47
       assert {:ok, "Humans"} == Content.process_property("name", ["Humans"])
48 48
     end
49 49
 
50
-    @tag skip: true
51
-    test "processes location"
50
+    test "processes location from a geo URI" do
51
+      assert {:ok, %{lat: 30.00, lng: -70.40}} == Content.process_property("location", ["geo:30.00,-70.40"])
52
+    end
52 53
   end
53 54
 
54 55
   describe ".parse_extensions/2" do

+ 27
- 2
test/unit/page/parser_test.exs View File

@@ -28,8 +28,33 @@ defmodule Koype.Page.ParserTest do
28 28
              } = Subject.convert_to_mf2(structure)
29 29
     end
30 30
 
31
-    @tag skip: true
32
-    test "ensure media from remote resource is imported"
31
+    test "ensure media from remote resource is imported" do
32
+      fake_photo_url = Faker.Avatar.image_url
33
+      structure = %Koype.Page.Structure{
34
+        name: Faker.Lorem.sentence(),
35
+        url: Faker.Internet.url(),
36
+        content: %{text: [Faker.Lorem.sentence()]},
37
+        author: Faker.Internet.url(),
38
+        media: [photo: [fake_photo_url]]
39
+      }
40
+
41
+      properties = %{
42
+        "content" => structure.content,
43
+        "name" => structure.name,
44
+        "url" => structure.url,
45
+        "author" => structure.author,
46
+        "photo" => structure.media[:photo]
47
+      }
48
+
49
+      assert %{
50
+               "items" => [
51
+                 %{
52
+                   "type" => ["h-entry"],
53
+                   "properties" => ^properties
54
+                 }
55
+               ]
56
+      } = Subject.convert_to_mf2(structure)
57
+    end
33 58
 
34 59
     test "fails if required parameters are missing" do
35 60
       structure = %Koype.Page.Structure{

+ 0
- 8
test/unit/repo/webmention_test.exs View File

@@ -31,14 +31,6 @@ defmodule Koype.Repo.WebmentionTest do
31 31
       refute cs.valid?
32 32
     end
33 33
 
34
-    @tag skip: true
35
-    test "fails if not unique" do
36
-      params = params_for(:webmention)
37
-      insert(:webmention, params)
38
-      cs = Subject.changeset(%Subject{}, params)
39
-      refute cs.valid?
40
-    end
41
-
42 34
     test "passes" do
43 35
       entry = insert(:entry)
44 36
       url = Koype.Repo.Entry.get_path(entry)

+ 9
- 10
test/unit/storage/image_test.exs View File

@@ -1,36 +1,35 @@
1 1
 defmodule Koype.Storage.ImageTest do
2 2
   use Koype.Test.BaseCase
3
-  alias Koype.Storage.Image
4
-  doctest Image
3
+  alias Koype.Storage.Image, as: Subject
4
+  doctest Subject
5 5
 
6 6
   @model %Koype.Repo.Entry{id: "foo", __meta__: %{source: {nil, "test"}}}
7 7
 
8 8
   describe ".storage_dir/2" do
9 9
     test "produces path for models" do
10
-      assert "images/test/foo" = Image.storage_dir(:test, {nil, @model})
10
+      assert "images/test/foo" = Subject.storage_dir(:test, {nil, @model})
11 11
     end
12 12
 
13 13
     test "stores floating, arbitrary images" do
14
-      assert "images/floating" = Image.storage_dir(:test, {nil, :floating})
14
+      assert "images/floating" = Subject.storage_dir(:test, {nil, :floating})
15 15
     end
16 16
   end
17 17
 
18 18
   describe ".filename/2" do
19 19
     test "adapts on provided name and version" do
20
-      assert "original_foo.jpg" = Image.filename(:original, {%{filename: "foo.jpg"}, nil})
21
-      assert "original_foo.jpg" = Image.filename(:original, {%{file_name: "foo.jpg"}, nil})
20
+      assert "original_foo.jpg" = Subject.filename(:original, {%{filename: "foo.jpg"}, nil})
21
+      assert "original_foo.jpg" = Subject.filename(:original, {%{file_name: "foo.jpg"}, nil})
22 22
     end
23 23
   end
24 24
 
25 25
   describe ".validate/1" do
26
-    @describetag skip: true
27 26
     test "allows image type to be used" do
28
-      assert Image.validate({%{filename: "foo.jpg"}, nil})
29
-      assert Image.validate({%{file_name: "foo.jpg"}, nil})
27
+      assert Subject.validate({%{filename: "foo.jpg"}, nil})
28
+      assert Subject.validate({%{file_name: "foo.jpg"}, nil})
30 29
     end
31 30
 
32 31
     test "prohibits non-image files" do
33
-      refute Image.validate({%{filename: "foo.xml"}, nil})
32
+      refute Subject.validate({%{filename: "foo.xml"}, nil})
34 33
     end
35 34
   end
36 35
 end

+ 4
- 2
test/unit/storage_test.exs View File

@@ -101,10 +101,12 @@ defmodule Koype.StorageTest do
101 101
     end
102 102
 
103 103
     test "gives proper module for audio" do
104
+      assert Koype.Storage.Audio = Subject.module_for_type("audio")
104 105
     end
105 106
 
106
-    @tag skip: true
107
-    test "fails out for unknown file class"
107
+    test "fails out for unknown file class" do
108
+      refute Subject.module_for_type("foobarz")
109
+    end
108 110
   end
109 111
 
110 112
   describe ".upload_properties_inline/2" do

+ 5
- 11
test/unit/timeline_test.exs View File

@@ -2,24 +2,22 @@ defmodule Koype.TimelineTest do
2 2
   use Koype.DataCase
3 3
   import Koype.Factory
4 4
   alias Koype.Timeline, as: Subject
5
-  @moduletag skip: true
6 5
 
7 6
   doctest Subject
8 7
 
9 8
   defp do_convert_list(list),
10
-    do: Enum.map(list, fn e -> {e.published_at, e.type} end)
9
+    do:  list |> Enum.map(fn e -> {e.published_at, e.type} end)
11 10
 
12 11
   @response_types IndieWeb.Post.response_types()
13 12
   @page_size Enum.random(5..7)
14 13
 
15 14
   describe ".sequential/1" do
16 15
     test "gets list of all published posts in descending order" do
17
-      Koype.Repo.delete_all(Koype.Repo.Entry)
18
-      known_posts = insert_list(20, :entry, post_status: "published")
16
+      known_posts = insert_list(20, :entry, post_status: "published", published_at: Faker.DateTime.backward(1))
19 17
 
20 18
       expected_post_ids =
21 19
         known_posts
22
-        |> Enum.sort(&(&1.published_at > &2.published_at))
20
+        |> Enum.sort(&(&1.published_at <= &2.published_at))
23 21
         |> Enum.take(@page_size)
24 22
         |> do_convert_list
25 23
 
@@ -29,8 +27,7 @@ defmodule Koype.TimelineTest do
29 27
     end
30 28
 
31 29
     test "gets list of all published responses in descending order" do
32
-      Koype.Repo.delete_all(Koype.Repo.Entry)
33
-      known_posts = insert_list(20, :entry, post_status: "published")
30
+      known_posts = insert_list(20, :entry, post_status: "published", published_at: Faker.DateTime.backward(1))
34 31
 
35 32
       expected_post_ids =
36 33
         known_posts
@@ -45,16 +42,13 @@ defmodule Koype.TimelineTest do
45 42
     end
46 43
 
47 44
     test "gets list of all published posts of a particular type in descending order" do
48
-      Koype.Repo.delete_all(Koype.Repo.Entry)
49 45
       testing_post_type = Enum.random(@response_types) |> Atom.to_string()
50
-      now = Calendar.DateTime.now_utc()
51 46
 
52 47
       known_posts =
53
-        insert_list(@page_size + 25, :entry, post_status: "published", type: testing_post_type)
48
+        insert_list(@page_size + 25, :entry, post_status: "published", type: testing_post_type, published_at: Faker.DateTime.backward(1))
54 49
 
55 50
       expected_post_ids =
56 51
         known_posts
57
-        |> Enum.filter(fn e -> e.published_at > now end)
58 52
         |> Enum.filter(fn e -> e.type == testing_post_type end)
59 53
         |> Enum.sort(&(&1.published_at > &2.published_at))
60 54
         |> Enum.take(@page_size)

+ 3
- 1
web/controllers/timeline_controller.ex View File

@@ -19,6 +19,8 @@
19 19
 defmodule Koype.Web.TimelineController do
20 20
   use Koype.Web, :controller
21 21
 
22
+  @format_timer 5_000
23
+
22 24
   defp do_async_formatting(entries) do
23 25
     entries
24 26
     |> Enum.map(fn entry ->
@@ -26,7 +28,7 @@ defmodule Koype.Web.TimelineController do
26 28
         Koype.Template.format(entry)
27 29
       end)
28 30
     end)
29
-    |> Task.yield_many(10_000)
31
+    |> Task.yield_many(@format_timer)
30 32
     |> Enum.map(fn
31 33
       {task, nil} ->
32 34
         Task.shutdown(task, :brutual_kill)

Loading…
Cancel
Save