Skip to content

Commit

Permalink
✅ Add server repo tests (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
NatoBoram authored Sep 19, 2024
1 parent 8cdb0fb commit b0d17f0
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ BITBUCKET_CLOUD_APP_PASSWORD=

BITBUCKET_SERVER_URL=
BITBUCKET_SERVER_TOKEN=
BITBUCKET_SERVER_TEST_PROJECT_KEY=
BITBUCKET_SERVER_TEST_PROJECT_NAME=
2 changes: 2 additions & 0 deletions .github/workflows/node.js.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ jobs:
BITBUCKET_CLOUD_APP_PASSWORD: ${{ secrets.BITBUCKET_CLOUD_APP_PASSWORD }}
BITBUCKET_SERVER_URL: ${{ secrets.BITBUCKET_SERVER_URL }}
BITBUCKET_SERVER_TOKEN: ${{ secrets.BITBUCKET_SERVER_TOKEN }}
BITBUCKET_SERVER_TEST_PROJECT_KEY: ${{ vars.BITBUCKET_SERVER_TEST_PROJECT_KEY }}
BITBUCKET_SERVER_TEST_PROJECT_NAME: ${{ vars.BITBUCKET_SERVER_TEST_PROJECT_NAME }}
2 changes: 1 addition & 1 deletion .github/workflows/pnpm-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
scope: "@coderabbitai"
- run: pnpm publish --access public --no-git-checks --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.CODERABBIT_NPM_TOKEN }}

- run: pnpm pack --pack-gzip-level 9
- name: Sign
Expand Down
2 changes: 1 addition & 1 deletion tests/cloud/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const basic = toBase64(
BITBUCKET_CLOUD_USERNAME + ":" + BITBUCKET_CLOUD_APP_PASSWORD,
)

export const cloud = createBitbucketCloudClient({
export const client = createBitbucketCloudClient({
baseUrl: BITBUCKET_CLOUD_URL.toString(),
headers: { Accept: "application/json", Authorization: `Basic ${basic}` },
})
4 changes: 2 additions & 2 deletions tests/cloud/repositories.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { test } from "vitest"
import { cloud } from "./client.js"
import { client } from "./client.js"

test("GET /repositories", async ({ expect }) => {
const got = await cloud.GET("/repositories")
const got = await client.GET("/repositories")

expect(got.data?.next).toBeTypeOf("string")
expect(got.data?.pagelen).toBeTypeOf("number")
Expand Down
8 changes: 8 additions & 0 deletions tests/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ export const BITBUCKET_CLOUD_APP_PASSWORD = envString(
)
export const BITBUCKET_SERVER_URL = envUrl("BITBUCKET_SERVER_URL")
export const BITBUCKET_SERVER_TOKEN = envString("BITBUCKET_SERVER_TOKEN")
export const NODE_ENV = parsed.NODE_ENV

export const BITBUCKET_SERVER_TEST_PROJECT_KEY = envString(
"BITBUCKET_SERVER_TEST_PROJECT_KEY",
)
export const BITBUCKET_SERVER_TEST_PROJECT_NAME = envString(
"BITBUCKET_SERVER_TEST_PROJECT_NAME",
)
2 changes: 1 addition & 1 deletion tests/server/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createBitbucketServerClient } from "../../src/index.js"
import { BITBUCKET_SERVER_TOKEN, BITBUCKET_SERVER_URL } from "../env.js"

export const server = createBitbucketServerClient({
export const client = createBitbucketServerClient({
baseUrl: BITBUCKET_SERVER_URL.toString(),
headers: {
Accept: "application/json",
Expand Down
31 changes: 31 additions & 0 deletions tests/server/projects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, test } from "vitest"
import {
BITBUCKET_SERVER_TEST_PROJECT_KEY,
BITBUCKET_SERVER_TEST_PROJECT_NAME,
} from "../env.js"
import { client } from "./client.js"

describe("Projects", () => {
const key = BITBUCKET_SERVER_TEST_PROJECT_KEY
const name = BITBUCKET_SERVER_TEST_PROJECT_NAME

// The API, even with a token with Project Admin permissions, cannot create
// or delete projects.

test("Get projects", async ({ expect }) => {
const page = await client.GET("/api/latest/projects", {
params: { query: { name } },
})
expect(page.data?.size).toBeTypeOf("number")

const found = page.data?.values?.find(p => p.key === key)
expect(found).toMatchObject({ key, name })
})

test("Get a project", async ({ expect }) => {
const project = await client.GET("/api/latest/projects/{projectKey}", {
params: { path: { projectKey: key } },
})
expect(project.data).toMatchObject({ key, name })
})
})
64 changes: 59 additions & 5 deletions tests/server/repositories.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
import { test } from "vitest"
import { server } from "./client.js"
import { describe, test } from "vitest"
import {
BITBUCKET_SERVER_TEST_PROJECT_KEY,
BITBUCKET_SERVER_TEST_PROJECT_NAME,
} from "../env.js"
import { client } from "./client.js"

test("GET /api/latest/repos", async ({ expect }) => {
const got = await server.GET("/api/latest/repos")
describe("Repositories", { concurrent: false, sequential: true }, () => {
const projectKey = BITBUCKET_SERVER_TEST_PROJECT_KEY
const projectName = BITBUCKET_SERVER_TEST_PROJECT_NAME
const slug = "test-repository"
const name = "Test Repository"

expect(got.data?.size).toBeTypeOf("number")
test("Create repository", async ({ expect }) => {
const created = await client.POST(
"/api/latest/projects/{projectKey}/repos",
{ params: { path: { projectKey } }, body: { name, scmId: "git", slug } },
)

if (created.error)
console.error("Failed to create a repository", created.error)

expect(created).toMatchObject({
data: {
slug,
name,
project: { key: projectKey, name: projectName },
scmId: "git",
},
response: { status: 201 },
})
})

test("Get a repository", async ({ expect }) => {
const repository = await client.GET(
"/api/latest/projects/{projectKey}/repos/{repositorySlug}",
{ params: { path: { projectKey, repositorySlug: slug } } },
)

if (repository.error)
console.error("Failed to get a repository", repository.error)

expect(repository.data).toMatchObject({
slug,
name,
project: { key: projectKey, name: projectName },
scmId: "git",
})
})

test("Delete a repository", async ({ expect }) => {
const deleted = await client.DELETE(
"/api/latest/projects/{projectKey}/repos/{repositorySlug}",
{ params: { path: { projectKey, repositorySlug: slug } } },
)

if (deleted.error)
console.error("Failed to delete a repository", deleted.error)

expect(deleted.response.status).toBe(202)
})
})

0 comments on commit b0d17f0

Please sign in to comment.