Skip to content

Commit 2115fd4

Browse files
geroplroboquat
authored andcommitted
[server] Fix BillingMode tests
1 parent ff707d2 commit 2115fd4

File tree

1 file changed

+78
-17
lines changed

1 file changed

+78
-17
lines changed

components/server/ee/src/billing/billing-mode.spec.db.ts

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,91 @@ import { Container, ContainerModule } from "inversify";
3838
import { suite, test, timeout } from "mocha-typescript";
3939
import Stripe from "stripe";
4040
import { Config } from "../../../src/config";
41-
import { StripeService } from "../user/stripe-service";
4241
import { BillingModes, BillingModesImpl } from "./billing-mode";
4342
import * as deepEqualInAnyOrder from "deep-equal-in-any-order";
43+
import {
44+
UsageServiceDefinition,
45+
UsageServiceClient,
46+
GetCostCenterResponse,
47+
CostCenter_BillingStrategy,
48+
GetBalanceResponse,
49+
SetCostCenterResponse,
50+
ReconcileUsageResponse,
51+
ListUsageRequest_Ordering,
52+
ListUsageResponse,
53+
} from "@gitpod/usage-api/lib/usage/v1/usage.pb";
54+
import { CallOptions } from "nice-grpc-common";
4455
chai.use(deepEqualInAnyOrder);
4556
const expect = chai.expect;
4657

4758
type StripeSubscription = Pick<Stripe.Subscription, "id"> & { customer: string };
48-
class StripeServiceMock extends StripeService {
49-
constructor(protected readonly subscription?: StripeSubscription) {
50-
super();
51-
}
59+
class UsageServiceClientMock implements UsageServiceClient {
60+
constructor(protected readonly subscription?: StripeSubscription) {}
5261

53-
async findUncancelledSubscriptionByAttributionId(attributionId: string): Promise<string | undefined> {
54-
const customerId = await this.findCustomerByAttributionId(attributionId);
55-
if (!!customerId && this.subscription?.customer === customerId) {
56-
return this.subscription.id;
62+
async getCostCenter(
63+
request: { attributionId?: string | undefined },
64+
options?: CallOptions | undefined,
65+
): Promise<GetCostCenterResponse> {
66+
if (!request.attributionId) {
67+
return { costCenter: undefined };
5768
}
58-
return undefined;
59-
}
6069

61-
async findCustomerByAttributionId(attributionId: string): Promise<string | undefined> {
62-
const customerId = this.subscription?.customer;
63-
if (!customerId) {
64-
return undefined;
70+
let billingStrategy = CostCenter_BillingStrategy.BILLING_STRATEGY_OTHER;
71+
if (this.subscription !== undefined) {
72+
billingStrategy = CostCenter_BillingStrategy.BILLING_STRATEGY_STRIPE;
6573
}
66-
return customerId;
74+
75+
return {
76+
costCenter: {
77+
attributionId: request.attributionId,
78+
billingStrategy,
79+
nextBillingTime: new Date(), // does not matter here.
80+
spendingLimit: 1234,
81+
},
82+
};
83+
}
84+
85+
async getBalance(
86+
request: { attributionId?: string | undefined },
87+
options?: CallOptions | undefined,
88+
): Promise<GetBalanceResponse> {
89+
throw new Error("Mock: not implemented");
90+
}
91+
92+
async setCostCenter(
93+
request: {
94+
costCenter?:
95+
| {
96+
attributionId?: string | undefined;
97+
spendingLimit?: number | undefined;
98+
billingStrategy?: CostCenter_BillingStrategy | undefined;
99+
nextBillingTime?: Date | undefined;
100+
}
101+
| undefined;
102+
},
103+
options?: CallOptions | undefined,
104+
): Promise<SetCostCenterResponse> {
105+
throw new Error("Mock: not implemented");
106+
}
107+
108+
async reconcileUsage(
109+
request: { from?: Date | undefined; to?: Date | undefined },
110+
options?: CallOptions | undefined,
111+
): Promise<ReconcileUsageResponse> {
112+
throw new Error("Mock: not implemented");
113+
}
114+
115+
async listUsage(
116+
request: {
117+
attributionId?: string | undefined;
118+
from?: Date | undefined;
119+
to?: Date | undefined;
120+
order?: ListUsageRequest_Ordering | undefined;
121+
pagination?: { perPage?: number | undefined; page?: number | undefined } | undefined;
122+
},
123+
options?: CallOptions | undefined,
124+
): Promise<ListUsageResponse> {
125+
throw new Error("Mock: not implemented");
67126
}
68127
}
69128

@@ -486,7 +545,9 @@ class BillingModeSpec {
486545
bind(SubscriptionService).toSelf().inSingletonScope();
487546
bind(BillingModes).to(BillingModesImpl).inSingletonScope();
488547

489-
bind(StripeService).toConstantValue(new StripeServiceMock(test.config.stripeSubscription));
548+
bind(UsageServiceDefinition.name).toConstantValue(
549+
new UsageServiceClientMock(test.config.stripeSubscription),
550+
);
490551
bind(ConfigCatClientFactory).toConstantValue(
491552
() =>
492553
new ConfigCatClientMock({

0 commit comments

Comments
 (0)