Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch teams query to nais-api spec #151

Merged
merged 4 commits into from
Feb 23, 2024
Merged
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
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