Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Nov 28, 2023
1 parent c68afac commit d24ea97
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/configuration/database/Database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestApiKey(t *testing.T) {
test.IsEqualString(t, keys["newkey"].Id, "newkey")
test.IsEqualString(t, keys["newkey"].LastUsedString, "LastUsed")
test.IsEqualInt64(t, keys["newkey"].LastUsed, 100)
test.IsEqualInt(t, keys["newkey"].Permissions, 20)
test.IsEqualBool(t, keys["newkey"].Permissions == 20, true)

test.IsEqualInt(t, len(GetAllApiKeys()), 2)
DeleteApiKey("newkey2")
Expand Down
8 changes: 4 additions & 4 deletions internal/models/Api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package models

const (
ApiPermView = 1 << iota // upper case
ApiPermUpload // lower case
ApiPermDelete // capitalizes
ApiPermApiMod // reverses
ApiPermView = 1 << iota
ApiPermUpload
ApiPermDelete
ApiPermApiMod
)

const ApiPermNone = 0
Expand Down
6 changes: 5 additions & 1 deletion internal/test/testconfiguration/TestConfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,24 @@ func writeApiKeyys() {
database.SaveApiKey(models.ApiKey{
Id: "validkey",
FriendlyName: "First Key",
Permissions: models.ApiPermAll, // TODO
})
database.SaveApiKey(models.ApiKey{
Id: "GAh1IhXDvYnqfYLazWBqMB9HSFmNPO",
FriendlyName: "Second Key",
LastUsed: 1620671580,
LastUsedString: "used",
Permissions: models.ApiPermAll, // TODO
})
database.SaveApiKey(models.ApiKey{
Id: "jiREglQJW0bOqJakfjdVfe8T1EM8n8",
FriendlyName: "Unnamed Key",
Permissions: models.ApiPermAll, // TODO
})
database.SaveApiKey(models.ApiKey{
Id: "okeCMWqhVMZSpt5c1qpCWhKvJJPifb",
FriendlyName: "Unnamed Key",
Permissions: models.ApiPermAll, // TODO
})
}

Expand Down Expand Up @@ -328,7 +332,7 @@ var configTestFile = []byte(`{
"Port":"127.0.0.1:53843",
"ServerUrl": "http://127.0.0.1:53843/",
"RedirectUrl": "https://test.com/",
"ConfigVersion": 15,
"ConfigVersion": 16,
"LengthId": 20,
"DataDir": "test/data",
"MaxMemory": 10,
Expand Down
2 changes: 2 additions & 0 deletions internal/webserver/api/Api.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ func isAuthorisedForApi(w http.ResponseWriter, request apiRequest) bool {
return false
}

// TODO investigate superfluous response.WriteHeader call from github.com/forceu/gokapi/internal/webserver/api.sendError (Api.go:244)
// Probably from new API permission system
func sendError(w http.ResponseWriter, errorInt int, errorMessage string) {
w.WriteHeader(errorInt)
_, _ = w.Write([]byte("{\"Result\":\"error\",\"ErrorMessage\":\"" + errorMessage + "\"}"))
Expand Down
10 changes: 6 additions & 4 deletions internal/webserver/api/Api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func TestMain(m *testing.M) {
os.Exit(exitVal)
}

// TODO test new permission system

const maxMemory = 20

var newKeyId string
Expand All @@ -56,13 +58,13 @@ func TestDeleteKey(t *testing.T) {
}

func TestIsValidApiKey(t *testing.T) {
test.IsEqualBool(t, IsValidApiKey("", false), false)
test.IsEqualBool(t, IsValidApiKey("invalid", false), false)
test.IsEqualBool(t, IsValidApiKey("validkey", false), true)
test.IsEqualBool(t, IsValidApiKey("", false, models.ApiPermNone), false) // TODO permission
test.IsEqualBool(t, IsValidApiKey("invalid", false, models.ApiPermNone), false) // TODO permission
test.IsEqualBool(t, IsValidApiKey("validkey", false, models.ApiPermNone), true) // TODO permission
key, ok := database.GetApiKey("validkey")
test.IsEqualBool(t, ok, true)
test.IsEqualBool(t, key.LastUsed == 0, true)
test.IsEqualBool(t, IsValidApiKey("validkey", true), true)
test.IsEqualBool(t, IsValidApiKey("validkey", true, models.ApiPermNone), true) // TODO permission
key, ok = database.GetApiKey("validkey")
test.IsEqualBool(t, ok, true)
test.IsEqualBool(t, key.LastUsed == 0, false)
Expand Down

0 comments on commit d24ea97

Please sign in to comment.