Compare commits
No commits in common. "2e44d1437023411d7458b7ee2ad40dbe76db665d" and "c6d93079790e0ca3e9945883d2223b20491686ea" have entirely different histories.
2e44d14370
...
c6d9307979
4 changed files with 6 additions and 97 deletions
4
Makefile
4
Makefile
|
@ -27,10 +27,6 @@ clean: remove-podman-containers
|
||||||
cd backend && make clean
|
cd backend && make clean
|
||||||
@echo "Cleaned up!"
|
@echo "Cleaned up!"
|
||||||
|
|
||||||
.PHONY: itest
|
|
||||||
itest:
|
|
||||||
python testing.py
|
|
||||||
|
|
||||||
# Cleans up everything related to podman, not just the project. Make sure you understand what this means.
|
# Cleans up everything related to podman, not just the project. Make sure you understand what this means.
|
||||||
podman-clean:
|
podman-clean:
|
||||||
podman system reset --force
|
podman system reset --force
|
||||||
|
|
|
@ -14,7 +14,6 @@ import (
|
||||||
type Database interface {
|
type Database interface {
|
||||||
// Insert a new user into the database, password should be hashed before calling
|
// Insert a new user into the database, password should be hashed before calling
|
||||||
AddUser(username string, password string) error
|
AddUser(username string, password string) error
|
||||||
CheckUser(username string, password string) bool
|
|
||||||
RemoveUser(username string) error
|
RemoveUser(username string) error
|
||||||
PromoteToAdmin(username string) error
|
PromoteToAdmin(username string) error
|
||||||
GetUserId(username string) (int, error)
|
GetUserId(username string) (int, error)
|
||||||
|
@ -78,15 +77,6 @@ func DbConnect(dbpath string) Database {
|
||||||
return &Db{db}
|
return &Db{db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Db) CheckUser(username string, password string) bool {
|
|
||||||
var dbPassword string
|
|
||||||
err := d.Get(&dbPassword, "SELECT password FROM users WHERE username = ?", username)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return dbPassword == password
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetProjectsForUser retrieves all projects associated with a specific user.
|
// GetProjectsForUser retrieves all projects associated with a specific user.
|
||||||
func (d *Db) GetProjectsForUser(username string) ([]types.Project, error) {
|
func (d *Db) GetProjectsForUser(username string) ([]types.Project, error) {
|
||||||
var projects []types.Project
|
var projects []types.Project
|
||||||
|
|
|
@ -106,20 +106,18 @@ func (gs *GState) IncrementButtonCount(c *fiber.Ctx) error {
|
||||||
|
|
||||||
// Login is a simple login handler that returns a JWT token
|
// Login is a simple login handler that returns a JWT token
|
||||||
func (gs *GState) Login(c *fiber.Ctx) error {
|
func (gs *GState) Login(c *fiber.Ctx) error {
|
||||||
// The body type is identical to a NewUser
|
// To test: curl --data "user=user&pass=pass" http://localhost:8080/api/login
|
||||||
u := new(types.NewUser)
|
user := c.FormValue("user")
|
||||||
if err := c.BodyParser(u); err != nil {
|
pass := c.FormValue("pass")
|
||||||
return c.Status(400).SendString(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if !gs.Db.CheckUser(u.Username, u.Password) {
|
// Throws Unauthorized error
|
||||||
println("User not found")
|
if user != "user" || pass != "pass" {
|
||||||
return c.SendStatus(fiber.StatusUnauthorized)
|
return c.SendStatus(fiber.StatusUnauthorized)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the Claims
|
// Create the Claims
|
||||||
claims := jwt.MapClaims{
|
claims := jwt.MapClaims{
|
||||||
"name": u.Username,
|
"name": user,
|
||||||
"admin": false,
|
"admin": false,
|
||||||
"exp": time.Now().Add(time.Hour * 72).Unix(),
|
"exp": time.Now().Add(time.Hour * 72).Unix(),
|
||||||
}
|
}
|
||||||
|
|
75
testing.py
75
testing.py
|
@ -1,75 +0,0 @@
|
||||||
import requests
|
|
||||||
import string
|
|
||||||
import random
|
|
||||||
|
|
||||||
|
|
||||||
def randomString(len=10):
|
|
||||||
"""Generate a random string of fixed length"""
|
|
||||||
letters = string.ascii_lowercase
|
|
||||||
return "".join(random.choice(letters) for i in range(len))
|
|
||||||
|
|
||||||
|
|
||||||
# Defined once per test run
|
|
||||||
username = randomString()
|
|
||||||
token = None
|
|
||||||
|
|
||||||
# The base URL of the API
|
|
||||||
base_url = "http://localhost:8080"
|
|
||||||
|
|
||||||
# Endpoint to test
|
|
||||||
registerPath = base_url + "/api/register"
|
|
||||||
loginPath = base_url + "/api/login"
|
|
||||||
addProjectPath = base_url + "/api/project"
|
|
||||||
|
|
||||||
|
|
||||||
# Define a function to prform POST request with data and return response
|
|
||||||
def register(username: string, password: string):
|
|
||||||
print("Registering with username: ", username, " and password: ", password)
|
|
||||||
response = requests.post(
|
|
||||||
registerPath, json={"username": username, "password": password}
|
|
||||||
)
|
|
||||||
print(response.text)
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
def login(username: string, password: string):
|
|
||||||
print("Logging in with username: ", username, " and password: ", password)
|
|
||||||
response = requests.post(
|
|
||||||
loginPath, json={"username": username, "password": password}
|
|
||||||
)
|
|
||||||
print(response.text)
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
def test_login():
|
|
||||||
response = login(username, "always_same")
|
|
||||||
assert response.status_code == 200, "Login failed"
|
|
||||||
print("Login successful")
|
|
||||||
return response.json()["token"]
|
|
||||||
|
|
||||||
|
|
||||||
# Define a function to test the POST request
|
|
||||||
def test_create_user():
|
|
||||||
response = register(username, "always_same")
|
|
||||||
assert response.status_code == 200, "Registration failed"
|
|
||||||
print("Registration successful")
|
|
||||||
|
|
||||||
|
|
||||||
def test_add_project():
|
|
||||||
loginResponse = login(username, "always_same")
|
|
||||||
token = loginResponse.json()["token"]
|
|
||||||
projectName = randomString()
|
|
||||||
response = requests.post(
|
|
||||||
addProjectPath,
|
|
||||||
json={"name": projectName, "description": "This is a project"},
|
|
||||||
headers={"Authorization": "Bearer " + token},
|
|
||||||
)
|
|
||||||
print(response.text)
|
|
||||||
assert response.status_code == 200, "Add project failed"
|
|
||||||
print("Add project successful")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
test_create_user()
|
|
||||||
test_login()
|
|
||||||
test_add_project()
|
|
Loading…
Add table
Reference in a new issue