-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: migrate to postgres in local dev env
- Loading branch information
1 parent
d70c415
commit 7650ed4
Showing
18 changed files
with
181 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
./backend | ||
.env | ||
*.db | ||
/.env | ||
*.db | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
services: | ||
database: | ||
image: postgres | ||
restart: always | ||
environment: | ||
- POSTGRES_USER=${DATABASE_USERNAME} | ||
- POSTGRES_PASSWORD=${DATABASE_PASSWORD} | ||
- POSTGRES_DB=${DATABASE_NAME} | ||
- PGPORT=${DATABASE_PORT} | ||
- PGHOST=${DATABASE_HOST} | ||
ports: | ||
- ${DATABASE_PORT}:${DATABASE_PORT} | ||
volumes: | ||
- db:/var/lib/postgresql/data | ||
|
||
volumes: | ||
db: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
BACKEND_PORT=8080 | ||
DATABASE_HOST=localhost | ||
DATABASE_PORT=5432 | ||
DATABASE_NAME=testDb | ||
DATABASE_USERNAME=user | ||
DATABASE_PASSWORD=pass | ||
JWT_SECRET_KEY= | ||
JWT_VALIDITY_TIME=720 | ||
GH_OAUTH_CLIENT_ID= | ||
GITHUB_OAUTH_CLIENT_SECRET= | ||
ORIGINS_ALLOWED=kwoc.kossiitkgp.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
services: | ||
database: | ||
image: postgres | ||
restart: always | ||
environment: | ||
- POSTGRES_USER=${DATABASE_USERNAME} | ||
- POSTGRES_PASSWORD=${DATABASE_PASSWORD} | ||
- POSTGRES_DB=${DATABASE_NAME} | ||
- PGPORT=${DATABASE_PORT} | ||
- PGHOST=${DATABASE_HOST} | ||
ports: | ||
- ${DATABASE_PORT}:${DATABASE_PORT} | ||
volumes: | ||
- db:/var/lib/postgresql/data | ||
|
||
volumes: | ||
db: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,13 @@ import ( | |
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"gorm.io/gorm" | ||
"net/http" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"gorm.io/gorm" | ||
|
||
"github.com/kossiitkgp/kwoc-backend/v2/controllers" | ||
"github.com/kossiitkgp/kwoc-backend/v2/models" | ||
"github.com/kossiitkgp/kwoc-backend/v2/utils" | ||
|
@@ -133,7 +134,7 @@ func tMentorRegAsStudent(db *gorm.DB, t *testing.T) { | |
func TestMentorRegOK(t *testing.T) { | ||
// Set up a local test database path | ||
db := setTestDB() | ||
defer unsetTestDB() | ||
defer unsetTestDB(db) | ||
|
||
// Generate a jwt secret key for testing | ||
setTestJwtSecretKey() | ||
|
@@ -164,6 +165,78 @@ func TestMentorRegOK(t *testing.T) { | |
) | ||
} | ||
|
||
func createFetchMentorRequest() *http.Request { | ||
req, _ := http.NewRequest( | ||
"GET", | ||
"/mentor/all/", | ||
nil, | ||
) | ||
return req | ||
} | ||
|
||
// Test unauthenticated request to /mentor/all/ | ||
func TestFetchMentorNoAuth(t *testing.T) { | ||
testRequestNoAuth(t, "GET", "/mentor/all/") | ||
} | ||
|
||
// Test request to /mentor/all/ with invalid jwt | ||
func TestFetchMentorInvalidAuth(t *testing.T) { | ||
testRequestInvalidAuth(t, "GET", "/mentor/all/") | ||
} | ||
|
||
func TestFetchMentorOK(t *testing.T) { | ||
const numMentors = 10 | ||
// Set up a local test database path | ||
db := setTestDB() | ||
defer unsetTestDB(db) | ||
|
||
// Generate a jwt secret key for testing | ||
setTestJwtSecretKey() | ||
defer unsetTestJwtSecretKey() | ||
|
||
// Test login fields | ||
testUsername := getTestUsername() | ||
testLoginFields := utils.LoginJwtFields{Username: testUsername} | ||
|
||
testJwt, _ := utils.GenerateLoginJwtString(testLoginFields) | ||
|
||
modelMentors := make([]models.Mentor, 0, numMentors) | ||
var testMentors [numMentors]controllers.Mentor | ||
for i := 0; i < numMentors; i++ { | ||
modelMentors = append(modelMentors, | ||
models.Mentor{ | ||
Name: fmt.Sprintf("Test%d", i), | ||
Username: fmt.Sprintf("test%d", i), | ||
Email: fmt.Sprintf("test%[email protected]", i), | ||
}) | ||
testMentors[i] = controllers.Mentor{ | ||
Name: fmt.Sprintf("Test%d", i), | ||
Username: fmt.Sprintf("test%d", i), | ||
} | ||
|
||
} | ||
_ = db.Table("mentors").Create(modelMentors) | ||
|
||
req := createFetchMentorRequest() | ||
req.Header.Add("Bearer", testJwt) | ||
|
||
res := executeRequest(req, db) | ||
|
||
var resMentors []controllers.Mentor | ||
_ = json.NewDecoder(res.Body).Decode(&resMentors) | ||
|
||
expectStatusCodeToBe(t, res, http.StatusOK) | ||
if len(resMentors) != numMentors { | ||
t.Fatalf("Not getting expected numbers of mentors from /mentor/all/") | ||
} | ||
|
||
for i, mentor := range resMentors { | ||
if mentor != testMentors[i] { | ||
t.Fatalf("Incorrect mentors returned from /mentor/all/") | ||
} | ||
} | ||
} | ||
|
||
// Test unauthenticated request to /mentor/dashboard/ | ||
func TestMentorDashboardNoAuth(t *testing.T) { | ||
testRequestNoAuth(t, "GET", "/mentor/dashboard/") | ||
|
@@ -178,7 +251,7 @@ func TestMentorDashboardInvalidAuth(t *testing.T) { | |
func TestMentorDashboardNoReg(t *testing.T) { | ||
// Set up a local test database path | ||
db := setTestDB() | ||
defer unsetTestDB() | ||
defer unsetTestDB(db) | ||
|
||
// Generate a jwt secret key for testing | ||
setTestJwtSecretKey() | ||
|
@@ -207,7 +280,7 @@ func TestMentorDashboardNoReg(t *testing.T) { | |
func TestMentorDashboardOK(t *testing.T) { | ||
// Set up a local test database path | ||
db := setTestDB() | ||
defer unsetTestDB() | ||
defer unsetTestDB(db) | ||
|
||
// Generate a jwt secret key for testing | ||
setTestJwtSecretKey() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -xe | ||
|
||
REPO_PATH=$(git rev-parse --show-toplevel) | ||
|
||
cd $REPO_PATH/tests | ||
source .env | ||
|
||
docker compose -f docker-compose.test.yaml up --build -d | ||
|
||
go test ./... -p 1 | ||
|
||
docker compose -f docker-compose.test.yaml down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.