<commit_before>package stripe

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"

	stripe "github.com/stripe/stripe-go"
	stripeSub "github.com/stripe/stripe-go/sub"
)

func TestSubscribe(t *testing.T) {
	Convey("Given nonexistent customer, plan", t, func() {
		token, accId, email := generateFakeUserInfo()
		err := Subscribe(token, accId, email, "hobbyist_month")

		So(err, ShouldBeNil)

		customerModel, err := FindCustomerByOldId(accId)
		id := customerModel.ProviderCustomerId

		So(err, ShouldBeNil)
		So(customerModel, ShouldNotBeNil)

		Convey("Then it should save customer", func() {
			So(checkCustomerIsSaved(accId), ShouldBeTrue)
		})

		Convey("Then it should create an customer in Stripe", func() {
			So(checkCustomerExistsInStripe(id), ShouldBeTrue)
		})

		Convey("Then it should subscribe user to plan", func() {
			customer, err := GetCustomerFromStripe(id)
			So(err, ShouldBeNil)

			So(customer.Subs.Count, ShouldEqual, 1)
		})

		Convey("Then customer can't subscribe to same plan again", func() {
			err = Subscribe(token, accId, email, "hobbyist_month")
			So(err, ShouldEqual, ErrCustomerAlreadySubscribedToPlan)
		})
	})

	Convey("Given existent customer, plan", t, func() {
		token, accId, email := generateFakeUserInfo()

		_, err := CreateCustomer(token, accId, email)
		So(err, ShouldBeNil)

		err = Subscribe(token, accId, email, "hobbyist_month")
		So(err, ShouldBeNil)

		customerModel, err := FindCustomerByOldId(accId)
		id := customerModel.ProviderCustomerId

		Convey("Then it should subscribe user to plan", func() {
			customer, err := GetCustomerFromStripe(id)
			So(err, ShouldBeNil)

			So(customer.Subs.Count, ShouldEqual, 1)
		})
	})

	Convey("Given customer already subscribed to a plan", t, func() {
		token, accId, email := generateFakeUserInfo()
		planId := "hobbyist_month"

		_, err := CreateCustomer(token, accId, email)
		So(err, ShouldBeNil)

		err = Subscribe(token, accId, email, planId)
		So(err, ShouldBeNil)

		customer, err := FindCustomerByOldId(accId)
		So(err, ShouldBeNil)

		customerId := customer.ProviderCustomerId

		subs, err := FindCustomerActiveSubscriptions(customer)
		So(err, ShouldBeNil)

		So(len(subs), ShouldEqual, 1)

		currentSub := subs[0]
		subId := currentSub.ProviderSubscriptionId

		Convey("Then customer can't subscribe to same plan again", func() {
			err = Subscribe(token, accId, email, planId)
			So(err, ShouldEqual, ErrCustomerAlreadySubscribedToPlan)
		})

		Convey("When customer upgrades to higher plan", func() {
			newPlanId := "developer_month"

			err = Subscribe(token, accId, email, newPlanId)
			So(err, ShouldBeNil)

			Convey("Then subscription is updated on stripe", func() {
				subParams := &stripe.SubParams{Customer: customerId}
				sub, err := stripeSub.Get(subId, subParams)

				So(err, ShouldBeNil)

				So(sub.Plan.Id, ShouldEqual, newPlanId)
			})

			Convey("Then subscription is saved", func() {
				subs, err := FindCustomerActiveSubscriptions(customer)
				So(err, ShouldBeNil)

				So(len(subs), ShouldEqual, 1)

				currentSub := subs[0]
				newPlan, err := FindPlanByTitle(newPlanId)

				So(err, ShouldBeNil)
				So(currentSub.PlanId, ShouldEqual, newPlan.Id)
			})
		})
	})
}<commit_msg>payment: add tests for subscribe upgrade, downgrade<commit_after>package stripe

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"

	stripe "github.com/stripe/stripe-go"
	stripeSub "github.com/stripe/stripe-go/sub"
)

