Skip to content

Commit

Permalink
Merge pull request #54 from talDoFlemis/52-feat-add-authentication
Browse files Browse the repository at this point in the history
52 feat add authentication
  • Loading branch information
talDoFlemis authored Apr 24, 2024
2 parents 58c042d + 77bf4e9 commit 43991d7
Show file tree
Hide file tree
Showing 38 changed files with 2,382 additions and 2,330 deletions.
27 changes: 25 additions & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
pull_request:
branches: [main, dev]

env:
NEXTAUTH_URL: "http://localhost:3000"
NEXTAUTH_SECRET: "saidmarinaamoreternoA8GPVSoaK/mUU="

jobs:
test:
timeout-minutes: 60
Expand All @@ -23,6 +27,7 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: "1.22.x"
cache-dependency-path: backend/go.sum

- name: Install Task
uses: arduino/setup-task@v1
Expand All @@ -42,12 +47,30 @@ jobs:
- name: Run backend
run: task backend:run-web -- &


- name: Get installed Playwright version
id: playwright-version
run: echo "PLAYWRIGHT_VERSION=$(node -e "console.log(require('./package-lock.json').dependencies['@playwright/test'].version)")" >> $GITHUB_ENV
working-directory: frontend

- name: Cache playwright binaries
uses: actions/cache@v3
id: playwright-cache
with:
path: |
~/.cache/ms-playwright
key: ${{ runner.os }}-playwright-${{ env.PLAYWRIGHT_VERSION }}

- name: Install dependencies
run: npm ci
working-directory: frontend

- name: Install Playwright Browsers
run: npx playwright install --with-deps
- run: npx playwright install --with-deps
if: steps.playwright-cache.outputs.cache-hit != 'true'
working-directory: frontend

- run: npx playwright install-deps
if: steps.playwright-cache.outputs.cache-hit != 'true'
working-directory: frontend

- name: Run Playwright tests
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/go-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: "1.22.x"
cache-dependency-path: backend/go.sum


- name: Install Task
uses: arduino/setup-task@v1
Expand Down Expand Up @@ -58,6 +60,7 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: "1.22.x"
cache-dependency-path: backend/go.sum

- name: Install dependencies
run: |
Expand All @@ -81,6 +84,7 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: "1.22.x"
cache-dependency-path: backend/go.sum

