Path: blob/main/components/gitpod-db/go/stripe_customer_test.go
2497 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package db_test56import (7"context"8"testing"9"time"1011db "github.com/gitpod-io/gitpod/components/gitpod-db/go"1213"github.com/gitpod-io/gitpod/components/gitpod-db/go/dbtest"14"github.com/google/uuid"15"github.com/stretchr/testify/require"16)1718func TestCreateStripeCustomer(t *testing.T) {19conn := dbtest.ConnectForTests(t)2021customer := db.StripeCustomer{22StripeCustomerID: "cus_1234",23AttributionID: db.NewTeamAttributionID(uuid.New().String()),24CreationTime: db.NewVarCharTime(time.Now()),25}26t.Cleanup(func() {27require.NoError(t, conn.Delete(&customer).Error)28})2930require.NoError(t, db.CreateStripeCustomer(context.Background(), conn, customer))3132// second create should fail due to PK contstraint33require.Error(t, db.CreateStripeCustomer(context.Background(), conn, customer))34}3536func TestGetStripeCustomer(t *testing.T) {37conn := dbtest.ConnectForTests(t)3839customers := dbtest.CreateStripeCustomers(t, conn, dbtest.NewStripeCustomer(t, db.StripeCustomer{}))40customer := customers[0]4142retrieved, err := db.GetStripeCustomer(context.Background(), conn, customer.StripeCustomerID)43require.NoError(t, err)44require.Equal(t, customer.StripeCustomerID, retrieved.StripeCustomerID)45require.Equal(t, customer.AttributionID, retrieved.AttributionID)46}4748func TestGetStripeCustomer_NotFound_WhenNotExists(t *testing.T) {49conn := dbtest.ConnectForTests(t)5051_, err := db.GetStripeCustomer(context.Background(), conn, "cus_12314141")52require.Error(t, err)53require.ErrorIs(t, err, db.ErrorNotFound)54}5556func TestGetStripeCustomerByAttributionID_ReturnsLatestRecord(t *testing.T) {57conn := dbtest.ConnectForTests(t)58now := time.Now()5960attributionID := db.NewTeamAttributionID(uuid.New().String())61first := dbtest.NewStripeCustomer(t, db.StripeCustomer{62AttributionID: attributionID,63CreationTime: db.NewVarCharTime(now.Add(-1 * time.Hour)),64})65second := dbtest.NewStripeCustomer(t, db.StripeCustomer{66AttributionID: attributionID,67CreationTime: db.NewVarCharTime(now),68})69dbtest.CreateStripeCustomers(t, conn, first, second)7071retrieved, err := db.GetStripeCustomerByAttributionID(context.Background(), conn, attributionID)72require.NoError(t, err)73require.Equal(t, second.StripeCustomerID, retrieved.StripeCustomerID)74require.Equal(t, second.AttributionID, retrieved.AttributionID)75}7677func TestGetStripeCustomerByAttributionID_NotFound_WhenNotExists(t *testing.T) {78conn := dbtest.ConnectForTests(t)7980attributionID := db.NewTeamAttributionID(uuid.New().String())8182_, err := db.GetStripeCustomerByAttributionID(context.Background(), conn, attributionID)83require.Error(t, err)84require.ErrorIs(t, err, db.ErrorNotFound)85}868788