func TestSubscribe(t *testing.T) {
	Convey("Given nonexistent customer, plan", t, func() {
		token, accId, email := generateFakeUserInfo()
		err := Subscribe(token, accId, email, "hobbyist_month")

		So(err, ShouldBeNil)

		customerModel, err := FindCustomerByOldId(accId)
		id := customerModel.ProviderCustomerId

		So(err, ShouldBeNil)
		So(customerModel, ShouldNotBeNil)

		Convey("Then it should save customer", func() {
			So(checkCustomerIsSaved(accId), ShouldBeTrue)
		})

		Convey("Then it should create an customer in Stripe", func() {
			So(checkCustomerExistsInStripe(id), ShouldBeTrue)
		})

		Convey("Then it should subscribe user to plan", func() {
			customer, err := GetCustomerFromStripe(id)
			So(err, ShouldBeNil)

			So(customer.Subs.Count, ShouldEqual, 1)
		})

		Convey("Then customer can't subscribe to same plan again", func() {
			err = Subscribe(token, accId, email, "hobbyist_month")
			So(err, ShouldEqual, ErrCustomerAlreadySubscribedToPlan)
		})
	})

	Convey("Given existent customer, plan", t, func() {
		token, accId, email := generateFakeUserInfo()

		_, err := CreateCustomer(token, accId, email)
		So(err, ShouldBeNil)

		err = Subscribe(token, accId, email, "hobbyist_month")
		So(err, ShouldBeNil)

		customerModel, err := FindCustomerByOldId(accId)
		id := customerModel.ProviderCustomerId

		Convey("Then it should subscribe user to plan", func() {
			customer, err := GetCustomerFromStripe(id)
			So(err, ShouldBeNil)

			So(customer.Subs.Count, ShouldEqual, 1)
		})
	})

	Convey("Given customer already subscribed to a plan", t, func() {
		token, accId, email := generateFakeUserInfo()
		planId := "hobbyist_month"

		_, err := CreateCustomer(token, accId, email)
		So(err, ShouldBeNil)

		err = Subscribe(token, accId, email, planId)
		So(err, ShouldBeNil)

		customer, err := FindCustomerByOldId(accId)
		So(err, ShouldBeNil)

		customerId := customer.ProviderCustomerId

		subs, err := FindCustomerActiveSubscriptions(customer)
		So(err, ShouldBeNil)

		So(len(subs), ShouldEqual, 1)

		currentSub := subs[0]
		subId := currentSub.ProviderSubscriptionId

		Convey("Then customer can't subscribe to same plan again", func() {
			err = Subscribe(token, accId, email, planId)
			So(err, ShouldEqual, ErrCustomerAlreadySubscribedToPlan)
		})

		Convey("When customer upgrades to higher plan", func() {
			newPlanId := "developer_month"

			err = Subscribe(token, accId, email, newPlanId)
			So(err, ShouldBeNil)

			Convey("Then subscription is updated on stripe", func() {
				subParams := &stripe.SubParams{Customer: customerId}
				sub, err := stripeSub.Get(subId, subParams)

				So(err, ShouldBeNil)

				So(sub.Plan.Id, ShouldEqual, newPlanId)
			})

			Convey("Then subscription is saved", func() {
				subs, err := FindCustomerActiveSubscriptions(customer)
				So(err, ShouldBeNil)

				So(len(subs), ShouldEqual, 1)

				currentSub := subs[0]
				newPlan, err := FindPlanByTitle(newPlanId)

				So(err, ShouldBeNil)
				So(currentSub.PlanId, ShouldEqual, newPlan.Id)
			})
		})
	})

	Convey("Given customer already subscribed to a plan", t, func() {
		token, accId, email := generateFakeUserInfo()
		planId := "professional_month"

		_, err := CreateCustomer(token, accId, email)
		So(err, ShouldBeNil)

		err = Subscribe(token, accId, email, planId)
		So(err, ShouldBeNil)

		customer, err := FindCustomerByOldId(accId)
		So(err, ShouldBeNil)

		customerId := customer.ProviderCustomerId

		subs, err := FindCustomerActiveSubscriptions(customer)
		So(err, ShouldBeNil)

		So(len(subs), ShouldEqual, 1)

		currentSub := subs[0]
		subId := currentSub.ProviderSubscriptionId

		Convey("When customer downgrades to lower plan", func() {
			newPlanId := "hobbyist_month"

			err = Subscribe(token, accId, email, newPlanId)
			So(err, ShouldBeNil)

			Convey("Then subscription is updated on stripe", func() {
				subParams := &stripe.SubParams{Customer: customerId}
				sub, err := stripeSub.Get(subId, subParams)

				So(err, ShouldBeNil)

				So(sub.Plan.Id, ShouldEqual, newPlanId)
			})

			Convey("Then subscription is saved", func() {
				subs, err := FindCustomerActiveSubscriptions(customer)
				So(err, ShouldBeNil)

				So(len(subs), ShouldEqual, 1)

				currentSub := subs[0]
				newPlan, err := FindPlanByTitle(newPlanId)

				So(err, ShouldBeNil)
				So(currentSub.PlanId, ShouldEqual, newPlan.Id)
			})
		})
	})
}
{'fraction_non_alphanumeric': 0.12455334354262379, 'fraction_numerical': 0.002041858090862685, 'mean_word_length': 4.554216867469879, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3432567', 'n_tokens_mistral': 3011, 'n_tokens_neox': 2584, 'n_words': 623}