Skip to content

Commit e8a931c

Browse files
authored
Merge pull request #591 from code-corps/525-add-stripe-tracking
Add stripe tracking for some basic cases
2 parents 3193d98 + ed5b552 commit e8a931c

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

test/controllers/donation_goal_controller_test.exs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ defmodule CodeCorps.DonationGoalControllerTest do
4242
end
4343

4444
describe "create" do
45-
@tag authenticated: :admin
46-
test "creates and renders resource when data is valid", %{conn: conn} do
47-
project = insert(:project)
45+
@tag :authenticated
46+
test "creates and renders resource when data is valid", %{conn: conn, current_user: current_user} do
47+
organization = insert(:organization)
48+
insert(:organization_membership, member: current_user, organization: organization, role: "owner")
49+
project = insert(:project, organization: organization)
50+
4851
attrs = @valid_attrs |> Map.merge(%{project: project})
4952
assert conn |> request_create(attrs) |> json_response(201)
53+
54+
user_id = current_user.id
55+
assert_received {:track, ^user_id, "Created Donation Goal", %{}}
5056
end
5157

5258
@tag authenticated: :admin
@@ -65,11 +71,19 @@ defmodule CodeCorps.DonationGoalControllerTest do
6571
end
6672

6773
describe "update" do
68-
@tag authenticated: :admin
69-
test "updates and renders chosen resource when data is valid", %{conn: conn} do
70-
project = insert(:project)
74+
@tag :authenticated
75+
test "updates and renders chosen resource when data is valid", %{conn: conn, current_user: current_user} do
76+
organization = insert(:organization)
77+
insert(:organization_membership, member: current_user, organization: organization, role: "owner")
78+
project = insert(:project, organization: organization)
79+
80+
donation_goal = insert(:donation_goal, project: project)
81+
7182
attrs = @valid_attrs |> Map.merge(%{project: project})
72-
assert conn |> request_update(attrs) |> json_response(200)
83+
assert conn |> request_update(donation_goal, attrs) |> json_response(200)
84+
85+
user_id = current_user.id
86+
assert_received {:track, ^user_id, "Updated Donation Goal", %{}}
7387
end
7488

7589
@tag authenticated: :admin
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defmodule CodeCorps.StripeConnectSubscriptionControllerTest do
2+
use CodeCorps.ApiCase, resource_name: :stripe_connect_subscription
3+
4+
defp build_payload(user, project, quantity) do
5+
%{
6+
"data" => %{
7+
"attributes" => %{
8+
"quantity" => quantity,
9+
"project-id" => project.id
10+
},
11+
"relationships" => %{
12+
"user" => %{"data" => %{"id" => user.id}}
13+
}
14+
}
15+
}
16+
end
17+
18+
defp make_create_request(conn, payload) do
19+
path = conn |> stripe_connect_subscription_path(:create)
20+
21+
conn |> post(path, payload)
22+
end
23+
24+
describe "create" do
25+
@tag :authenticated
26+
test "creates and renders resource when user is authenticated and authorized", %{conn: conn, current_user: current_user} do
27+
# make project ready to accept donations
28+
organization = insert(:organization)
29+
insert(:stripe_connect_account, organization: organization, charges_enabled: true)
30+
project = insert(:project, organization: organization)
31+
insert(:stripe_connect_plan, project: project)
32+
33+
# make user ready to donate
34+
insert(:stripe_platform_customer, user: current_user)
35+
insert(:stripe_platform_card, user: current_user)
36+
37+
payload = build_payload(current_user, project, 10)
38+
assert conn |> make_create_request(payload) |> json_response(201)
39+
40+
user_id = current_user.id
41+
assert_received {:track, ^user_id, "Created Stripe Connect Subscription", %{}}
42+
end
43+
44+
test "does not create resource and renders 401 when unauthenticated", %{conn: conn} do
45+
assert conn |> request_create |> json_response(401)
46+
end
47+
48+
@tag :authenticated
49+
test "does not create resource and renders 403 when not authorized", %{conn: conn} do
50+
assert conn |> request_create |> json_response(403)
51+
end
52+
end
53+
end

web/controllers/donation_goal_controller.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ defmodule CodeCorps.DonationGoalController do
1313

1414
def filter(_conn, query, "id", id_list), do: id_filter(query, id_list)
1515

16-
def handle_create(_conn, attributes) do
16+
def handle_create(conn, attributes) do
1717
attributes
1818
|> DonationGoalsService.create
19+
|> CodeCorps.Analytics.Segment.track(:created, conn)
1920
end
2021

21-
def handle_update(_conn, record, attributes) do
22+
def handle_update(conn, record, attributes) do
2223
record
2324
|> DonationGoalsService.update(attributes)
25+
|> CodeCorps.Analytics.Segment.track(:updated, conn)
2426
end
2527
end

0 commit comments

Comments
 (0)