Skip to content

Commit

Permalink
[Test] Add test for get content size endpoint (#71)
Browse files Browse the repository at this point in the history
* chore: add return to getSizeHandler

* test: add func for mocking json post requests

* test: add test setup

* chore(test): change package name to main_test

* refactor: move tests to ui dir

* chore: remove baseUrl to config

* test: api/cdn/size endpoint

* fix: set LoadEnvVariables to prod mode
  • Loading branch information
kevinanielsen authored Dec 26, 2023
1 parent e48c796 commit 685da91
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 11 deletions.
1 change: 1 addition & 0 deletions handlers/getSizeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func GetSizeHandler(c *gin.Context) {
if err != nil {
c.JSON(http.StatusInternalServerError, err)
log.Println(err)
return
}

c.JSON(200, gin.H{"cdn_size_bytes": cdnSize})
Expand Down
25 changes: 25 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main_test

import (
"os"
"testing"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/database"
ini "github.com/kevinanielsen/go-fast-cdn/initializers"
"github.com/kevinanielsen/go-fast-cdn/util"
)

func setup() {
util.LoadExPath()
gin.SetMode(gin.TestMode)
ini.LoadEnvVariables(true)
ini.CreateFolders()
database.ConnectToDB()
}

func TestMain(m *testing.M) {
setup()
code := m.Run()
os.Exit(code)
}
24 changes: 24 additions & 0 deletions testUtils/mockJsonPost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package testutils

import (
"bytes"
"encoding/json"
"io"

"github.com/gin-gonic/gin"
)

func MockJsonPost(c *gin.Context, content interface{}) {
c.Request.Method = "POST" // or PUT
c.Request.Header.Set("Content-Type", "application/json")

jsonbytes, err := json.Marshal(content)
if err != nil {
panic(err)
}

// the request body must be an io.ReadCloser
// the bytes buffer though doesn't implement io.Closer,
// so you wrap it in a no-op closer
c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonbytes))
}
2 changes: 1 addition & 1 deletion ui/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default defineConfig({
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',
baseURL: "http://localhost:8080",

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
Expand Down
20 changes: 20 additions & 0 deletions ui/tests/api/content-size-api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test, expect } from "@playwright/test";

/**
* Tests that the /api/cdn/size endpoint returns a 200 OK status.
* Verifies the status code is 200 and the response is ok.
*/
test("should return OK", async ({ request }) => {
const response = await request.get("/api/cdn/size");
expect(response.status()).toBe(200);
expect(response.ok()).toBe(true);
});

/**
* Test that ensures the /api/cdn/size endpoint returns a response with cdn_size_bytes > 0.
* This validates that the CDN size API is returning a valid non-zero byte count.
*/
test("should be > 0 bytes", async ({ request }) => {
const response = await request.get("/api/cdn/size");
expect((await response.json()).cdn_size_bytes).toBeGreaterThan(0);
});
1 change: 0 additions & 1 deletion ui/tests/config.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { test, expect } from "@playwright/test";
import { baseUrl } from "./config";

test.beforeEach(async ({ page }) => {
await page.goto(baseUrl);
await page.goto("/");
});

test("sidebar content-size label", async ({ page }) => {
Expand Down
13 changes: 6 additions & 7 deletions ui/tests/routes.spec.ts → ui/tests/ui/routes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { test, expect } from "@playwright/test";
import { baseUrl } from "./config";

/**
* E2E test that navigates to the base URL
* and verifies the page title matches
* the expected title regex.
*/
test("has title", async ({ page }) => {
await page.goto(baseUrl);
await page.goto("/");

await expect(page).toHaveTitle(/Go-Fast CDN/);
});
Expand All @@ -18,15 +17,15 @@ test("has title", async ({ page }) => {
* verifies the upload heading is visible.
*/
test("upload link", async ({ page }) => {
await page.goto(baseUrl);
await page.goto("/");

await page.getByRole("link", { name: "Upload Content" }).click();

await expect(
page.getByRole("heading", { name: "Upload Files" })
).toBeVisible();

expect(page.url()).toBe(baseUrl + "upload/docs");
expect(page.url()).toBe("/upload/docs");
});

/**
Expand All @@ -35,21 +34,21 @@ test("upload link", async ({ page }) => {
* updates correctly.
*/
test("upload tabs", async ({ page }) => {
await page.goto(baseUrl + "upload/docs");
await page.goto("/upload/docs");

/**
* Clicks the "Images" upload tab button,
* then asserts the URL updates to /upload/images.
*/
await page.getByRole("button", { name: "Images" }).click();

expect(page.url()).toBe(baseUrl + "upload/images");
expect(page.url()).toBe("/upload/images");

/**
* Clicks the "Documents" upload tab button,
* then asserts the URL updates to /upload/docs.
*/
await page.getByRole("button", { name: "Documents" }).click();

expect(page.url()).toBe(baseUrl + "upload/docs");
expect(page.url()).toBe("/upload/docs");
});

0 comments on commit 685da91

Please sign in to comment.