From ddff5f5baf94af9b8027c30896ba049ebf7e25ec Mon Sep 17 00:00:00 2001 From: Jigar-f Date: Fri, 26 Apr 2024 16:16:09 +0530 Subject: [PATCH 1/2] Use gock to mock payment request --- go.mod | 6 ++++-- go.sum | 10 ++++++++-- pro/client/client_test.go | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index bbd64f08b..9c147dc62 100644 --- a/go.mod +++ b/go.mod @@ -58,7 +58,6 @@ require ( github.com/getlantern/jibber_jabber v0.0.0-20210901195950-68955124cc42 github.com/getlantern/kcpwrapper v0.0.0-20230327091313-c12d7c17c6de github.com/getlantern/keyman v0.0.0-20230503155501-4e864ca2175b - github.com/getlantern/lampshade v0.0.0-20201109225444-b06082e15f3a github.com/getlantern/lantern-algeneva v0.0.0-20240402195540-eb1bbf6d7366 github.com/getlantern/mockconn v0.0.0-20200818071412-cb30d065a848 github.com/getlantern/mtime v0.0.0-20200417132445-23682092d1f7 @@ -87,6 +86,7 @@ require ( github.com/getsentry/sentry-go v0.20.0 github.com/golang/protobuf v1.5.3 github.com/google/gopacket v1.1.19 + github.com/h2non/gock v1.2.0 github.com/hashicorp/golang-lru v0.5.4 github.com/jaffee/commandeer v0.6.0 github.com/keighl/mandrill v0.0.0-20170605120353-1775dd4b3b41 @@ -97,7 +97,7 @@ require ( github.com/refraction-networking/utls v1.3.3 github.com/samber/lo v1.38.1 github.com/shadowsocks/go-shadowsocks2 v0.1.5 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 github.com/vulcand/oxy v1.4.2 github.com/xtaci/smux v1.5.24 go.opentelemetry.io/otel v1.19.0 @@ -117,7 +117,9 @@ require ( git.torproject.org/pluggable-transports/goptlib.git v1.2.0 // indirect github.com/dchest/siphash v1.2.3 // indirect github.com/getlantern/algeneva v0.0.0-20240222191137-2b4e88234f59 // indirect + github.com/getlantern/lampshade v0.0.0-20201109225444-b06082e15f3a // indirect github.com/getlantern/withtimeout v0.0.0-20160829163843-511f017cd913 // indirect + github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect gitlab.com/yawning/edwards25519-extra.git v0.0.0-20211229043746-2f91fcc9fbdb // indirect gitlab.com/yawning/obfs4.git v0.0.0-20220204003609-77af0cba934d // indirect ) diff --git a/go.sum b/go.sum index c00291963..7b9f90fdf 100644 --- a/go.sum +++ b/go.sum @@ -516,6 +516,10 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms= github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg= +github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE= +github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= @@ -611,6 +615,8 @@ github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM= github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= github.com/nwaples/rardecode v1.1.2 h1:Cj0yZY6T1Zx1R7AhTbyGSALm44/Mmq+BAPc4B/p/d3M= github.com/nwaples/rardecode v1.1.2/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= @@ -809,8 +815,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/templexxx/cpu v0.0.1/go.mod h1:w7Tb+7qgcAlIyX4NhLuDKt78AHA5SzPmq0Wj6HiEnnk= github.com/templexxx/cpu v0.0.8 h1:va6GebSxedVdR5XEyPJD49t94p5ZsjWO6Wh/PfbmZnc= github.com/templexxx/cpu v0.0.8/go.mod h1:w7Tb+7qgcAlIyX4NhLuDKt78AHA5SzPmq0Wj6HiEnnk= diff --git a/pro/client/client_test.go b/pro/client/client_test.go index dfb29e391..14d52b1c9 100644 --- a/pro/client/client_test.go +++ b/pro/client/client_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/h2non/gock" "github.com/pborman/uuid" "github.com/stretchr/testify/assert" @@ -88,6 +89,9 @@ func TestPaymentMethodsV4(t *testing.T) { user.UserID = res.User.ID user.Token = res.User.Token + defer gock.Off() + mockGetRequest("/plans-v4", expectedPlansV4Response) + paymentResp, err := createClient().PaymentMethodsV4(user) if !assert.NoError(t, err) { return @@ -96,6 +100,7 @@ func TestPaymentMethodsV4(t *testing.T) { assert.True(t, len(paymentResp.PaymentMethodsResponse.Icons) > 0) assert.True(t, len(paymentResp.PaymentMethodsResponse.Plans) > 0) assert.True(t, len(paymentResp.PaymentMethodsResponse.Providers) > 0) + assert.True(t, gock.IsDone()) } func TestUserDataMissing(t *testing.T) { @@ -156,3 +161,16 @@ func TestCreateUniqueUsers(t *testing.T) { assert.NotEqual(t, userA.UserID, userB.UserID) assert.NotEqual(t, userA.Token, userB.Token) } + +// this method is used to mock the get request from the server +func mockGetRequest(path string, responseBody interface{}) { + gock.New("https://" + common.ProAPIHost).Get(path).Reply(200).JSON(responseBody) +} + +// this method is used to mock the post request from the server +func mockPostRequest(path string, responseBody interface{}) { + gock.New("https://" + common.ProAPIHost).Post(path).Reply(200).JSON(responseBody) +} + +// / Since now we stared using mock server, we need to update the expected response +const expectedPlansV4Response = `{"icons":{"paymentwall":["https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/5fa28e16-5b78-44b1-f02b-2b80593b5c00/public"],"stripe":["https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/e02a8c29-20a6-478f-ed01-e93d0dba0800/public","https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/94870838-0129-417c-d65a-276422eac900/public","https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/f56ce1a8-96d6-4528-fdc6-da4605f1c500/public"]},"plans":[{"id":"1y-usd-10","description":"One Year Plan","duration":{"days":0,"months":0,"years":1},"price":{"usd":4800},"expectedMonthlyPrice":{"usd":400},"usdPrice":4800,"usdPrice1Y":4800,"usdPrice2Y":8700,"redeemFor":{"days":0,"months":2},"renewalBonus":{"days":0,"months":1},"renewalBonusExpired":{"days":15,"months":0},"renewalBonusExpected":{"days":0,"months":0},"discount":0,"bestValue":false,"level":"pro"},{"id":"2y-usd-10","description":"Two Year Plan","duration":{"days":0,"months":0,"years":2},"price":{"usd":8700},"expectedMonthlyPrice":{"usd":363},"usdPrice":8700,"usdPrice1Y":4800,"usdPrice2Y":8700,"redeemFor":{"days":0,"months":2},"renewalBonus":{"days":0,"months":3},"renewalBonusExpired":{"days":15,"months":1},"renewalBonusExpected":{"days":0,"months":0},"discount":0.0925,"bestValue":true,"level":"pro"}],"providers":{"android":[{"method":"credit-card","providers":[{"name":"stripe","data":{"pubKey":"pk_live_4MSPfR6qNHMwjG86TZJv4NI0"}}]}],"desktop":[{"method":"credit-card","providers":[{"name":"stripe","data":{"pubKey":"pk_live_4MSPfR6qNHMwjG86TZJv4NI0"}}]},{"method":"paymentwall","providers":[{"name":"paymentwall"}]}]}}` From a80de8567d324731a8d4c548e6ada339cec65f77 Mon Sep 17 00:00:00 2001 From: Jigar-f Date: Fri, 26 Apr 2024 16:19:21 +0530 Subject: [PATCH 2/2] Updated data. --- pro/client/client_test.go | 118 +++++++++++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/pro/client/client_test.go b/pro/client/client_test.go index 14d52b1c9..04fad4a77 100644 --- a/pro/client/client_test.go +++ b/pro/client/client_test.go @@ -173,4 +173,120 @@ func mockPostRequest(path string, responseBody interface{}) { } // / Since now we stared using mock server, we need to update the expected response -const expectedPlansV4Response = `{"icons":{"paymentwall":["https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/5fa28e16-5b78-44b1-f02b-2b80593b5c00/public"],"stripe":["https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/e02a8c29-20a6-478f-ed01-e93d0dba0800/public","https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/94870838-0129-417c-d65a-276422eac900/public","https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/f56ce1a8-96d6-4528-fdc6-da4605f1c500/public"]},"plans":[{"id":"1y-usd-10","description":"One Year Plan","duration":{"days":0,"months":0,"years":1},"price":{"usd":4800},"expectedMonthlyPrice":{"usd":400},"usdPrice":4800,"usdPrice1Y":4800,"usdPrice2Y":8700,"redeemFor":{"days":0,"months":2},"renewalBonus":{"days":0,"months":1},"renewalBonusExpired":{"days":15,"months":0},"renewalBonusExpected":{"days":0,"months":0},"discount":0,"bestValue":false,"level":"pro"},{"id":"2y-usd-10","description":"Two Year Plan","duration":{"days":0,"months":0,"years":2},"price":{"usd":8700},"expectedMonthlyPrice":{"usd":363},"usdPrice":8700,"usdPrice1Y":4800,"usdPrice2Y":8700,"redeemFor":{"days":0,"months":2},"renewalBonus":{"days":0,"months":3},"renewalBonusExpired":{"days":15,"months":1},"renewalBonusExpected":{"days":0,"months":0},"discount":0.0925,"bestValue":true,"level":"pro"}],"providers":{"android":[{"method":"credit-card","providers":[{"name":"stripe","data":{"pubKey":"pk_live_4MSPfR6qNHMwjG86TZJv4NI0"}}]}],"desktop":[{"method":"credit-card","providers":[{"name":"stripe","data":{"pubKey":"pk_live_4MSPfR6qNHMwjG86TZJv4NI0"}}]},{"method":"paymentwall","providers":[{"name":"paymentwall"}]}]}}` +const expectedPlansV4Response = `{ + "icons": { + "paymentwall": [ + "https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/5fa28e16-5b78-44b1-f02b-2b80593b5c00/public" + ], + "stripe": [ + "https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/e02a8c29-20a6-478f-ed01-e93d0dba0800/public", + "https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/94870838-0129-417c-d65a-276422eac900/public", + "https://imagedelivery.net/sB6Q2DOuXQsFvrpwTks_kA/f56ce1a8-96d6-4528-fdc6-da4605f1c500/public" + ] + }, + "plans": [ + { + "id": "1y-usd-10", + "description": "One Year Plan", + "duration": { + "days": 0, + "months": 0, + "years": 1 + }, + "price": { + "usd": 4800 + }, + "expectedMonthlyPrice": { + "usd": 400 + }, + "usdPrice": 4800, + "usdPrice1Y": 4800, + "usdPrice2Y": 8700, + "redeemFor": { + "days": 0, + "months": 2 + }, + "renewalBonus": { + "days": 0, + "months": 1 + }, + "renewalBonusExpired": { + "days": 15, + "months": 0 + }, + "renewalBonusExpected": { + "days": 0, + "months": 0 + }, + "discount": 0, + "bestValue": false, + "level": "pro" + }, + { + "id": "2y-usd-10", + "description": "Two Year Plan", + "duration": { + "days": 0, + "months": 0, + "years": 2 + }, + "price": { + "usd": 8700 + }, + "expectedMonthlyPrice": { + "usd": 363 + }, + "usdPrice": 8700, + "usdPrice1Y": 4800, + "usdPrice2Y": 8700, + "redeemFor": { + "days": 0, + "months": 2 + }, + "renewalBonus": { + "days": 0, + "months": 3 + }, + "renewalBonusExpired": { + "days": 15, + "months": 1 + }, + "renewalBonusExpected": { + "days": 0, + "months": 0 + }, + "discount": 0.0925, + "bestValue": true, + "level": "pro" + } + ], + "providers": { + "android": [ + { + "method": "credit-card", + "providers": [ + { + "name": "stripe", + "data": { + "pubKey": "pk_live_4MSPfR6qNHMwjG86TZJv4NI0" + } + } + ] + } + ], + "desktop": [ + { + "method": "credit-card", + "providers": [] + }, + { + "method": "paymentwall", + "providers": [ + { + "name": "paymentwall" + } + ] + } + ] + } + }`