Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ language: node_js
node_js:
- 12

services:
- mongodb

deploy:
provider: lambda
function_name: "c2w-lambda-users-$TRAVIS_BRANCH"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"eslint": "^7.5.0",
"husky": "^4.2.5",
"jest": "^26.1.0",
"mongodb-memory-server": "^6.6.1",
"nock": "^13.0.2"
},
"scripts": {
Expand Down
34 changes: 21 additions & 13 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import nock from "nock";
import { MongoMemoryServer } from "mongodb-memory-server";

import { handler } from ".";
import { CLUBS_COLLECTION, USERS_COLLECTION, DB_NAME } from "./config";
Expand All @@ -16,29 +17,21 @@ import {

import { getMongoClient } from "./services/mongo-db";

nock("https://www.strava.com")
.post("/oauth/token")
.query({ refresh_token: mockedRefreshtoken, grant_type: "refresh_token" })
.reply(400, getInvalidToken())
.post("/oauth/token")
.query({ refresh_token: mockedRefreshtoken, grant_type: "refresh_token" })
.reply(200, getValidToken())
.get("/api/v3/athlete/clubs?")
.times(2)
.reply(200, listAthleteClubs());

describe("`Cycle2work auth function`", () => {
let mongoDb;
let client;
let context;
let callback;

beforeAll(async () => {
client = await getMongoClient();
mongoDb = new MongoMemoryServer();
client = await getMongoClient(await mongoDb.getUri());
});

afterAll(async () => {
await client.db(DB_NAME).dropDatabase();
await client.close(true);
await mongoDb.stop();
});

beforeEach(() => {
Expand All @@ -48,11 +41,19 @@ describe("`Cycle2work auth function`", () => {
callback = jest.fn();
});

afterEach(() => {
nock.cleanAll();
});

it("Invalid code provided, do not persist user data", async () => {
nock("https://www.strava.com")
.post("/oauth/token")
.query({ refresh_token: mockedRefreshtoken, grant_type: "refresh_token" })
.reply(400, getInvalidToken());

await handler({ queryStringParameters: { code: mockedRefreshtoken } }, context, callback);

expect(callback).toHaveBeenCalledTimes(1);

const users = await client.db(DB_NAME).collection(USERS_COLLECTION).find({}).toArray();
expect(users).toHaveLength(0);

Expand All @@ -61,6 +62,13 @@ describe("`Cycle2work auth function`", () => {
});

it("Valid code provided, persist user token and clubs", async () => {
nock("https://www.strava.com")
.post("/oauth/token")
.query({ refresh_token: mockedRefreshtoken, grant_type: "refresh_token" })
.reply(200, getValidToken())
.get("/api/v3/athlete/clubs?")
.reply(200, listAthleteClubs());

await handler({ queryStringParameters: { code: mockedRefreshtoken } }, context, callback);

expect(callback).toHaveBeenCalledTimes(1);
Expand Down
8 changes: 4 additions & 4 deletions src/services/mongo-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { MONGODB_URL, USERS_COLLECTION, CLUBS_COLLECTION, DB_NAME } from "../con

let clientInstance;

export async function getMongoClient() {
export async function getMongoClient(url) {
if (!clientInstance) {
/* eslint-disable-next-line */
clientInstance = await mongodb.connect(MONGODB_URL, {
clientInstance = await mongodb.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand All @@ -16,11 +16,11 @@ export async function getMongoClient() {
}

export async function upsertUser(id, user) {
const client = await getMongoClient();
const client = await getMongoClient(MONGODB_URL);
await client.db(DB_NAME).collection(USERS_COLLECTION).updateOne({ id }, { $set: user }, { upsert: true });
}

export async function upsertClub(id, club) {
const client = await getMongoClient();
const client = await getMongoClient(MONGODB_URL);
await client.db(DB_NAME).collection(CLUBS_COLLECTION).updateOne({ id }, { $set: club }, { upsert: true });
}
Loading