- name: Install Task
uses: arduino/setup-task@v1
Expand Down
52 changes: 26 additions & 26 deletions backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,27 @@ const docTemplate = `{
"tags": [
"Authentication"
],
"summary": "Log In a User",
"summary": "Register an User",
"parameters": [
{
"description": "login Request",
"description": "Register User Request",
"name": "req",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/web.LoginRequest"
"$ref": "#/definitions/web.RegisterUserRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/web.TokenResponse"
}
},
"401": {
"description": "Authentication Failed",
"409": {
"description": "User already exists",
"schema": {
"type": "string"
}
Expand Down Expand Up @@ -132,7 +132,7 @@ const docTemplate = `{
}
}
},
"/auth/refresh": {
"/auth/login": {
"post": {
"consumes": [
"application/json"
Expand All @@ -143,15 +143,15 @@ const docTemplate = `{
"tags": [
"Authentication"
],
"summary": "Refresh an Access Token",
"summary": "Log In a User",
"parameters": [
{
"description": "Refresh Token Request",
"description": "login Request",
"name": "req",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/web.RefreshTokenRequest"
"$ref": "#/definitions/web.LoginRequest"
}
}
],
Expand All @@ -162,14 +162,8 @@ const docTemplate = `{
"$ref": "#/definitions/web.TokenResponse"
}
},
"400": {
"description": "Bad Refresh Token",
"schema": {
"type": "string"
}
},
"401": {
"description": "Expired Token",
"description": "Authentication Failed",
"schema": {
"type": "string"
}
Expand All @@ -183,7 +177,7 @@ const docTemplate = `{
}
}
},
"/auth/register": {
"/auth/refresh": {
"post": {
"consumes": [
"application/json"
Expand All @@ -194,27 +188,33 @@ const docTemplate = `{
"tags": [
"Authentication"
],
"summary": "Register an User",
"summary": "Refresh an Access Token",
"parameters": [
{
"description": "Register User Request",
"description": "Refresh Token Request",
"name": "req",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/web.RegisterUserRequest"
"$ref": "#/definitions/web.RefreshTokenRequest"
}
}
],
"responses": {
"201": {
"description": "Created",
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.TokenResponse"
}
},
"409": {
"description": "User already exists",
"400": {
"description": "Bad Refresh Token",
"schema": {
"type": "string"
}
},
"401": {
"description": "Expired Token",
"schema": {
"type": "string"
}
Expand Down
24 changes: 12 additions & 12 deletions backend/internal/adapters/drivers/web/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (h *authHandler) RegisterRoutes(router fiber.Router) {
// @Param req body LoginRequest true "login Request"
// @Success 200 {object} TokenResponse
// @Failure 401 {string} string "Authentication Failed"
// @Failure 422 {object} ValidationErrorResponse
// @Router /auth/ [post]
// @Failure 422 {object} ValidationErrorResponse
// @Router /auth/login [post]
func (h *authHandler) Login(c *fiber.Ctx) error {
req := new(LoginRequest)

Expand Down Expand Up @@ -151,7 +151,7 @@ func (h *authHandler) Login(c *fiber.Ctx) error {
// @Success 200 {object} TokenResponse
// @Failure 400 {string} string "Bad Refresh Token"
// @Failure 401 {string} string "Expired Token"
// @Failure 422 {object} ValidationErrorResponse
// @Failure 422 {object} ValidationErrorResponse
// @Router /auth/refresh [post]
func (h *authHandler) RefreshToken(c *fiber.Ctx) error {
req := new(RefreshTokenRequest)
Expand Down Expand Up @@ -194,8 +194,8 @@ func (h *authHandler) RefreshToken(c *fiber.Ctx) error {
// @Param req body RegisterUserRequest true "Register User Request"
// @Success 201 {object} TokenResponse
// @Failure 409 {string} string "User already exists"
// @Failure 422 {object} ValidationErrorResponse
// @Router /auth/register [post]
// @Failure 422 {object} ValidationErrorResponse
// @Router /auth/ [post]
func (h *authHandler) RegisterUser(c *fiber.Ctx) error {
req := new(RegisterUserRequest)

Expand Down Expand Up @@ -238,10 +238,10 @@ func (h *authHandler) RegisterUser(c *fiber.Ctx) error {
// @Accept json
// @Param req body UpdateAccountRequest true "Update Account Request"
// @Success 200
// @Failure 400 {string} string
// @Failure 401 {string} string
// @Failure 400 {string} string
// @Failure 401 {string} string
// @Failure 409 {string} string "User already exists"
// @Failure 422 {object} ValidationErrorResponse
// @Failure 422 {object} ValidationErrorResponse
// @Router /auth/ [put]
func (h *authHandler) UpdateAccount(c *fiber.Ctx) error {
id := extractTokenFromContext(c)
Expand Down Expand Up @@ -281,8 +281,8 @@ func (h *authHandler) UpdateAccount(c *fiber.Ctx) error {
// @Summary Delete an Account
// @Tags Authentication
// @Success 200
// @Failure 400 {string} string
// @Failure 401 {string} string
// @Failure 400 {string} string
// @Failure 401 {string} string
// @Router /auth/ [delete]
func (h *authHandler) DeleteAccount(c *fiber.Ctx) error {
id := extractTokenFromContext(c)
Expand All @@ -300,8 +300,8 @@ func (h *authHandler) DeleteAccount(c *fiber.Ctx) error {
// @Summary Get User Info
// @Tags Authentication
// @Success 200
// @Failure 400 {string} string
// @Failure 401 {string} string
// @Failure 400 {string} string
// @Failure 401 {string} string
// @Router /auth/userinfo [get]
func (h *authHandler) UserInfo(c *fiber.Ctx) error {
header := c.GetReqHeaders()["Authorization"]
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ services:
context: ./frontend/
ports:
- "3000:3000"
environment:
NEXTAUTH_URL: "${NEXTAUTH_URL:-http://localhost:3000}"
NEXTAUTH_SECRET: "${NEXTAUTH_SECRET:-saidmarinaamoreternoA8GPVSoaK/mUU=}"
BACKEND_API_URL: "${BACKEND_API_URL:-http://backend:42069}"
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:3000 || exit 1
interval: 60s
Expand Down
2 changes: 2 additions & 0 deletions frontend/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd frontend
npx lint-staged
53 changes: 25 additions & 28 deletions frontend/e2e/sign-in.spec.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
import { test, expect } from "@playwright/test";
import { expect, test } from "@playwright/test";

test.describe("Sign In page tests", () => {
test.beforeEach(async ({ page }) => {
await page.goto("/sign-in");
});

test.fixme(
"Should login and be redirected to dashboard",
async ({ page }) => {
const validEmail = "marcelomx30@gmail.com";
const validPassword = "mypassword";
test("Should login and be redirected to dashboard", async ({ page }) => {
const validUsername = "marcelomx30";
const validPassword = "mypassword";

await page.getByLabel(/email/i).fill(validEmail);
await page.getByLabel(/senha/i).fill(validPassword);
await page.getByRole("button").click();
await page.getByLabel(/username/i).fill(validUsername);
await page.getByLabel(/senha/i).fill(validPassword);
await page.getByRole("button", { name: "Login" }).click();

await expect(page).toHaveURL(/dashboard/);
},
);
await expect(page).toHaveURL(/dashboard/);
});

test.fixme(
"Should show an error if user submitted wrong password or email",
async ({ page }) => {
const validEmail = "marcelomx30@gmail.com";
const wrongPassword = "12345678";
test("Should show an error if user submitted wrong password or email", async ({
page,
}) => {
const validUsername = "marcelomx30";
const wrongPassword = "12345678";

const emailBtn = page.getByLabel(/email/i);
const passwordBtn = page.getByLabel(/senha/i);
const usernameInput = page.getByLabel(/username/i);
const passwordInput = page.getByLabel(/senha/i);

await emailBtn.fill(validEmail);
await passwordBtn.fill(wrongPassword);
await page.getByRole("button").click();
await usernameInput.fill(validUsername);
await passwordInput.fill(wrongPassword);
await page.getByRole("button", { name: "Login" }).click();

await expect(emailBtn).toHaveText(validEmail);
await expect(passwordBtn).toHaveText(wrongPassword);
await expect(page).toHaveURL(/sign-in/);
},
);
await expect(page.locator("#username-error")).toHaveText(
"Usuario ou senhas incorretas",
);
await expect(page).toHaveURL(/sign-in/);
});

test("Should click on register and go to sign up page", async ({ page }) => {
await page.getByRole("link", { name: /registrar/i }).click();
await expect(page).toHaveURL(/sign-up/);
});

test.skip("Should click on forgot password and go to forgot password page", async ({
test("Should click on forgot password and go to forgot password page", async ({
page,
}) => {
await page.getByRole("link", { name: /esqueci/i }).click();
Expand Down
Loading

0 comments on commit 43991d7

Please sign in to comment.