From 236854207e7d8e511f9230d8bf64dab71f54574d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vidmantas=20Kabo=C5=A1is?= Date: Sun, 16 Oct 2016 13:25:30 +0200 Subject: [PATCH] Test Segment tracking after user creation --- config/test.exs | 2 +- lib/code_corps/analytics/test_api.ex | 18 ++++++++++ test/controllers/user_controller_test.exs | 40 +++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 lib/code_corps/analytics/test_api.ex diff --git a/config/test.exs b/config/test.exs index 0875da59e..ba20ca47b 100644 --- a/config/test.exs +++ b/config/test.exs @@ -28,7 +28,7 @@ config :code_corps, allowed_origins: ["http://localhost:4200"] config :guardian, Guardian, secret_key: "e62fb6e2746f6b1bf8b5b735ba816c2eae1d5d76e64f18f3fc647e308b0c159e" -config :code_corps, :analytics, CodeCorps.Analytics.InMemoryAPI +config :code_corps, :analytics, CodeCorps.Analytics.TestAPI config :code_corps, :icon_color_generator, CodeCorps.RandomIconColor.TestGenerator diff --git a/lib/code_corps/analytics/test_api.ex b/lib/code_corps/analytics/test_api.ex new file mode 100644 index 000000000..b3a77b093 --- /dev/null +++ b/lib/code_corps/analytics/test_api.ex @@ -0,0 +1,18 @@ +defmodule CodeCorps.Analytics.TestAPI do + @moduledoc """ + In-memory interface to simulate calling out to the Segment API, + sending back itself a message with passed parameters - they're used for assertions. + + Each function should have the same signature as `CodeCorps.Analytics.SegmentAPI` and simply return `nil`. + """ + + def identify(user_id, traits) do + send self(), {:identify, user_id, traits} + nil + end + + def track(user_id, event_name, properties) do + send self(), {:track, user_id, event_name, properties} + nil + end +end diff --git a/test/controllers/user_controller_test.exs b/test/controllers/user_controller_test.exs index 2adc5f75f..f45842eb6 100644 --- a/test/controllers/user_controller_test.exs +++ b/test/controllers/user_controller_test.exs @@ -91,6 +91,20 @@ defmodule CodeCorps.UserControllerTest do assert user.id == slugged_route.user_id end + test "calls segment tracking after user is created", %{conn: conn} do + conn = post conn, user_path(conn, :create), %{ + "meta" => %{}, + "data" => %{ + "type" => "user", + "attributes" => Map.put(@valid_attrs, :password, "password"), + "relationships" => relationships + } + } + id = json_response(conn, 201)["data"]["id"] + + assert_received {:track, id, "Signed Up", %{}} + end + test "does not create resource and renders errors when data is invalid", %{conn: conn} do conn = post conn, user_path(conn, :create), %{ "meta" => %{}, @@ -137,6 +151,32 @@ defmodule CodeCorps.UserControllerTest do assert user.biography == "Just a test user" end + test "tracks authentication & update profile events in Segment", %{conn: conn} do + user = insert(:user) + attrs = Map.put(@valid_attrs, :password, "password") + + params = %{ + "meta" => %{}, + "data" => %{ + "type" => "user", + "id" => user.id, + "attributes" => attrs, + "relationships" => relationships + } + } + + path = user_path(conn, :update, user) + + conn = + conn + |> authenticate(user) + |> put(path, params) + + id = json_response(conn, 200)["data"]["id"] + assert_received {:identify, id, attrs} + assert_received {:track, id, "Updated Profile", %{}} + end + test "does not update when authorized as different user", %{conn: conn} do [user, another_user] = insert_pair(:user)