Skip to content

Commit

Permalink
Merge pull request #159 from COS301-SE-2024/feat/new-e2e-tests
Browse files Browse the repository at this point in the history
Feat/new e2e tests
  • Loading branch information
MichaelHorwitz authored Jun 23, 2024
2 parents 9ccd709 + 33dd64b commit 203b4c8
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 81 deletions.
13 changes: 4 additions & 9 deletions .github/workflows/cypress-test-basic.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
name: End-to-end tests
on:
workflow_dispatch:
pull_request:
branches:
- main
- release
- dev
types:
- opened
- reopened
jobs:
cypress-run:
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -54,7 +46,10 @@ jobs:
${{ runner.os }}-build-
- name: Start server
run: |
cd
cd $GITHUB_WORKSPACE
touch fe.env
touch api.env
touch db.env
docker compose up -d
- name: Cypress run
uses: cypress-io/github-action@v6
Expand Down
111 changes: 111 additions & 0 deletions .github/workflows/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Deployment

on:
workflow_run:

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
- name: Make db env file
uses: SpicyPizza/[email protected]
with:
envkey_POSTGRES_PASSWORD : ${{ secrets.POSTGRES_PASSWORD }}
envkey_POSTGRES_USER : ${{ secrets.POSTGRES_USER }}
envkey_POSTGRES_DB : ${{ secrets.POSTGRES_DB }}
directory: .
file_name: db.env
fail_on_empty: false
- name: Make api env files
uses: SpicyPizza/[email protected]
with:
envkey_DATABASE_URL : ${{ secrets.DATABASE_URL }}
envkey_DATABASE_PORT : ${{ secrets.DATABASE_PORT }}
envkey_DATABASE_NAME : ${{ secrets.DATABASE_NAME }}
envkey_DATABASE_USER : ${{ secrets.DATABASE_USER }}
envkey_DATABASE_PASSWORD : ${{ secrets.DATABASE_PASSWORD }}
envkey_COMPANY_EMAIL : ${{ secrets.COMPANY_EMAIL }}
envkey_COMPANY_AUTH : ${{ secrets.COMPANY_AUTH }}
directory: .
file_name: api.env
fail_on_empty: false
- name: Make fe env files
uses: SpicyPizza/[email protected]
with:
envkey_API_URL : ${{ secrets.API_URL }}
directory: .
file_name: fe.env
fail_on_empty: false

- name: Docker compose build
run: |
cd $GITHUB_WORKSPACE
docker compose build
- name: Make it a tar file
run: |
cd $GITHUB_WORKSPACE
docker save -o frontend.tar dispute-resolution-engine-frontend:latest
docker save -o api.tar api
docker save -o postgres.tar postgres
- name: Install wireguard
run: sudo apt-get install wireguard

