Skip to content

Commit 0bc8ba4

Browse files
author
Ashu Goel
committed
[suppression_list] minor PR fixes and test suite
1 parent 3040954 commit 0bc8ba4

8 files changed

+116
-5
lines changed

lib/suppression_list.ex

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
defmodule SparkPost.SuppressionList do
22
@moduledoc """
33
The SparkPost Suppression List API for working with suppression lists.
4-
Use `SparkPost.SupressionList.search/1` to search through your account's suppression list.
4+
Use `SparkPost.SuppressionList.search/1` to search through your account's suppression list.
55
66
Check out the documentation for each function
77
or use the [SparkPost API reference](https://developers.sparkpost.com/api/suppression_list.html) for details.
88
9-
Returned by `SparkPost.SupressionList.search/1`.
9+
Returned by `SparkPost.SuppressionList.search/1`.
1010
- %SparkPost.SuppressionList.SearchResult{}
1111
"""
1212

@@ -32,7 +32,12 @@ defmodule SparkPost.SuppressionList do
3232
response = Endpoint.request(:get, "suppression-list", %{}, %{}, [params: params], false)
3333
case response do
3434
%SparkPost.Endpoint.Response{results: body} ->
35-
struct(SparkPost.SuppressionList.SearchResult, body)
35+
mapped_results = Enum.map(body.results, fn res -> struct(SparkPost.SuppressionList.ListEntry, res) end)
36+
%SparkPost.SuppressionList.SearchResult{
37+
results: mapped_results,
38+
links: body.links,
39+
total_count: body.total_count
40+
}
3641
_ -> response
3742
end
3843
end
12 KB
Binary file not shown.

lib/suppression_list/list_entry.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ defmodule SparkPost.SuppressionList.ListEntry do
1414
defstruct recipient: :required,
1515
type: :required,
1616
source: nil,
17-
description: nil
17+
description: nil,
18+
transactional: nil,
19+
non_transactional: nil
1820
end

lib/suppression_list/search_result.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ defmodule SparkPost.SuppressionList.SearchResult do
99
- total_count: Total number of results across all pages based on the query params.
1010
"""
1111

12-
defstruct results: :required, links: :required, total_count: :required
12+
defstruct results: :required,
13+
links: :required,
14+
total_count: :required
1315
end

test/.suppression_list_test.exs.swp

12 KB
Binary file not shown.

test/data/suppressionsearch.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"results": [
3+
{
4+
"recipient": "test@marketing.com",
5+
"type": "non_transactional"
6+
}
7+
],
8+
"links": [],
9+
"total_count": 1
10+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"results": [
3+
{
4+
"recipient": "test@marketing.com",
5+
"type": "non_transactional"
6+
}
7+
],
8+
"links": [
9+
{
10+
"href": "/currentlink",
11+
"rel": "first"
12+
},
13+
{
14+
"href": "/linkwithcursor",
15+
"rel": "next"
16+
}
17+
],
18+
"total_count": 1
19+
}

test/suppression_list_test.exs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
defmodule SparkPost.SuppressionListTest do
2+
@moduledoc false
3+
4+
use ExUnit.Case, async: false
5+
6+
alias SparkPost.{MockServer, SuppressionList}
7+
alias SparkPost.SuppressionList.{SearchResult, ListEntry}
8+
9+
import Mock
10+
11+
test_with_mock "SuppressionList.search succeeds with SuppressionList.SearchResult",
12+
HTTPoison, [request: fn (method, url, body, headers, opts) ->
13+
assert method == :get
14+
fun = MockServer.mk_http_resp(200, MockServer.get_json("suppressionsearch"))
15+
fun.(method, url, body, headers, opts)
16+
end] do
17+
resp = SuppressionList.search()
18+
assert %SearchResult{} = resp
19+
end
20+
21+
test_with_mock "SuppressionList.search fails with Endpoint.Error", HTTPoison,
22+
[request: MockServer.mk_fail] do
23+
resp = SuppressionList.search()
24+
assert %SparkPost.Endpoint.Error{} = resp
25+
end
26+
27+
test_with_mock "SuppressionList.search creates ListEntry structs", HTTPoison,
28+
[request: fn (method, url, body, headers, opts) ->
29+
assert method == :get
30+
fun = MockServer.mk_http_resp(200, MockServer.get_json("suppressionsearch"))
31+
fun.(method, url, body, headers, opts)
32+
end] do
33+
resp = SuppressionList.search()
34+
assert %SearchResult{
35+
results: [
36+
%ListEntry{
37+
recipient: "test@marketing.com",
38+
type: "non_transactional",
39+
source: nil,
40+
description: nil,
41+
non_transactional: nil
42+
}
43+
],
44+
links: [],
45+
total_count: 1
46+
} == resp
47+
end
48+
49+
test_with_mock "SuppressionList.search parses out cursor info", HTTPoison,
50+
[request: fn (method, url, body, headers, opts) ->
51+
assert method == :get
52+
fun = MockServer.mk_http_resp(200, MockServer.get_json("suppressionsearch_links"))
53+
fun.(method, url, body, headers, opts)
54+
end] do
55+
resp = SuppressionList.search()
56+
assert %SearchResult{
57+
results: [
58+
%ListEntry{
59+
recipient: "test@marketing.com",
60+
type: "non_transactional",
61+
source: nil,
62+
description: nil,
63+
non_transactional: nil
64+
}
65+
],
66+
links: [
67+
%{href: "/currentlink", rel: "first"},
68+
%{href: "/linkwithcursor", rel: "next"}
69+
],
70+
total_count: 1
71+
} == resp
72+
end
73+
end

0 commit comments

Comments
 (0)