Extending test script
This commit is contained in:
parent
04d7a2cdec
commit
2e44d14370
2 changed files with 50 additions and 19 deletions
|
@ -106,17 +106,20 @@ func (gs *GState) IncrementButtonCount(c *fiber.Ctx) error {
|
|||
|
||||
// Login is a simple login handler that returns a JWT token
|
||||
func (gs *GState) Login(c *fiber.Ctx) error {
|
||||
// To test: curl --data "user=user&pass=pass" http://localhost:8080/api/login
|
||||
user := c.FormValue("user")
|
||||
pass := c.FormValue("pass")
|
||||
// The body type is identical to a NewUser
|
||||
u := new(types.NewUser)
|
||||
if err := c.BodyParser(u); err != nil {
|
||||
return c.Status(400).SendString(err.Error())
|
||||
}
|
||||
|
||||
if !gs.Db.CheckUser(user, pass) {
|
||||
if !gs.Db.CheckUser(u.Username, u.Password) {
|
||||
println("User not found")
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
// Create the Claims
|
||||
claims := jwt.MapClaims{
|
||||
"name": user,
|
||||
"name": u.Username,
|
||||
"admin": false,
|
||||
"exp": time.Now().Add(time.Hour * 72).Unix(),
|
||||
}
|
||||
|
|
56
testing.py
56
testing.py
|
@ -2,46 +2,74 @@ import requests
|
|||
import string
|
||||
import random
|
||||
|
||||
|
||||
def randomString(len=10):
|
||||
"""Generate a random string of fixed length """
|
||||
"""Generate a random string of fixed length"""
|
||||
letters = string.ascii_lowercase
|
||||
return ''.join(random.choice(letters) for i in range(len))
|
||||
return "".join(random.choice(letters) for i in range(len))
|
||||
|
||||
|
||||
# Defined once per test run
|
||||
username = randomString()
|
||||
token = None
|
||||
|
||||
# Define the base URL of the API
|
||||
# The base URL of the API
|
||||
base_url = "http://localhost:8080"
|
||||
|
||||
# Define the endpoint to test
|
||||
# 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 create_user(data):
|
||||
response = requests.post(registerPath, json=data)
|
||||
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, password):
|
||||
response = requests.post(loginPath, json={"username": username, "password": password})
|
||||
|
||||
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")
|
||||
print(response.json()["token"])
|
||||
return response.json()["token"]
|
||||
|
||||
|
||||
# Define a function to test the POST request
|
||||
def test_create_user():
|
||||
data = {"username": username, "password": "always_same"}
|
||||
response = create_user(data)
|
||||
response = register(username, "always_same")
|
||||
assert response.status_code == 200, "Registration failed"
|
||||
print("Registration successful")
|
||||
|
||||
# Run the tests
|
||||
|
||||
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_get_users()
|
||||
test_create_user()
|
||||
test_login()
|
||||
|
||||
test_add_project()
|
||||
|
|
Loading…
Reference in a new issue