Skip to content

Commit

Permalink
Switch teams query to nais-api spec (#151)
Browse files Browse the repository at this point in the history
Co-authored-by: Frode Sundby <[email protected]>
Co-authored-by: Johnny Horvi <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2024
1 parent d96e3be commit 43cfceb
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 59 deletions.
65 changes: 38 additions & 27 deletions src/nais-teams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ describe("NaisTeams", () => {
const expectedUser: User = {
name: "John Doe",
email: "[email protected]",
teams: [
{
role: "admin",
team: {
slug: "team-a",
teams: {
nodes: [
{
role: "admin",
team: {
slug: "team-a",
},
},
],
pageInfo: {
hasNextPage: false,
},
],
},
};
const mockResponse = {
data: {
userByEmail: expectedUser,
user: expectedUser,
},
};
jest.spyOn(global, "fetch").mockResolvedValueOnce({
Expand Down Expand Up @@ -52,7 +57,7 @@ describe("NaisTeams", () => {
const email = "[email protected]";
const mockResponse = {
data: {
userByEmail: null,
user: null,
},
};
jest.spyOn(global, "fetch").mockResolvedValueOnce({
Expand Down Expand Up @@ -99,21 +104,24 @@ describe("NaisTeams", () => {
describe("authorize", () => {
it("should return status true and user object when user is authorized", async () => {
const email = "[email protected]";
const expectedUser = {
const expectedUser: User = {
name: "John Doe",
email: "[email protected]",
teams: [
{
role: "admin",
team: {
slug: "team-a",
teams: {
nodes: [
{
role: "admin",
team: {
slug: "team-a",
},
},
},
],
],
pageInfo: { hasNextPage: false },
},
};
const mockResponse = {
data: {
userByEmail: expectedUser,
user: expectedUser,
},
};
jest.spyOn(global, "fetch").mockResolvedValueOnce({
Expand Down Expand Up @@ -142,7 +150,7 @@ describe("NaisTeams", () => {
const email = "[email protected]";
const mockResponse = {
data: {
userByEmail: null,
user: null,
},
};
jest.spyOn(global, "fetch").mockResolvedValueOnce({
Expand All @@ -169,21 +177,24 @@ describe("NaisTeams", () => {

it("should return status false and null user when user is not in allowed teams", async () => {
const email = "[email protected]";
const user = {
const user: User = {
name: "John Doe",
email: "[email protected]",
teams: [
{
role: "admin",
team: {
slug: "team-c",
teams: {
nodes: [
{
role: "admin",
team: {
slug: "team-c",
},
},
},
],
],
pageInfo: { hasNextPage: false },
},
};
const mockResponse = {
data: {
userByEmail: user,
user: user,
},
};
jest.spyOn(global, "fetch").mockResolvedValueOnce({
Expand Down
46 changes: 32 additions & 14 deletions src/nais-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,31 @@ export type User = {
name: string;
email: string;
teams: {
role: string;
team: {
slug: string;
nodes: {
role: string;
team: {
slug: string;
};
}[];
pageInfo: {
hasNextPage: boolean;
};
}[];
};
};

export const lookupUserQuery = `query LookupUser($email: String!) {
userByEmail(email:$email) {
name,
email,
teams {
role,
team {
slug,
user(email: $email) {
name
email
teams(limit: 100) {
nodes {
role
team {
slug
}
}
pageInfo {
hasNextPage
}
}
}
Expand Down Expand Up @@ -84,17 +94,23 @@ export class NaisTeams {
logger.debug("lookupUser: response status", response.status);
logger.debug("lookupUser: response headers", response.headers);

const json: any = await response.json();
const json: { data: { user: User }; errors: any[] } = await response.json();

if (json.errors) {
logger.error("lookupUser: json errors", json.errors);
throw new Error(json.errors[0].message);
}

if (json.data.user?.teams.pageInfo.hasNextPage) {
logger.error(
"lookupUser: user has more than 100 teams, pagination not implemented",
);
}

logger.info("lookupUser: user found");
logger.debug("lookupUser: response", json);

return json.data.userByEmail;
return json.data.user;
};

/**
Expand Down Expand Up @@ -129,7 +145,9 @@ export class NaisTeams {
}

logger.info("authorize: user found", userByEmail);
const { teams } = userByEmail;
const {
teams: { nodes: teams },
} = userByEmail;

if (!teams) {
logger.warn("authorize: user has no teams", userByEmail);
Expand Down
46 changes: 28 additions & 18 deletions src/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { KeyObject } from "crypto";
import { TeamsService, User } from "nais-teams";
import nock from "nock";
import request from "supertest";
import { IUnleash } from "unleash-server";
import naisleash from "./server";
import Cache from "./cache";
import request from "supertest";
import { IAP_AUDIENCE, IAP_JWT_HEADER, IAP_JWT_ISSUER } from "./google-iap";
import naisleash from "./server";
import { newSignedToken } from "./utils";
import nock from "nock";
import { KeyObject } from "crypto";
import { IAP_JWT_HEADER, IAP_JWT_ISSUER, IAP_AUDIENCE } from "./google-iap";
import { TeamsService, User } from "nais-teams";

let mockTeamsService: TeamsService;
let server: IUnleash;
Expand Down Expand Up @@ -130,14 +130,19 @@ describe("Unleash server", () => {
const mockUser: User = {
name: "test",
email: "[email protected]",
teams: [
{
role: "admin",
team: {
slug: "team",
teams: {
nodes: [
{
role: "admin",
team: {
slug: "team",
},
},
],
pageInfo: {
hasNextPage: false,
},
],
},
};

jest.spyOn(mockTeamsService, "authorize").mockResolvedValueOnce({
Expand Down Expand Up @@ -172,14 +177,19 @@ describe("Unleash server", () => {
const mockUser: User = {
name: "test",
email: "[email protected]",
teams: [
{
role: "member",
team: {
slug: "team",
teams: {
nodes: [
{
role: "member",
team: {
slug: "team",
},
},
],
pageInfo: {
hasNextPage: false,
},
],
},
};

jest.spyOn(mockTeamsService, "authorize").mockResolvedValueOnce({
Expand Down

0 comments on commit 43cfceb

Please sign in to comment.