Skip to content

Commit 87dedbe

Browse files
committed
Subscription create tests, with segment tracking assertions
1 parent 3193d98 commit 87dedbe

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
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

0 commit comments

Comments
 (0)