Skip to content

Commit

Permalink
tests: add integration tests for review requests and comments (#576)
Browse files Browse the repository at this point in the history
* fix: force tests to run sequentially

* fix: reset the state of the tables before running the tests

* feat: add fixtures used for review requests

* fix: await results from IdentityService

* tests: add partial integration tests for review requests

* chore: switch site name to use repo name instead

* chore: switch to using variable for file name

* chore: standardise the response codes for non-happy paths

* fix: add additional sanity checks for comments-related features

* tests: add remaining integration tests for review requests

* fix: add methods to generate router for default values

* chore: rename test to be more reflective of its purpose

* fix: add missing return and await when updating database

* chore: add comment on the need for Table.sync
  • Loading branch information
dcshzj authored and alexanderleegs committed Mar 8, 2023
1 parent 410f157 commit f4da383
Show file tree
Hide file tree
Showing 10 changed files with 2,354 additions and 19 deletions.
18 changes: 16 additions & 2 deletions src/fixtures/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
mockGithubSessionData,
MOCK_USER_SESSION_DATA_ONE,
} from "./sessionData"
import { MOCK_SITE_NAME_ONE } from "./sites"
import { MOCK_REPO_NAME_ONE } from "./sites"

/**
* @deprecated
Expand Down Expand Up @@ -93,7 +93,7 @@ const attachDefaultUserSessionDataWithSite: RequestHandler<
}
> = attachUserSessionDataWithSite(
MOCK_USER_SESSION_DATA_ONE,
MOCK_SITE_NAME_ONE
MOCK_REPO_NAME_ONE
)

/**
Expand Down Expand Up @@ -140,3 +140,17 @@ export const generateRouterForUserWithSite = (
app.use(router)
return generateFinalRouter(app)
}

export const generateRouterForDefaultUser = (router: Express) => {
const app = express()
app.use(attachDefaultUserSessionData)
app.use(router)
return generateFinalRouter(app)
}

export const generateRouterForDefaultUserWithSite = (router: Express) => {
const app = express()
app.use(attachDefaultUserSessionDataWithSite)
app.use(router)
return generateFinalRouter(app)
}
139 changes: 139 additions & 0 deletions src/fixtures/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { Commit, RawComment, RawFileChangeInfo } from "@root/types/github"

import { MOCK_USER_ID_ONE, MOCK_USER_ID_TWO } from "./users"

export const MOCK_GITHUB_USER_NAME_ONE = "isomergithub1"
export const MOCK_GITHUB_USER_NAME_TWO = "isomergithub2"

export const MOCK_GITHUB_USER_EMAIL_ONE =
"[email protected]"
export const MOCK_GITHUB_USER_EMAIL_TWO =
"[email protected]"

export const MOCK_GITHUB_PULL_REQUEST_NUMBER = 251

// This is one set of commits and file changes which should be used together
export const MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA =
"a15a7c8b23324f680cd7c5011ca763e36d350f41"
export const MOCK_GITHUB_COMMIT_DATE_ONE = "2022-10-12T06:31:05Z"
export const MOCK_GITHUB_COMMIT_DATE_TWO = "2022-10-13T05:39:43Z"
export const MOCK_GITHUB_COMMIT_DATE_THREE = "2022-11-07T16:32:08Z"
export const MOCK_GITHUB_FILENAME_ALPHA_ONE = "index.md"
export const MOCK_GITHUB_FILEPATH_ALPHA_ONE = ""
export const MOCK_GITHUB_FULL_FILEPATH_ALPHA_ONE = encodeURIComponent(
MOCK_GITHUB_FILENAME_ALPHA_ONE
)
export const MOCK_GITHUB_FILENAME_ALPHA_TWO = "Example Title 22.md"
export const MOCK_GITHUB_FILEPATH_ALPHA_TWO = "pages/"
export const MOCK_GITHUB_FULL_FILEPATH_ALPHA_TWO = encodeURIComponent(
MOCK_GITHUB_FILEPATH_ALPHA_TWO + MOCK_GITHUB_FILENAME_ALPHA_TWO
)
export const MOCK_GITHUB_COMMIT_MESSAGE_ALPHA_ONE = `Update file: ${MOCK_GITHUB_FILENAME_ALPHA_ONE}`
export const MOCK_GITHUB_COMMIT_MESSAGE_ALPHA_TWO = `Update file: ${MOCK_GITHUB_FILENAME_ALPHA_TWO}`
export const MOCK_GITHUB_COMMIT_MESSAGE_OBJECT_ALPHA_ONE = {
message: MOCK_GITHUB_COMMIT_MESSAGE_ALPHA_ONE,
fileName: MOCK_GITHUB_FILENAME_ALPHA_ONE,
userId: MOCK_USER_ID_ONE,
}
export const MOCK_GITHUB_COMMIT_MESSAGE_OBJECT_ALPHA_TWO = {
message: MOCK_GITHUB_COMMIT_MESSAGE_ALPHA_TWO,
fileName: MOCK_GITHUB_FILENAME_ALPHA_TWO,
userId: MOCK_USER_ID_ONE,
}
export const MOCK_GITHUB_COMMIT_MESSAGE_OBJECT_ALPHA_THREE = {
message: MOCK_GITHUB_COMMIT_MESSAGE_ALPHA_TWO,
fileName: MOCK_GITHUB_FILENAME_ALPHA_TWO,
userId: MOCK_USER_ID_TWO,
}

export const MOCK_GITHUB_FILE_CHANGE_INFO_ALPHA_ONE: RawFileChangeInfo = {
sha: "66804d21ba86f1a193c31714bc15e388c2013a57",
filename: MOCK_GITHUB_FILENAME_ALPHA_ONE,
status: "modified",
additions: 1,
deletions: 2,
changes: 3,
blob_url: `https://github.com/isomerpages/a-test-v4/blob/${MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA}/${MOCK_GITHUB_FULL_FILEPATH_ALPHA_ONE}`,
raw_url: `https://github.com/isomerpages/a-test-v4/raw/${MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA}/${MOCK_GITHUB_FULL_FILEPATH_ALPHA_ONE}`,
contents_url: `https://api.github.com/repos/isomerpages/a-test-v4/contents/${MOCK_GITHUB_FULL_FILEPATH_ALPHA_ONE}?ref=${MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA}`,
}
export const MOCK_GITHUB_FILE_CHANGE_INFO_ALPHA_TWO: RawFileChangeInfo = {
sha: "f04f18eaa8d31fffc9f8cf5020b1f6a765ac225f",
filename: `${MOCK_GITHUB_FILEPATH_ALPHA_TWO}${MOCK_GITHUB_FILENAME_ALPHA_TWO}`,
status: "modified",
additions: 13,
deletions: 2,
changes: 15,
blob_url: `https://github.com/isomerpages/a-test-v4/blob/${MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA}/${MOCK_GITHUB_FULL_FILEPATH_ALPHA_TWO}`,
raw_url: `https://github.com/isomerpages/a-test-v4/raw/${MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA}/${MOCK_GITHUB_FULL_FILEPATH_ALPHA_TWO}`,
contents_url: `https://api.github.com/repos/isomerpages/a-test-v4/contents/${MOCK_GITHUB_FULL_FILEPATH_ALPHA_TWO}?ref=${MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA}`,
}

export const MOCK_GITHUB_COMMIT_ALPHA_ONE: Commit = {
url:
"https://api.github.com/repos/isomerpages/a-test-v4/commits/a79525f0d188880b965053bc0df25a041b476fad",
sha: "a79525f0d188880b965053bc0df25a041b476fad",
commit: {
url:
"https://api.github.com/repos/isomerpages/a-test-v4/git/commits/a79525f0d188880b965053bc0df25a041b476fad",
author: {
name: MOCK_GITHUB_USER_NAME_ONE,
email: MOCK_GITHUB_USER_EMAIL_ONE,
date: MOCK_GITHUB_COMMIT_DATE_ONE,
},
message: JSON.stringify(MOCK_GITHUB_COMMIT_MESSAGE_OBJECT_ALPHA_ONE),
},
}
export const MOCK_GITHUB_COMMIT_ALPHA_TWO: Commit = {
url:
"https://api.github.com/repos/isomerpages/a-test-v4/commits/ad2b13184f8ee1030636c304737941146bd67f4d",
sha: "ad2b13184f8ee1030636c304737941146bd67f4d",
commit: {
url:
"https://api.github.com/repos/isomerpages/a-test-v4/git/commits/ad2b13184f8ee1030636c304737941146bd67f4d",
author: {
name: MOCK_GITHUB_USER_NAME_TWO,
email: MOCK_GITHUB_USER_EMAIL_TWO,
date: MOCK_GITHUB_COMMIT_DATE_TWO,
},
message: JSON.stringify(MOCK_GITHUB_COMMIT_MESSAGE_OBJECT_ALPHA_TWO),
},
}
export const MOCK_GITHUB_COMMIT_ALPHA_THREE: Commit = {
url: `https://api.github.com/repos/isomerpages/a-test-v4/commits/${MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA}`,
sha: MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA,
commit: {
url: `https://api.github.com/repos/isomerpages/a-test-v4/git/commits/${MOCK_GITHUB_COMMIT_SHA_LATEST_ALPHA}`,
author: {
name: MOCK_GITHUB_USER_NAME_ONE,
email: MOCK_GITHUB_USER_EMAIL_ONE,
date: MOCK_GITHUB_COMMIT_DATE_THREE,
},
message: JSON.stringify(MOCK_GITHUB_COMMIT_MESSAGE_OBJECT_ALPHA_THREE),
},
}
// end of set

export const MOCK_GITHUB_COMMENT_BODY_ONE = "Comment 1"
export const MOCK_GITHUB_COMMENT_BODY_TWO = "Comment 2"

export const MOCK_GITHUB_COMMENT_OBJECT_ONE = {
message: MOCK_GITHUB_COMMENT_BODY_ONE,
fileName: MOCK_GITHUB_FILENAME_ALPHA_ONE,
userId: MOCK_USER_ID_ONE,
}

export const MOCK_GITHUB_COMMENT_OBJECT_TWO = {
message: MOCK_GITHUB_COMMENT_BODY_TWO,
fileName: MOCK_GITHUB_FILENAME_ALPHA_TWO,
userId: MOCK_USER_ID_TWO,
}

export const MOCK_GITHUB_RAWCOMMENT_ONE: RawComment = {
body: JSON.stringify(MOCK_GITHUB_COMMENT_OBJECT_ONE),
created_at: MOCK_GITHUB_COMMIT_DATE_ONE,
}
export const MOCK_GITHUB_RAWCOMMENT_TWO: RawComment = {
body: JSON.stringify(MOCK_GITHUB_COMMENT_OBJECT_TWO),
created_at: MOCK_GITHUB_COMMIT_DATE_THREE,
}
14 changes: 9 additions & 5 deletions src/fixtures/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Attributes } from "sequelize/types"

import { ReviewRequestStatus } from "@root/constants"
import { ReviewRequest, ReviewRequestView } from "@root/database/models"
import { Commit } from "@root/types/github"
import { Commit, RawPullRequest } from "@root/types/github"

import {
mockCollaboratorAdmin1,
Expand Down Expand Up @@ -56,10 +56,14 @@ export const MOCK_PULL_REQUEST_COMMIT_TWO: Commit = {
},
}

export const MOCK_PULL_REQUEST_ONE = {
title: "Pull Request 1",
body: "Pull Request 1 Description",
changed_files: 3,
export const MOCK_PULL_REQUEST_TITLE_ONE = "Pull Request 1"
export const MOCK_PULL_REQUEST_BODY_ONE = "Pull Request 1 Description"
export const MOCK_PULL_REQUEST_CHANGED_FILES_ONE = 3

export const MOCK_PULL_REQUEST_ONE: RawPullRequest = {
title: MOCK_PULL_REQUEST_TITLE_ONE,
body: MOCK_PULL_REQUEST_BODY_ONE,
changed_files: MOCK_PULL_REQUEST_CHANGED_FILES_ONE,
created_at: MOCK_GITHUB_DATE_ONE,
}

Expand Down
134 changes: 132 additions & 2 deletions src/fixtures/sites.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,132 @@
export const MOCK_SITE_NAME_ONE = "test-site-one"
export const MOCK_SITE_NAME_TWO = "test-site-two"
import { Attributes } from "sequelize/types"

import { Deployment, Repo, Site, SiteMember } from "@database/models"
import { CollaboratorRoles, JobStatus, SiteStatus } from "@root/constants"

import {
MOCK_USER_ID_FOUR,
MOCK_USER_ID_ONE,
MOCK_USER_ID_THREE,
MOCK_USER_ID_TWO,
} from "./users"

export const MOCK_SITE_ID_ONE = 1
export const MOCK_SITE_ID_TWO = 2

export const MOCK_SITE_NAME_ONE = "Human readable site name one"
export const MOCK_SITE_NAME_TWO = "Human readable site name two"

export const MOCK_SITE_DATE_ONE = new Date("2022-09-23T00:00:00Z")
export const MOCK_SITE_DATE_TWO = new Date("2022-09-25T00:00:00Z")

export const MOCK_REPO_NAME_ONE = "repo-name-test-one"
export const MOCK_REPO_NAME_TWO = "repo-name-test-two"

export const MOCK_REPO_URL_ONE = "https://github.com/example/repo-one"
export const MOCK_REPO_URL_TWO = "https://github.com/example/repo-two"

export const MOCK_DEPLOYMENT_PROD_URL_ONE =
"https://master.gibberishone.amplifyapp.com"
export const MOCK_DEPLOYMENT_PROD_URL_TWO =
"https://master.gibberishtwo.amplifyapp.com"

export const MOCK_DEPLOYMENT_STAGING_URL_ONE =
"https://staging.gibberishone.amplifyapp.com"
export const MOCK_DEPLOYMENT_STAGING_URL_TWO =
"https://staging.gibberishtwo.amplifyapp.com"

export const MOCK_SITE_DBENTRY_ONE: Attributes<Site> = {
id: MOCK_SITE_ID_ONE,
name: MOCK_REPO_NAME_ONE,
apiTokenName: "unused",
siteStatus: SiteStatus.Launched,
jobStatus: JobStatus.Ready,
creatorId: MOCK_USER_ID_ONE,
createdAt: MOCK_SITE_DATE_ONE,
updatedAt: MOCK_SITE_DATE_ONE,
}

export const MOCK_SITE_DBENTRY_TWO: Attributes<Site> = {
id: MOCK_SITE_ID_TWO,
name: MOCK_REPO_NAME_TWO,
apiTokenName: "unused",
siteStatus: SiteStatus.Launched,
jobStatus: JobStatus.Ready,
creatorId: MOCK_USER_ID_TWO,
createdAt: MOCK_SITE_DATE_TWO,
updatedAt: MOCK_SITE_DATE_TWO,
}

export const MOCK_REPO_DBENTRY_ONE: Attributes<Repo> = {
id: 1,
name: MOCK_REPO_NAME_ONE,
url: MOCK_REPO_URL_ONE,
siteId: MOCK_SITE_ID_ONE,
createdAt: MOCK_SITE_DATE_ONE,
updatedAt: MOCK_SITE_DATE_ONE,
}

export const MOCK_REPO_DBENTRY_TWO: Attributes<Repo> = {
id: 2,
name: MOCK_REPO_NAME_TWO,
url: MOCK_REPO_URL_TWO,
siteId: MOCK_SITE_ID_TWO,
createdAt: MOCK_SITE_DATE_TWO,
updatedAt: MOCK_SITE_DATE_TWO,
}

export const MOCK_DEPLOYMENT_DBENTRY_ONE: Attributes<Deployment> = {
id: 1,
siteId: MOCK_SITE_ID_ONE,
productionUrl: MOCK_DEPLOYMENT_PROD_URL_ONE,
stagingUrl: MOCK_DEPLOYMENT_STAGING_URL_ONE,
createdAt: MOCK_SITE_DATE_ONE,
updatedAt: MOCK_SITE_DATE_ONE,
hostingId: "1",
}

export const MOCK_DEPLOYMENT_DBENTRY_TWO: Attributes<Deployment> = {
id: 2,
siteId: MOCK_SITE_ID_TWO,
productionUrl: MOCK_DEPLOYMENT_PROD_URL_TWO,
stagingUrl: MOCK_DEPLOYMENT_STAGING_URL_TWO,
createdAt: MOCK_SITE_DATE_TWO,
updatedAt: MOCK_SITE_DATE_TWO,
hostingId: "1",
}

export const MOCK_SITEMEMBER_DBENTRY_ONE: Attributes<SiteMember> = {
id: 1,
userId: MOCK_USER_ID_ONE,
siteId: MOCK_SITE_ID_ONE,
role: CollaboratorRoles.Admin,
createdAt: MOCK_SITE_DATE_ONE,
updatedAt: MOCK_SITE_DATE_ONE,
}

export const MOCK_SITEMEMBER_DBENTRY_TWO: Attributes<SiteMember> = {
id: 2,
userId: MOCK_USER_ID_TWO,
siteId: MOCK_SITE_ID_ONE,
role: CollaboratorRoles.Contributor,
createdAt: MOCK_SITE_DATE_ONE,
updatedAt: MOCK_SITE_DATE_ONE,
}

export const MOCK_SITEMEMBER_DBENTRY_THREE: Attributes<SiteMember> = {
id: 3,
userId: MOCK_USER_ID_THREE,
siteId: MOCK_SITE_ID_TWO,
role: CollaboratorRoles.Admin,
createdAt: MOCK_SITE_DATE_TWO,
updatedAt: MOCK_SITE_DATE_TWO,
}

export const MOCK_SITEMEMBER_DBENTRY_FOUR: Attributes<SiteMember> = {
id: 4,
userId: MOCK_USER_ID_FOUR,
siteId: MOCK_SITE_ID_TWO,
role: CollaboratorRoles.Contributor,
createdAt: MOCK_SITE_DATE_TWO,
updatedAt: MOCK_SITE_DATE_TWO,
}
46 changes: 46 additions & 0 deletions src/fixtures/users.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Attributes } from "sequelize/types"

import { User } from "@database/models"

export const MOCK_USER_ID_ONE = 1
export const MOCK_USER_ID_TWO = 2
export const MOCK_USER_ID_THREE = 3
Expand All @@ -7,3 +11,45 @@ export const MOCK_USER_EMAIL_ONE = "[email protected]"
export const MOCK_USER_EMAIL_TWO = "[email protected]"
export const MOCK_USER_EMAIL_THREE = "[email protected]"
export const MOCK_USER_EMAIL_FOUR = "[email protected]"

export const MOCK_USER_DATE_ONE = new Date("2022-08-23T00:00:00Z")
export const MOCK_USER_DATE_TWO = new Date("2022-08-25T00:00:00Z")
export const MOCK_USER_DATE_THREE = new Date("2022-08-27T00:00:00Z")
export const MOCK_USER_DATE_FOUR = new Date("2022-08-29T00:00:00Z")

export const MOCK_USER_LAST_LOGIN_ONE = new Date("2022-09-12T00:00:00Z")
export const MOCK_USER_LAST_LOGIN_TWO = new Date("2022-09-14T00:00:00Z")
export const MOCK_USER_LAST_LOGIN_THREE = new Date("2022-09-16T00:00:00Z")
export const MOCK_USER_LAST_LOGIN_FOUR = new Date("2022-09-18T00:00:00Z")

export const MOCK_USER_DBENTRY_ONE: Attributes<User> = {
id: MOCK_USER_ID_ONE,
email: MOCK_USER_EMAIL_ONE,
lastLoggedIn: MOCK_USER_LAST_LOGIN_ONE,
createdAt: MOCK_USER_DATE_ONE,
updatedAt: MOCK_USER_DATE_ONE,
}

export const MOCK_USER_DBENTRY_TWO: Attributes<User> = {
id: MOCK_USER_ID_TWO,
email: MOCK_USER_EMAIL_TWO,
lastLoggedIn: MOCK_USER_LAST_LOGIN_TWO,
createdAt: MOCK_USER_DATE_TWO,
updatedAt: MOCK_USER_DATE_TWO,
}

export const MOCK_USER_DBENTRY_THREE: Attributes<User> = {
id: MOCK_USER_ID_THREE,
email: MOCK_USER_EMAIL_THREE,
lastLoggedIn: MOCK_USER_LAST_LOGIN_THREE,
createdAt: MOCK_USER_DATE_THREE,
updatedAt: MOCK_USER_DATE_THREE,
}

export const MOCK_USER_DBENTRY_FOUR: Attributes<User> = {
id: MOCK_USER_ID_FOUR,
email: MOCK_USER_EMAIL_FOUR,
lastLoggedIn: MOCK_USER_LAST_LOGIN_FOUR,
createdAt: MOCK_USER_DATE_FOUR,
updatedAt: MOCK_USER_DATE_FOUR,
}
Loading

0 comments on commit f4da383

Please sign in to comment.