Skip to content

Commit

Permalink
Simplify http mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tfinnell committed Nov 9, 2017
1 parent 5d59d29 commit 3a248d6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 94 deletions.
146 changes: 52 additions & 94 deletions test/facebook_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule FacebookTest do

import Mock

alias Facebook.GraphMock

@app_id "123"
@app_secret "456"
# 19292868552 = facebook for developers page
Expand All @@ -22,10 +24,8 @@ defmodule FacebookTest do
describe "me" do
test "success", %{id: id, access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.me(:success) end
request: GraphMock.request,
body: fn(_) -> GraphMock.me(:success) end
] do
assert {:ok, user} = Facebook.me("id,first_name", access_token)
assert(user["id"] == id)
Expand All @@ -35,10 +35,8 @@ defmodule FacebookTest do

test "error", %{invalid_access_token: invalid_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
request: GraphMock.request,
body: fn(_) -> GraphMock.error() end
] do
assert {:error, %{"code" => _,"message" => _,}} = Facebook.me(
"id,first_name",
Expand All @@ -51,10 +49,8 @@ defmodule FacebookTest do
describe "picture" do
test "success", %{id: id, access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.picture(:success) end
request: GraphMock.request,
body: fn(_) -> GraphMock.picture(:success) end
] do
{:ok, %{"data" => picture_data}} = Facebook.picture(
id,
Expand All @@ -68,10 +64,8 @@ defmodule FacebookTest do

test "error", %{id: id, invalid_access_token: invalid_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
request: GraphMock.request,
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.picture(id, "small", invalid_access_token)
end
Expand All @@ -81,10 +75,8 @@ defmodule FacebookTest do
describe "publish" do
test "feed - success", %{id: id, access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.publish(:success, :feed) end
request: GraphMock.request,
body: fn(_) -> GraphMock.publish(:success, :feed) end
] do
{:ok, response} = Facebook.publish(
:feed,
Expand All @@ -102,7 +94,7 @@ defmodule FacebookTest do
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.publish(
:feed,
Expand All @@ -116,10 +108,8 @@ defmodule FacebookTest do

test "photo - success", %{id: id, access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.publish(:success, :image) end
request: GraphMock.request,
body: fn(_) -> GraphMock.publish(:success, :image) end
] do
file_path = "test/assets/sample_image.png"
assert {:ok, %{"id" => _, "post_id" => _}} = Facebook.publish(
Expand All @@ -137,7 +127,7 @@ defmodule FacebookTest do
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
body: fn(_) -> GraphMock.error() end
] do
file_path = "test/assets/sample_image.png"
assert {:error, _} = Facebook.publish(
Expand All @@ -152,10 +142,8 @@ defmodule FacebookTest do

test "video - success", %{id: id, access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.publish(:success, :image) end
request: GraphMock.request,
body: fn(_) -> GraphMock.publish(:success, :image) end
] do
file_path = "test/assets/sample_video.mpg"
assert {:ok, response} = Facebook.publish(
Expand All @@ -174,7 +162,7 @@ defmodule FacebookTest do
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
body: fn(_) -> GraphMock.error() end
] do
file_path = "test/assets/sample_image.png"
assert {:error, _} = Facebook.publish(
Expand All @@ -191,10 +179,8 @@ defmodule FacebookTest do
describe "my_likes" do
test "success", %{access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.my_likes(:success) end
request: GraphMock.request,
body: fn(_) -> GraphMock.my_likes(:success) end
] do
{:ok, likes_data} = Facebook.my_likes(access_token)

Expand All @@ -204,10 +190,8 @@ defmodule FacebookTest do

test "error", %{invalid_access_token: invalid_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
request: GraphMock.request,
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.my_likes(invalid_access_token)
end
Expand All @@ -217,10 +201,8 @@ defmodule FacebookTest do
describe "permissions" do
test "success", %{id: id, access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.permissions(:success) end
request: GraphMock.request,
body: fn(_) -> GraphMock.permissions(:success) end
] do
assert {:ok, %{"data" => data}} = Facebook.permissions(id, access_token)

Expand All @@ -232,10 +214,8 @@ defmodule FacebookTest do

test "error", %{id: id, invalid_access_token: invalid_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
request: GraphMock.request,
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.permissions(id, invalid_access_token)
end
Expand All @@ -245,10 +225,8 @@ defmodule FacebookTest do
describe "fan_count" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.page(:success, :fan_count) end
request: GraphMock.request,
body: fn(_) -> GraphMock.page(:success, :fan_count) end
] do
assert {:ok, %{"fan_count" => _}} = Facebook.fan_count(
@page_id,
Expand All @@ -259,10 +237,8 @@ defmodule FacebookTest do

test "error", %{invalid_access_token: invalid_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
request: GraphMock.request,
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.fan_count(@page_id, invalid_access_token)
end
Expand All @@ -272,10 +248,8 @@ defmodule FacebookTest do
describe "page" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.page(:success) end
request: GraphMock.request,
body: fn(_) -> GraphMock.page(:success) end
] do
assert {:ok, %{
"name" => name,
Expand All @@ -289,10 +263,8 @@ defmodule FacebookTest do

test "error", %{invalid_access_token: invalid_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
request: GraphMock.request,
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.page(@page_id, invalid_access_token)
end
Expand All @@ -302,10 +274,8 @@ defmodule FacebookTest do
describe "page with fields" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.page(:success, :with_fields) end
request: GraphMock.request,
body: fn(_) -> GraphMock.page(:success, :with_fields) end
] do
assert {:ok, %{
"id" => id,
Expand All @@ -322,7 +292,7 @@ defmodule FacebookTest do
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.page(
@page_id,
Expand All @@ -336,10 +306,8 @@ defmodule FacebookTest do
describe "page feed" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.page(:success, :feed) end
request: GraphMock.request,
body: fn(_) -> GraphMock.page(:success, :feed) end
] do
assert {:ok, %{"data" => [data | _]}} = Facebook.page_feed(
:feed,
Expand All @@ -362,7 +330,7 @@ defmodule FacebookTest do
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.page_feed(
:feed,
Expand All @@ -377,10 +345,8 @@ defmodule FacebookTest do
describe "object count" do
test "success", %{access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.object_count(:success, :likes) end
request: GraphMock.request,
body: fn(_) -> GraphMock.object_count(:success, :likes) end
] do
assert {:ok, 10} = Facebook.object_count(
:likes,
Expand All @@ -394,10 +360,8 @@ defmodule FacebookTest do
describe "object reaction count" do
test "success", %{access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.object_count(:success, :likes) end
request: GraphMock.request,
body: fn(_) -> GraphMock.object_count(:success, :likes) end
] do
assert {:ok, 10} = Facebook.object_count(
:reaction,
Expand All @@ -412,10 +376,8 @@ defmodule FacebookTest do
describe "object count all" do
test "success", %{access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.object_count_all(:success) end
request: GraphMock.request,
body: fn(_) -> GraphMock.object_count_all(:success) end
] do
assert {:ok, %{"haha" => haha, "love" => love}} = Facebook.object_count_all(
"#{@test_page_id}_629967087187379",
Expand All @@ -431,10 +393,8 @@ defmodule FacebookTest do
describe "long lived access token" do
test "success", %{access_token: access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.long_lived_access_token(:success) end
request: GraphMock.request,
body: fn(_) -> GraphMock.long_lived_access_token(:success) end
] do
assert {:ok, %{
"access_token" => access_token,
Expand All @@ -457,7 +417,7 @@ defmodule FacebookTest do
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.error() end
body: fn(_) -> GraphMock.error() end
] do
assert {:error, _} = Facebook.long_lived_access_token(
@app_id,
Expand All @@ -471,10 +431,8 @@ defmodule FacebookTest do
describe "new stream" do
test "success", %{app_access_token: app_access_token} do
with_mock :hackney, [
request: fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end,
body: fn(_) -> Facebook.GraphMock.page(:success, :feed) end
request: GraphMock.request,
body: fn(_) -> GraphMock.page(:success, :feed) end
] do
posts = Facebook.page_feed(
:feed,
Expand Down
6 changes: 6 additions & 0 deletions test/graph_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,10 @@ defmodule Facebook.GraphMock do
"id": "120762398683628",
})
end

def request do
fn(_method, _url, _headers, _payload, _options) ->
{:ok, nil, nil, nil}
end
end
end

0 comments on commit 3a248d6

Please sign in to comment.