コード例 #1
0
func withSubscription(endpoint, groupName, sessionID, planID string, f func(subscriptionID string)) {
	createURL := endpoint + EndpointSubscriptionCreate
	deleteURL := endpoint + EndpointSubscriptionCancel

	group, err := modelhelper.GetGroup(groupName)
	tests.ResultedWithNoErrorCheck(group, err)

	Convey("We should be able to create a subscription", func() {
		req, err := json.Marshal(&stripe.SubParams{
			Customer: group.Payment.Customer.ID,
			Plan:     planID,
		})
		tests.ResultedWithNoErrorCheck(req, err)

		res, err := rest.DoRequestWithAuth("POST", createURL, req, sessionID)
		tests.ResultedWithNoErrorCheck(res, err)

		v := &stripe.Sub{}
		err = json.Unmarshal(res, v)
		So(err, ShouldBeNil)

		f(v.ID)

		Convey("We should be able to cancel the subscription", func() {
			res, err = rest.DoRequestWithAuth("DELETE", deleteURL, req, sessionID)
			tests.ResultedWithNoErrorCheck(res, err)

			v = &stripe.Sub{}
			err = json.Unmarshal(res, v)
			So(err, ShouldBeNil)
			So(v.Status, ShouldEqual, "canceled")
		})
	})
}
コード例 #2
0
func TestCreateSubscription(t *testing.T) {
	Convey("Given stub data", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withTestPlan(func(planID string) {
					withTestCreditCardToken(func(token string) {
						updateURL := endpoint + EndpointCustomerUpdate
						cp := &stripe.CustomerParams{
							Source: &stripe.SourceParams{
								Token: token,
							},
						}
						req, err := json.Marshal(cp)
						tests.ResultedWithNoErrorCheck(req, err)
						res, err := rest.DoRequestWithAuth("POST", updateURL, req, sessionID)
						tests.ResultedWithNoErrorCheck(res, err)

						withSubscription(endpoint, groupName, sessionID, planID, func(subscriptionID string) {
							So(subscriptionID, ShouldNotBeEmpty)
							getURL := endpoint + EndpointSubscriptionGet
							res, err := rest.DoRequestWithAuth("GET", getURL, nil, sessionID)
							tests.ResultedWithNoErrorCheck(res, err)

							v := &stripe.Sub{}
							err = json.Unmarshal(res, v)
							So(err, ShouldBeNil)
							So(v.Status, ShouldEqual, "active")
						})
					})
				})
			})
		})
	})
}
コード例 #3
0
func TestCreditCardAuthRetryFail(t *testing.T) {
	Convey("When a non subscribed user request for Auth", t, func() {
		withTestServer(t, func(endpoint string) {
			withTestCreditCardToken(func(token string) {
				testUsername := "guest-testcreditcardauth"
				testGroupName := "guests"

				ses, err := modelhelper.CreateSessionForAccount(testUsername, testGroupName)
				tests.ResultedWithNoErrorCheck(ses, err)

				cp := &stripe.ChargeParams{
					Source: &stripe.SourceParams{
						Token: token,
					},
					Email: "[email protected]",
				}

				req, err := json.Marshal(cp)
				tests.ResultedWithNoErrorCheck(req, err)

				res, err := rest.DoRequestWithAuth("POST", endpoint+EndpointCreditCardAuth, req, ses.ClientId)
				So(err, ShouldBeNil)

				res, err = rest.DoRequestWithAuth("POST", endpoint+EndpointCreditCardAuth, req, ses.ClientId)
				So(err, ShouldNotBeNil)
				So(res, ShouldBeNil)
			})
		})
	})
}
コード例 #4
0
func TestPing(t *testing.T) {
	Convey("Given testing user & group", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				Convey("We should be able to send the ping request to", func() {
					externalURL := endpoint + presence.EndpointPresencePing
					privateURL := endpoint + presence.EndpointPresencePingPrivate

					acc := &models.Account{}
					err := acc.ByNick(username)
					tests.ResultedWithNoErrorCheck(acc, err)

					Convey("external endpoint", func() {
						_, err := rest.DoRequestWithAuth("GET", externalURL, nil, sessionID)
						So(err, ShouldBeNil)

						pp := &presence.PrivatePing{
							GroupName: groupName,
							Username:  username,
						}
						req, err := json.Marshal(pp)
						tests.ResultedWithNoErrorCheck(req, err)
						Convey("internal endpoint without auth", func() {
							_, err := rest.DoRequestWithAuth("POST", privateURL, req, "")
							So(err, ShouldBeNil)
						})
					})
				})
			})
		})
	})
}
コード例 #5
0
func TestInvoiceList(t *testing.T) {
	Convey("Given stub data", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withTestPlan(func(planID string) {
					createURL := endpoint + EndpointSubscriptionCreate
					deleteURL := endpoint + EndpointSubscriptionCancel

					group, err := modelhelper.GetGroup(groupName)
					tests.ResultedWithNoErrorCheck(group, err)

					addCreditCardToUserWithChecks(endpoint, sessionID)

					Convey("We should be able to create a subscription", func() {
						req, err := json.Marshal(&stripe.SubParams{
							Customer: group.Payment.Customer.ID,
							Plan:     planID,
						})
						tests.ResultedWithNoErrorCheck(req, err)

						res, err := rest.DoRequestWithAuth("POST", createURL, req, sessionID)
						tests.ResultedWithNoErrorCheck(res, err)

						v := &stripe.Sub{}
						err = json.Unmarshal(res, v)
						So(err, ShouldBeNil)
						So(v.Status, ShouldEqual, "active")

						Convey("We should be able to list invoices", func() {
							listInvoicesURL := endpoint + EndpointInvoiceList
							res, err = rest.DoRequestWithAuth("GET", listInvoicesURL, nil, sessionID)
							tests.ResultedWithNoErrorCheck(res, err)

							var invoices []*stripe.Invoice
							err = json.Unmarshal(res, &invoices)
							So(err, ShouldBeNil)
							So(len(invoices), ShouldBeGreaterThan, 0)
							Convey("We should be able to list invoices with startingAfter query param", func() {
								listInvoicesURLWithQuery := fmt.Sprintf("%s%s?startingAfter=%s", endpoint, EndpointInvoiceList, invoices[0].ID)
								res, err = rest.DoRequestWithAuth("GET", listInvoicesURLWithQuery, nil, sessionID)
								tests.ResultedWithNoErrorCheck(res, err)

								var invoices []*stripe.Invoice
								err = json.Unmarshal(res, &invoices)
								So(err, ShouldBeNil)
								So(len(invoices), ShouldEqual, 0) // because we only have one invoice

								Convey("We should be able to cancel the subscription", func() {
									res, err = rest.DoRequestWithAuth("DELETE", deleteURL, req, sessionID)
									tests.ResultedWithNoErrorCheck(res, err)
								})
							})
						})
					})
				})
			})
		})
	})
}
コード例 #6
0
// make sure we can subscribe to 7 days-free plan with more trial period
func TestSubscribingToPaidPlanWithWithDifferentTrialPeriodThanDefault(t *testing.T) {
	Convey("Given stub data", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				pp := &stripe.PlanParams{
					Amount:        12345,
					Interval:      stripeplan.Month,
					IntervalCount: 1,
					TrialPeriod:   1, // trial for one day
					Name:          fmt.Sprintf("plan for %s", username),
					Currency:      currency.USD,
					ID:            fmt.Sprintf("plan_for_%s", username),
					Statement:     "NAN-FREE",
				}

				plan, err := stripeplan.New(pp)
				So(err, ShouldBeNil)

				addCreditCardToUserWithChecks(endpoint, sessionID)

				createURL := endpoint + EndpointSubscriptionCreate
				deleteURL := endpoint + EndpointSubscriptionCancel
				group, err := modelhelper.GetGroup(groupName)
				tests.ResultedWithNoErrorCheck(group, err)

				req, err := json.Marshal(&stripe.SubParams{
					Customer: group.Payment.Customer.ID,
					Plan:     plan.ID,
					TrialEnd: time.Now().Add(time.Hour * 48).Unix(),
				})
				tests.ResultedWithNoErrorCheck(req, err)

				sub, err := rest.DoRequestWithAuth("POST", createURL, req, sessionID)
				tests.ResultedWithNoErrorCheck(sub, err)

				v := &stripe.Sub{}
				err = json.Unmarshal(sub, v)
				So(err, ShouldBeNil)

				So(v.TrialEnd, ShouldBeGreaterThan, time.Now().Add(time.Hour*24*time.Duration(pp.TrialPeriod)).Unix())

				Convey("We should be able to cancel the subscription", func() {
					res, err := rest.DoRequestWithAuth("DELETE", deleteURL, req, sessionID)
					tests.ResultedWithNoErrorCheck(res, err)

					v := &stripe.Sub{}
					err = json.Unmarshal(res, v)
					So(err, ShouldBeNil)
					So(v.Status, ShouldEqual, "canceled")
				})
			})
		})
	})
}
コード例 #7
0
func TestCustomer(t *testing.T) {
	Convey("Given a user", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				Convey("Then Group should have customer id", func() {
					group, err := modelhelper.GetGroup(groupName)
					tests.ResultedWithNoErrorCheck(group, err)

					So(group.Payment.Customer.ID, ShouldNotBeBlank)
					Convey("We should be able to get the customer", func() {
						getURL := endpoint + EndpointCustomerGet

						res, err := rest.DoRequestWithAuth("GET", getURL, nil, sessionID)
						So(err, ShouldBeNil)
						So(res, ShouldNotBeNil)

						v := &stripe.Customer{}
						err = json.Unmarshal(res, v)
						So(err, ShouldBeNil)

						So(v.Deleted, ShouldEqual, false)
						So(v.Desc, ShouldContainSubstring, groupName)
						So(len(v.Meta), ShouldBeGreaterThanOrEqualTo, 2)
						So(v.Meta["groupName"], ShouldEqual, groupName)
						So(v.Meta["username"], ShouldEqual, username)

						Convey("After adding credit card to the user", func() {
							addCreditCardToUserWithChecks(endpoint, sessionID)

							res, err = rest.DoRequestWithAuth("GET", getURL, nil, sessionID)
							So(err, ShouldBeNil)
							So(res, ShouldNotBeNil)

							Convey("Customer should have CC assigned", func() {
								v = &stripe.Customer{}
								err = json.Unmarshal(res, v)
								So(err, ShouldBeNil)

								So(v.DefaultSource, ShouldNotBeNil)
								So(v.DefaultSource.Deleted, ShouldBeFalse)
								So(v.DefaultSource.ID, ShouldNotBeEmpty)
							})
						})
					})
				})
			})
		})
	})
}
コード例 #8
0
func TestCreditCardInfoNotSubscribingMember(t *testing.T) {
	Convey("When a non subscribed user request to get CC", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				group, err := modelhelper.GetGroup(groupName)
				tests.ResultedWithNoErrorCheck(group, err)

				err = modelhelper.UpdateGroupPartial(
					modelhelper.Selector{"_id": group.Id},
					modelhelper.Selector{
						"$unset": modelhelper.Selector{"payment.customer.id": ""},
					},
				)
				So(err, ShouldBeNil)

				Convey("Endpoint should return error", func() {
					_, err := rest.DoRequestWithAuth("GET", endpoint+EndpointCreditCardHas, nil, sessionID)
					So(err, ShouldNotBeNil)

					// set the customer id back becase test data callback requires it.
					err = modelhelper.UpdateGroupPartial(
						modelhelper.Selector{"_id": group.Id},
						modelhelper.Selector{
							"$set": modelhelper.Selector{"payment.customer.id": group.Payment.Customer.ID},
						},
					)
					So(err, ShouldBeNil)
				})
			})
		})
	})
}
コード例 #9
0
func TestWebhook(t *testing.T) {
	Convey("Given a user", t, func() {
		withTestServer(t, func(endpoint string) {
			webhookURL := endpoint + EndpointWebhook

			Convey("Should give error when event id is not valid", func() {
				req := []byte(webhookTestData["customer.subscription.created"])
				_, err := rest.DoRequestWithAuth("POST", webhookURL, req, "")
				So(err, ShouldNotBeNil)
			})

			Convey("Should not give error when event type is not supported", func() {
				req := []byte(webhookTestData["invalid.event_name"])
				_, err := rest.DoRequestWithAuth("POST", webhookURL, req, "")
				So(err, ShouldBeNil)
			})
		})
	})
}
コード例 #10
0
func TestCreditCardInfoLoggedOut(t *testing.T) {
	Convey("When a non registered request comes", t, func() {
		withTestServer(t, func(endpoint string) {
			Convey("Endpoint should return error", func() {
				_, err := rest.DoRequestWithAuth("GET", endpoint+EndpointCreditCardHas, nil, "")
				So(err, ShouldNotBeNil)
			})
		})
	})
}
コード例 #11
0
func TestCreditCardDeleteLoggedOut(t *testing.T) {
	Convey("When a non registered request comes", t, func() {
		withTestServer(t, func(endpoint string) {
			Convey("Endpoint should return error", func() {
				ccdeleteURL := endpoint + EndpointCreditCardDelete
				_, err := rest.DoRequestWithAuth("DELETE", ccdeleteURL, nil, "")
				So(err, ShouldNotBeNil)
			})
		})
	})
}
コード例 #12
0
func TestCreditCardInfo(t *testing.T) {
	Convey("Given a user", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				Convey("When a credit card added", func() {
					c1 := addCreditCardToUserWithChecks(endpoint, sessionID)

					Convey("Group should have the credit card", func() {
						_, err := rest.DoRequestWithAuth("GET", endpoint+EndpointCreditCardHas, nil, sessionID)
						So(err, ShouldBeNil)

						Convey("After updating the credit card", func() {
							c2 := addCreditCardToUserWithChecks(endpoint, sessionID)

							Convey("Group should still have a card", func() {
								_, err := rest.DoRequestWithAuth("GET", endpoint+EndpointCreditCardHas, nil, sessionID)
								So(err, ShouldBeNil)

								// current and previous cc id should not be same
								So(c1.DefaultSource.ID, ShouldNotEqual, c2.DefaultSource.ID)

								Convey("After deleting the credit card", func() {
									ccdeleteURL := endpoint + EndpointCreditCardDelete

									res, err := rest.DoRequestWithAuth("DELETE", ccdeleteURL, nil, sessionID)
									tests.ResultedWithNoErrorCheck(res, err)

									Convey("Customer should not have the a credit card", func() {
										_, err := rest.DoRequestWithAuth("GET", endpoint+EndpointCreditCardHas, nil, sessionID)
										So(err, ShouldNotBeNil)
									})
								})
							})
						})
					})
				})
			})
		})
	})
}
コード例 #13
0
func TestCreditCardDeleteNonAdmin(t *testing.T) {
	Convey("When a non admin user request comes", t, func() {
		withTestServer(t, func(endpoint string) {
			acc, _, groupName := models.CreateRandomGroupDataWithChecks()

			ses, err := modelhelper.CreateSessionForAccount(acc.Nick, groupName)
			tests.ResultedWithNoErrorCheck(ses, err)

			Convey("Endpoint should return error", func() {
				ccdeleteURL := endpoint + EndpointCreditCardDelete
				_, err := rest.DoRequestWithAuth("DELETE", ccdeleteURL, nil, ses.ClientId)
				So(err, ShouldNotBeNil)
			})
		})
	})
}
コード例 #14
0
func addCreditCardToUserWithChecks(endpoint, sessionID string) *stripe.Customer {
	customerUpdateURL := endpoint + EndpointCustomerUpdate
	c := &stripe.Customer{}
	withTestCreditCardToken(func(token string) {
		cp := &stripe.CustomerParams{
			Source: &stripe.SourceParams{
				Token: token,
			},
		}
		req, err := json.Marshal(cp)
		So(err, ShouldBeNil)
		So(req, ShouldNotBeNil)

		res, err := rest.DoRequestWithAuth("POST", customerUpdateURL, req, sessionID)
		So(err, ShouldBeNil)
		So(res, ShouldNotBeNil)

		err = json.Unmarshal(res, c)
		So(err, ShouldBeNil)
	})
	return c
}
コード例 #15
0
func TestCreditCardAuthNoSession(t *testing.T) {
	Convey("When a non subscribed user request for Auth", t, func() {
		withTestServer(t, func(endpoint string) {
			withTestCreditCardToken(func(token string) {
				cp := &stripe.ChargeParams{
					Source: &stripe.SourceParams{
						Token: token,
					},
					Email: "[email protected]",
				}

				req, err := json.Marshal(cp)
				tests.ResultedWithNoErrorCheck(req, err)

				res, err := rest.DoRequestWithAuth("POST", endpoint+EndpointCreditCardAuth, req, "")
				So(err, ShouldNotBeNil)
				So(res, ShouldBeNil)
				So(err.Error(), ShouldContainSubstring, "does not have session id")
			})
		})
	})
}
コード例 #16
0
func withStubData(endpoint string, f func(username string, groupName string, sessionID string)) {
	createURL := endpoint + EndpointCustomerCreate
	acc, _, groupName := models.CreateRandomGroupDataWithChecks()

	group, err := modelhelper.GetGroup(groupName)
	tests.ResultedWithNoErrorCheck(group, err)

	err = modelhelper.MakeAdmin(bson.ObjectIdHex(acc.OldId), group.Id)
	So(err, ShouldBeNil)

	ses, err := modelhelper.FetchOrCreateSession(acc.Nick, groupName)
	tests.ResultedWithNoErrorCheck(ses, err)

	req, err := json.Marshal(&stripe.CustomerParams{})
	tests.ResultedWithNoErrorCheck(req, err)

	res, err := rest.DoRequestWithAuth("POST", createURL, req, ses.ClientId)
	tests.ResultedWithNoErrorCheck(res, err)

	f(acc.Nick, groupName, ses.ClientId)

	So(payment.DeleteCustomerForGroup(groupName), ShouldBeNil)
}
コード例 #17
0
func TestCouponApply(t *testing.T) {
	Convey("Given a user", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withTestCoupon(func(couponID string) {
					Convey("After adding coupon to the user", func() {

						updateURL := endpoint + EndpointCustomerUpdate

						cp := &stripe.CustomerParams{
							Coupon: couponID,
						}

						req, err := json.Marshal(cp)
						So(err, ShouldBeNil)
						So(req, ShouldNotBeNil)

						res, err := rest.DoRequestWithAuth("POST", updateURL, req, sessionID)
						So(err, ShouldBeNil)
						So(res, ShouldNotBeNil)

						v := &stripe.Customer{}
						err = json.Unmarshal(res, v)
						So(err, ShouldBeNil)

						Convey("Customer should have coupon assigned", func() {
							So(v.Discount, ShouldNotBeNil)
							So(v.Discount.Coupon.ID, ShouldEqual, couponID)
							So(v.Discount.Coupon.Valid, ShouldBeTrue)
							So(v.Discount.Coupon.Deleted, ShouldBeFalse)
						})
					})
				})
			})
		})
	})
}
コード例 #18
0
func TestInfoPlan(t *testing.T) {
	Convey("Given a user", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withTestPlan(func(planID string) {
					addCreditCardToUserWithChecks(endpoint, sessionID)
					withSubscription(endpoint, groupName, sessionID, planID, func(subscriptionID string) {
						Convey("We should be able to get info", func() {
							infoURL := endpoint + EndpointInfo
							res, err := rest.DoRequestWithAuth("GET", infoURL, nil, sessionID)
							tests.ResultedWithNoErrorCheck(res, err)

							v := &payment.Usage{}
							err = json.Unmarshal(res, v)
							So(err, ShouldBeNil)

							So(v.ExpectedPlan.ID, ShouldEqual, planID)
						})
					})
				})
			})
		})
	})
}
コード例 #19
0
// make sure we can subscribe to a paid plan without a CC if it has trial period
func TestSubscribingToPaidPlanWithWithTrialPeriodHavingNoCC(t *testing.T) {
	Convey("Given stub data", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withNonFreeTestPlan(func(planID string) {
					createURL := endpoint + EndpointSubscriptionCreate
					group, err := modelhelper.GetGroup(groupName)
					tests.ResultedWithNoErrorCheck(group, err)

					Convey("We should not be able to create a subscription", func() {
						req, err := json.Marshal(&stripe.SubParams{
							Customer: group.Payment.Customer.ID,
							Plan:     planID,
						})
						tests.ResultedWithNoErrorCheck(req, err)

						_, err = rest.DoRequestWithAuth("POST", createURL, req, sessionID)
						So(err, ShouldNotBeNil)
					})
				})
			})
		})
	})
}
コード例 #20
0
func TestCreditCardDelete(t *testing.T) {
	Convey("Given a user", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				Convey("When a credit card added", func() {
					withTestCreditCardToken(func(token string) {
						updateURL := endpoint + EndpointCustomerUpdate
						getURL := endpoint + EndpointCustomerGet

						cp := &stripe.CustomerParams{
							Source: &stripe.SourceParams{
								Token: token,
							},
						}

						req, err := json.Marshal(cp)
						tests.ResultedWithNoErrorCheck(req, err)

						res, err := rest.DoRequestWithAuth("POST", updateURL, req, sessionID)
						So(err, ShouldBeNil)
						So(res, ShouldNotBeNil)
						Convey("Customer should have the credit card", func() {
							c1 := &stripe.Customer{}
							err = json.Unmarshal(res, c1)
							So(err, ShouldBeNil)

							So(c1.DefaultSource, ShouldNotBeNil)
							So(c1.DefaultSource.Deleted, ShouldBeFalse)
							So(c1.DefaultSource.ID, ShouldNotBeEmpty)
							Convey("After updating the credit card", func() {
								withTestCreditCardToken(func(token string) {
									cp := &stripe.CustomerParams{
										Source: &stripe.SourceParams{
											Token: token,
										},
									}

									req, err := json.Marshal(cp)
									tests.ResultedWithNoErrorCheck(req, err)

									res, err = rest.DoRequestWithAuth("POST", updateURL, req, sessionID)
									tests.ResultedWithNoErrorCheck(res, err)

									c2 := &stripe.Customer{}
									err = json.Unmarshal(res, c2)
									So(err, ShouldBeNil)
									Convey("Customer should have the new credit card", func() {
										So(c2.DefaultSource, ShouldNotBeNil)
										So(c2.DefaultSource.Deleted, ShouldBeFalse)
										So(c2.DefaultSource.ID, ShouldNotBeEmpty)

										// current and previous cc id should not be same
										So(c1.DefaultSource.ID, ShouldNotEqual, c2.DefaultSource.ID)

										Convey("After deleting the credit card", func() {
											ccdeleteURL := endpoint + EndpointCreditCardDelete

											_, err = rest.DoRequestWithAuth("DELETE", ccdeleteURL, nil, sessionID)
											tests.ResultedWithNoErrorCheck(res, err)

											res, err = rest.DoRequestWithAuth("GET", getURL, nil, sessionID)
											So(err, ShouldBeNil)
											So(res, ShouldNotBeNil)

											Convey("Customer should not have the a credit card", func() {
												c3 := &stripe.Customer{}
												err = json.Unmarshal(res, c3)
												So(err, ShouldBeNil)
												So(c3.DefaultSource, ShouldBeNil)
												So(len(c3.Sources.Values), ShouldEqual, 0)
											})
										})
									})
								})
							})
						})
					})
				})
			})
		})
	})
}
コード例 #21
0
func TestCancellingSubscriptionCreatesAnotherInvoice(t *testing.T) {
	Convey("Given stub data", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withTrialTestPlan(func(planID string) {
					createURL := endpoint + EndpointSubscriptionCreate
					deleteURL := endpoint + EndpointSubscriptionCancel
					customerUpdateURL := endpoint + EndpointCustomerUpdate

					group, err := modelhelper.GetGroup(groupName)
					tests.ResultedWithNoErrorCheck(group, err)

					withTestCreditCardToken(func(token string) {
						cp := &stripe.CustomerParams{
							Source: &stripe.SourceParams{
								Token: token,
							},
						}
						req, err := json.Marshal(cp)
						So(err, ShouldBeNil)
						So(req, ShouldNotBeNil)

						res, err := rest.DoRequestWithAuth("POST", customerUpdateURL, req, sessionID)
						So(err, ShouldBeNil)
						So(res, ShouldNotBeNil)

						req, err = json.Marshal(&stripe.SubParams{
							Customer: group.Payment.Customer.ID,
							Plan:     planID,
						})
						tests.ResultedWithNoErrorCheck(req, err)

						sub, err := rest.DoRequestWithAuth("POST", createURL, req, sessionID)
						tests.ResultedWithNoErrorCheck(sub, err)

						Convey("We should be able to list invoices", func() {
							listInvoicesURL := endpoint + EndpointInvoiceList
							res, err := rest.DoRequestWithAuth("GET", listInvoicesURL, nil, sessionID)
							tests.ResultedWithNoErrorCheck(res, err)

							var invoices []*stripe.Invoice
							err = json.Unmarshal(res, &invoices)
							So(err, ShouldBeNil)
							So(len(invoices), ShouldEqual, 1) // because we only have one invoice

							Convey("We should be able to cancel the subscription", func() {
								res, err := rest.DoRequestWithAuth("DELETE", deleteURL, req, sessionID)
								tests.ResultedWithNoErrorCheck(res, err)

								v := &stripe.Sub{}
								err = json.Unmarshal(res, v)
								So(err, ShouldBeNil)
								So(v.Status, ShouldEqual, "canceled")

								Convey("We should be able to list invoices with startingAfter query param", func() {
									listInvoicesURLWithQuery := endpoint + EndpointInvoiceList
									res, err = rest.DoRequestWithAuth("GET", listInvoicesURLWithQuery, nil, sessionID)
									tests.ResultedWithNoErrorCheck(res, err)

									var invoices []*stripe.Invoice
									err = json.Unmarshal(res, &invoices)
									So(err, ShouldBeNil)
									So(len(invoices), ShouldEqual, 2)
								})
							})
						})
					})
				})
			})
		})
	})
}
コード例 #22
0
func TestAtTheEndOfTrialPeriodSubscriptionStatusIsStillTrialing(t *testing.T) {
	Convey("Given stub data", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withTestCreditCardToken(func(token string) {
					req, err := json.Marshal(&stripe.CustomerParams{
						Source: &stripe.SourceParams{
							Token: token,
						},
					})
					tests.ResultedWithNoErrorCheck(req, err)

					res, err := rest.DoRequestWithAuth(
						"POST",
						endpoint+EndpointCustomerUpdate,
						req,
						sessionID,
					)
					tests.ResultedWithNoErrorCheck(res, err)

					createURL := endpoint + EndpointSubscriptionCreate
					group, err := modelhelper.GetGroup(groupName)
					tests.ResultedWithNoErrorCheck(group, err)

					pp := &stripe.PlanParams{
						Amount:        12345,
						Interval:      stripeplan.Month,
						IntervalCount: 1,
						TrialPeriod:   1, // trial for one day
						Name:          fmt.Sprintf("plan for %s", username),
						Currency:      currency.USD,
						ID:            fmt.Sprintf("plan_for_%s", username),
						Statement:     "NAN-FREE",
					}
					plan, err := stripeplan.New(pp)
					So(err, ShouldBeNil)
					defer stripeplan.Del(plan.ID)

					req, err = json.Marshal(&stripe.SubParams{
						Customer: group.Payment.Customer.ID,
						Plan:     plan.ID,
					})
					tests.ResultedWithNoErrorCheck(req, err)

					res, err = rest.DoRequestWithAuth("POST", createURL, req, sessionID)
					tests.ResultedWithNoErrorCheck(res, err)

					sub := &stripe.Sub{}
					err = json.Unmarshal(res, sub)
					So(err, ShouldBeNil)

					subParams := &stripe.SubParams{
						Customer: group.Payment.Customer.ID,
						Plan:     plan.ID,
						TrialEnd: time.Now().UTC().Add(time.Second * 60 * 5).Unix(),
					}
					sub, err = stripesub.Update(sub.ID, subParams)
					tests.ResultedWithNoErrorCheck(sub, err)

					sub, err = stripesub.Get(sub.ID, nil)
					tests.ResultedWithNoErrorCheck(sub, err)

					So(sub.Status, ShouldEqual, "trialing")
				})
			})
		})
	})
}