From 6a36d00171a30fada5c26713671c63ae66bb4efb Mon Sep 17 00:00:00 2001 From: elhmn Date: Sun, 28 May 2023 21:09:27 +0200 Subject: [PATCH] backend: add test to handle payments with opencollective --- backend/e2etests/pay.test.after.js | 46 ++++++++++++++++++++++++ backend/internal/handlers/pay_handler.go | 10 ++++++ 2 files changed, 56 insertions(+) create mode 100644 backend/e2etests/pay.test.after.js diff --git a/backend/e2etests/pay.test.after.js b/backend/e2etests/pay.test.after.js new file mode 100644 index 0000000..9265611 --- /dev/null +++ b/backend/e2etests/pay.test.after.js @@ -0,0 +1,46 @@ +import { expect } from "chai"; +import request from "supertest"; +import dotenv from "dotenv"; + +dotenv.config(); +const apiHost = process.env.API_HOST; +const endpoint = "pay"; + +describe(`${endpoint}`, function () { + describe("POST", function () { + it("fails to make a payment with invalid email address", async function () { + return request(apiHost) + .post(`${endpoint}`) + .set("Accept", "application/json") + .send({ + email: "wrong email", + tier: "new jobsika tier", + job_offer_id: "1", + }) + .expect(400) + .expect("Content-Type", "application/json; charset=utf-8") + .then((res) => { + expect(JSON.stringify(res.body)).contain("email field is invalid"); + }); + }); + + it("proceed with a payment", async function () { + return request(apiHost) + .post(`${endpoint}`) + .set("Accept", "application/json") + .send({ + email: "test@email.com", + tier: "new jobsika tier", + job_offer_id: "1", + }) + .expect(200) + .expect("Content-Type", "application/json; charset=utf-8") + .then((res) => { + expect(JSON.stringify(res.body)).contain( + "opencollective.com" + ); + }); + }); + + }); +}); diff --git a/backend/internal/handlers/pay_handler.go b/backend/internal/handlers/pay_handler.go index ba3f70c..0f896e6 100644 --- a/backend/internal/handlers/pay_handler.go +++ b/backend/internal/handlers/pay_handler.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/http" + "os" "strconv" "strings" "time" @@ -75,6 +76,15 @@ func PostPay(c *gin.Context) { } url := fmt.Sprintf("%s%s-%d", payment.OPEN_COLLECTIVE_CONTRIBUTE, response.CreateTier.Slug, response.CreateTier.LegacyID) + + //We can't proceed with a payment on test mode as this will create unecessary + //payment on opencollective + if os.Getenv("ENVIRONMENT") == "test" { + //Send the link to the newly created opencollective tier to back to the client + c.JSON(http.StatusOK, gin.H{"tier_url": url}) + return + } + err = db.CreatePaymentRecord(&v1beta.PaymentRecord{ TierId: response.CreateTier.ID, JobOfferID: jobOfferID,