- name: Create WG Config
run: |
sed -i -e "s@{{WG_PRIVATEKEY}}@${{ secrets.WG_PRIVATEKEY }}@" $GITHUB_WORKSPACE/tunnel.conf
sed -i -e "s@{{WG_ADDRESS}}@${{ secrets.WG_ADDRESS }}@" $GITHUB_WORKSPACE/tunnel.conf
sed -i -e "s@{{WG_DNS}}@${{ secrets.WG_DNS }}@" $GITHUB_WORKSPACE/tunnel.conf
sed -i -e "s@{{WG_PUBLICKEY}}@${{ secrets.WG_PUBLICKEY }}@" $GITHUB_WORKSPACE/tunnel.conf
sed -i -e "s@{{WG_ALLOWEDIPS}}@${{ secrets.WG_ALLOWEDIPS }}@" $GITHUB_WORKSPACE/tunnel.conf
sed -i -e "s@{{WG_ENDPOINT}}@${{ secrets.WG_ENDPOINT }}@" $GITHUB_WORKSPACE/tunnel.conf
sudo cp $GITHUB_WORKSPACE/tunnel.conf /etc/wireguard/
- name: WG Creating Tunnel
run: |
sudo apt install resolvconf
wg-quick up $GITHUB_WORKSPACE/tunnel.conf
- name: Send through the docker container using scp
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
port: ${{ secrets.SSH_PORT }}
source: "api.tar,frontend.tar,postgres.tar,docker-compose.yaml"
target: ./
- name: Spin up docker image
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
port: ${{ secrets.SSH_PORT }}
script: |
docker load -i frontend.tar
docker load -i api.tar
docker load -i postgres.tar
docker-compose up -d
cypress-test:
container:
image: cypress/included
needs: deploy
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Cypress run
uses: cypress-io/github-action@v2
with:
wait-on: 'http://capstone-dre.dns.net.za'
wait-on-timeout: 300
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
14 changes: 5 additions & 9 deletions .github/workflows/go-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ name: Go Test
run-name: ${{ github.actor }} at ${{ github.event_name }} event
on:
workflow_dispatch:
pull_request:
branches:
- main
- release
- dev
types:
- opened
- reopened
jobs:
go-test:
runs-on: ubuntu-latest
Expand All @@ -21,4 +13,8 @@ jobs:
- name: Run go test
run: |
cd api/__tests__
go test
go test
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
14 changes: 5 additions & 9 deletions .github/workflows/jest-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ name: Jest-Test
run-name: ${{ github.actor }} at ${{ github.event_name }} event
on:
workflow_dispatch:
pull_request:
branches:
- main
- release
- dev
types:
- opened
- reopened
jobs:
jest-test:
runs-on: ubuntu-latest
Expand All @@ -28,4 +20,8 @@ jobs:
- name: Run jest
run: |
cd frontend
yarn test
yarn test
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
8 changes: 0 additions & 8 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ name: Linting
run-name: ${{ github.actor }} at ${{ github.event_name }} event
on:
workflow_dispatch:
pull_request:
branches:
- main
- release
- dev
types:
- opened
- reopened
jobs:
jest-test:
runs-on: ubuntu-latest
Expand Down
60 changes: 60 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Unit-And-Integration-Tests
run-name: ${{ github.actor }} at ${{ github.event_name }} event
on:
workflow_dispatch:
jobs:
setup server:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: '18.20.3'
- name: Install dependencies
run: |
cd frontend
npm install -g yarn
yarn
- name: Build Docker
run: |
docker compose build
docker compose up -d
run jest tests:
runs-on: ubuntu-latest
depends-on: setup server
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: '18.20.3'
- name: Install dependencies
run: |
cd frontend
npm install -g yarn
yarn
- name: Run jest
run: |
cd frontend
yarn test
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
run go tests:
runs-on: ubuntu-latest
depends-on: setup server
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run go test
run: |
cd api/__tests__
go test
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<img src="https://github.com/COS301-SE-2024/Dispute-Resolution-Engine/actions/workflows/go-test.yaml/badge.svg">
<img src="https://github.com/COS301-SE-2024/Dispute-Resolution-Engine/actions/workflows/jest-test.yaml/badge.svg">
<img src="https://github.com/COS301-SE-2024/Dispute-Resolution-Engine/actions/workflows/lint.yaml/badge.svg">
<img src="https://github.com/COS301-SE-2024/Dispute-Resolution-Engine/actions/workflows/deployment.yml/badge.svg">
<img src="https://github.com/COS301-SE-2024/Dispute-Resolution-Engine/actions/workflows/deployment.yaml/badge.svg">
<a href="https://codecov.io/github/COS301-SE-2024/Dispute-Resolution-Engine" >
<img src="https://codecov.io/github/COS301-SE-2024/Dispute-Resolution-Engine/branch/working/graph/badge.svg?token=Q021VJLYQ8"/>
</a>
Expand Down
25 changes: 0 additions & 25 deletions api/__tests__/api-test.go

This file was deleted.

53 changes: 53 additions & 0 deletions api/__tests__/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package api

import (
"api/handlers"
"api/db" // Import the package that contains the db symbo
"net/http"
"net/http/httptest"
"strings"
"testing"
)

// MockResponseWriter is used to capture the HTTP response
type MockResponseWriter struct {
httptest.ResponseRecorder
}

func TestCreateUser(t *testing.T) {
DB := db.Init()
// Example of a valid request body
validRequestBody := `{"first_name":"John","surname":"Doe","birthdate":"1990-01-01","nationality":"Country","email":"[email protected]"}`
// Example of an invalid request body (incomplete JSON)
invalidRequestBody := `{"first_name":"John"`

tests := []struct {
name string
body string
expectedStatus int
}{
{"Valid Request", validRequestBody, http.StatusOK},
{"Invalid Request", invalidRequestBody, http.StatusBadRequest},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
request, err := http.NewRequest("POST", "/createuser", strings.NewReader(tt.body))
if err != nil {
t.Fatal(err)
}

// Use httptest.ResponseRecorder to capture the response
responseRecorder := httptest.NewRecorder()
handler := handlers.New(DB)

// Serve the HTTP request to our handler
handler.CreateUser(responseRecorder, request)

// Check the status code is what we expect
if status := responseRecorder.Code; status != tt.expectedStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, tt.expectedStatus)
}
})
}
}
25 changes: 25 additions & 0 deletions api/__tests__/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package api

import (
"api/db"
"api/handlers"
"net/http"
"github.com/gorilla/mux"

"testing"
)

type MockStorage struct{}

func TestInit(t *testing.T) {
DB := db.Init()
if DB == nil {
t.Errorf("Expected db to be initialized, got nil")
}
h := handlers.New(DB)
router := mux.NewRouter()

router.HandleFunc("/createAcc", h.CreateUser).Methods(http.MethodPost)
router.HandleFunc("/login", h.LoginUser).Methods(http.MethodPost)

}
Loading

0 comments on commit 203b4c8

Please sign in to comment.