@@ -15,19 +15,23 @@ import (
1515
1616func TestConnectHandler_CheckFeatureEntitlement (t * testing.T ) {
1717 tests := []struct {
18- name string
19- setup func (es * mocks.EntitlementService )
20- request * connect.Request [frontierv1beta1.CheckFeatureEntitlementRequest ]
21- want * connect.Response [frontierv1beta1.CheckFeatureEntitlementResponse ]
22- wantErr error
23- errCode connect.Code
18+ name string
19+ customerSetup func (cs * mocks.CustomerService )
20+ setup func (es * mocks.EntitlementService )
21+ request * connect.Request [frontierv1beta1.CheckFeatureEntitlementRequest ]
22+ want * connect.Response [frontierv1beta1.CheckFeatureEntitlementResponse ]
23+ wantErr error
24+ errCode connect.Code
2425 }{
2526 {
2627 name : "should return internal server error when entitlement service returns error" ,
2728 request : connect .NewRequest (& frontierv1beta1.CheckFeatureEntitlementRequest {
28- BillingId : "billing -123" ,
29- Feature : "feature-abc" ,
29+ OrgId : "org -123" ,
30+ Feature : "feature-abc" ,
3031 }),
32+ customerSetup : func (cs * mocks.CustomerService ) {
33+ cs .EXPECT ().GetByOrgID (mock .Anything , "org-123" ).Return (customer.Customer {ID : "billing-123" }, nil )
34+ },
3135 setup : func (es * mocks.EntitlementService ) {
3236 es .EXPECT ().Check (mock .Anything , "billing-123" , "feature-abc" ).Return (false , errors .New ("service error" ))
3337 },
@@ -37,13 +41,16 @@ func TestConnectHandler_CheckFeatureEntitlement(t *testing.T) {
3741 },
3842 {
3943 name : "should return false when feature is not entitled" ,
44+ request : connect .NewRequest (& frontierv1beta1.CheckFeatureEntitlementRequest {
45+ OrgId : "org-123" ,
46+ Feature : "feature-abc" ,
47+ }),
48+ customerSetup : func (cs * mocks.CustomerService ) {
49+ cs .EXPECT ().GetByOrgID (mock .Anything , "org-123" ).Return (customer.Customer {ID : "billing-123" }, nil )
50+ },
4051 setup : func (es * mocks.EntitlementService ) {
4152 es .EXPECT ().Check (mock .Anything , "billing-123" , "feature-abc" ).Return (false , nil )
4253 },
43- request : connect .NewRequest (& frontierv1beta1.CheckFeatureEntitlementRequest {
44- BillingId : "billing-123" ,
45- Feature : "feature-abc" ,
46- }),
4754 want : connect .NewResponse (& frontierv1beta1.CheckFeatureEntitlementResponse {
4855 Status : false ,
4956 }),
@@ -52,57 +59,63 @@ func TestConnectHandler_CheckFeatureEntitlement(t *testing.T) {
5259 },
5360 {
5461 name : "should return true when feature is entitled" ,
62+ request : connect .NewRequest (& frontierv1beta1.CheckFeatureEntitlementRequest {
63+ OrgId : "org-123" ,
64+ Feature : "feature-abc" ,
65+ }),
66+ customerSetup : func (cs * mocks.CustomerService ) {
67+ cs .EXPECT ().GetByOrgID (mock .Anything , "org-123" ).Return (customer.Customer {ID : "billing-123" }, nil )
68+ },
5569 setup : func (es * mocks.EntitlementService ) {
5670 es .EXPECT ().Check (mock .Anything , "billing-123" , "feature-abc" ).Return (true , nil )
5771 },
58- request : connect .NewRequest (& frontierv1beta1.CheckFeatureEntitlementRequest {
59- BillingId : "billing-123" ,
60- Feature : "feature-abc" ,
61- }),
6272 want : connect .NewResponse (& frontierv1beta1.CheckFeatureEntitlementResponse {
6373 Status : true ,
6474 }),
6575 wantErr : nil ,
6676 errCode : connect .Code (0 ),
6777 },
6878 {
69- name : "should handle empty billing id" ,
70- setup : func (es * mocks.EntitlementService ) {
71- es .EXPECT ().Check (mock .Anything , "" , "feature-abc" ).Return (false , nil )
72- },
79+ name : "should return empty response when billing account not found" ,
7380 request : connect .NewRequest (& frontierv1beta1.CheckFeatureEntitlementRequest {
74- BillingId : "" ,
75- Feature : "feature-abc" ,
76- }),
77- want : connect .NewResponse (& frontierv1beta1.CheckFeatureEntitlementResponse {
78- Status : false ,
81+ OrgId : "org-123" ,
82+ Feature : "feature-abc" ,
7983 }),
84+ customerSetup : func (cs * mocks.CustomerService ) {
85+ cs .EXPECT ().GetByOrgID (mock .Anything , "org-123" ).Return (customer.Customer {}, customer .ErrNotFound )
86+ },
87+ setup : func (es * mocks.EntitlementService ) {},
88+ want : connect .NewResponse (& frontierv1beta1.CheckFeatureEntitlementResponse {}),
8089 wantErr : nil ,
8190 errCode : connect .Code (0 ),
8291 },
8392 {
84- name : "should handle empty feature" ,
85- setup : func (es * mocks.EntitlementService ) {
86- es .EXPECT ().Check (mock .Anything , "billing-123" , "" ).Return (false , nil )
87- },
93+ name : "should return invalid argument when org_id is invalid" ,
8894 request : connect .NewRequest (& frontierv1beta1.CheckFeatureEntitlementRequest {
89- BillingId : "billing-123 " ,
90- Feature : " " ,
95+ OrgId : " " ,
96+ Feature : "feature-abc " ,
9197 }),
92- want : connect .NewResponse (& frontierv1beta1.CheckFeatureEntitlementResponse {
93- Status : false ,
94- }),
95- wantErr : nil ,
96- errCode : connect .Code (0 ),
98+ customerSetup : func (cs * mocks.CustomerService ) {
99+ cs .EXPECT ().GetByOrgID (mock .Anything , "" ).Return (customer.Customer {}, customer .ErrInvalidUUID )
100+ },
101+ setup : func (es * mocks.EntitlementService ) {},
102+ want : nil ,
103+ wantErr : customer .ErrInvalidUUID ,
104+ errCode : connect .CodeInvalidArgument ,
97105 },
98106 }
99107 for _ , tt := range tests {
100108 t .Run (tt .name , func (t * testing.T ) {
109+ mockCustomerSvc := new (mocks.CustomerService )
101110 mockEntitlementSvc := new (mocks.EntitlementService )
111+ if tt .customerSetup != nil {
112+ tt .customerSetup (mockCustomerSvc )
113+ }
102114 if tt .setup != nil {
103115 tt .setup (mockEntitlementSvc )
104116 }
105117 h := & ConnectHandler {
118+ customerService : mockCustomerSvc ,
106119 entitlementService : mockEntitlementSvc ,
107120 }
108121 got , err := h .CheckFeatureEntitlement (context .Background (), tt .request )
0 commit comments