update mods

This commit is contained in:
2025-12-02 21:17:12 +01:00
parent 513b7b726e
commit ce8a14cc91
9 changed files with 214 additions and 180 deletions

View File

@@ -18,12 +18,13 @@ package api
import (
//"github.com/alecthomas/repr"
"github.com/maxatome/go-testdeep/td"
"codeberg.org/scip/ephemerup/cfg"
"codeberg.org/scip/ephemerup/common"
"os"
"testing"
"time"
"codeberg.org/scip/ephemerup/cfg"
"codeberg.org/scip/ephemerup/common"
"github.com/maxatome/go-testdeep/td"
)
func finalize(db *Db) {
@@ -51,11 +52,11 @@ func TestNew(t *testing.T) {
db, err := NewDb(c)
defer finalize(db)
if err != nil && !tt.wantfail {
t.Errorf("expected: &Db{}, got err: " + err.Error())
t.Error("expected: &Db{}, got err: " + err.Error())
}
if err == nil && tt.wantfail {
t.Errorf("expected: fail, got &Db{}")
t.Error("expected: fail, got &Db{}")
}
})
}
@@ -94,8 +95,8 @@ var dbtests = []struct {
}
/*
We need to test the whole Db operation in one run, because it
doesn't work well if using a global Db.
We need to test the whole Db operation in one run, because it
doesn't work well if using a global Db.
*/
func TestDboperation(t *testing.T) {
for _, tt := range dbtests {
@@ -106,14 +107,14 @@ func TestDboperation(t *testing.T) {
defer finalize(db)
if err != nil {
t.Errorf("Could not open new DB: " + err.Error())
t.Error("Could not open new DB: " + err.Error())
}
if tt.upload.Id != "" {
// set ts
ts, err := time.Parse(timeformat, tt.ts)
if err != nil {
t.Errorf("Could not parse time: " + err.Error())
t.Error("Could not parse time: " + err.Error())
}
tt.upload.Created = common.Timestamp{Time: ts}
@@ -121,18 +122,18 @@ func TestDboperation(t *testing.T) {
// create new upload db object
err = db.Insert(tt.id, tt.upload)
if err != nil {
t.Errorf("Could not insert new upload object: " + err.Error())
t.Error("Could not insert new upload object: " + err.Error())
}
// fetch it
response, err := db.Get(tt.context, tt.id, common.TypeUpload)
if err != nil {
t.Errorf("Could not fetch upload object: " + err.Error())
t.Error("Could not fetch upload object: " + err.Error())
}
// is it there?
if len(response.Uploads) != 1 {
t.Errorf("db.Get() did not return an upload obj")
t.Error("db.Get() did not return an upload obj")
}
// compare times
@@ -151,24 +152,24 @@ func TestDboperation(t *testing.T) {
// fetch list
response, err = db.List(tt.context, tt.filter, tt.query, common.TypeUpload)
if err != nil {
t.Errorf("Could not fetch uploads list: " + err.Error())
t.Error("Could not fetch uploads list: " + err.Error())
}
// is it there?
if len(response.Uploads) != 1 {
t.Errorf("db.List() did not return upload obj[s]")
t.Error("db.List() did not return upload obj[s]")
}
// delete
err = db.Delete(tt.context, tt.id)
if err != nil {
t.Errorf("Could not delete upload obj: " + err.Error())
t.Error("Could not delete upload obj: " + err.Error())
}
// fetch again, shall return empty
_, err = db.Get(tt.context, tt.id, common.TypeUpload)
if err == nil {
t.Errorf("Could fetch upload object again although we deleted it")
t.Error("Could fetch upload object again although we deleted it")
}
}
@@ -176,25 +177,25 @@ func TestDboperation(t *testing.T) {
// set ts
ts, err := time.Parse(timeformat, tt.ts)
if err != nil {
t.Errorf("Could not parse time: " + err.Error())
t.Error("Could not parse time: " + err.Error())
}
tt.form.Created = common.Timestamp{Time: ts}
// create new form db object
err = db.Insert(tt.id, tt.form)
if err != nil {
t.Errorf("Could not insert new form object: " + err.Error())
t.Error("Could not insert new form object: " + err.Error())
}
// fetch it
response, err := db.Get(tt.context, tt.id, common.TypeForm)
if err != nil {
t.Errorf("Could not fetch form object: " + err.Error())
t.Error("Could not fetch form object: " + err.Error())
}
// is it there?
if len(response.Forms) != 1 {
t.Errorf("db.Get() did not return an form obj")
t.Error("db.Get() did not return an form obj")
}
// compare times
@@ -213,24 +214,24 @@ func TestDboperation(t *testing.T) {
// fetch list
response, err = db.List(tt.context, tt.filter, tt.query, common.TypeForm)
if err != nil {
t.Errorf("Could not fetch forms list: " + err.Error())
t.Error("Could not fetch forms list: " + err.Error())
}
// is it there?
if len(response.Forms) != 1 {
t.Errorf("db.FormsList() did not return form obj[s]")
t.Error("db.FormsList() did not return form obj[s]")
}
// delete
err = db.Delete(tt.context, tt.id)
if err != nil {
t.Errorf("Could not delete form obj: " + err.Error())
t.Error("Could not delete form obj: " + err.Error())
}
// fetch again, shall return empty
_, err = db.Get(tt.context, tt.id, common.TypeForm)
if err == nil {
t.Errorf("Could fetch form object again although we deleted it")
t.Error("Could fetch form object again although we deleted it")
}
}
})

View File

@@ -18,10 +18,11 @@ package api
import (
"fmt"
"github.com/gofiber/fiber/v2"
"time"
"codeberg.org/scip/ephemerup/cfg"
"codeberg.org/scip/ephemerup/common"
"time"
"github.com/gofiber/fiber/v2"
)
const ApiVersion string = "/v1"
@@ -47,17 +48,17 @@ func Ts() string {
}
/*
Retrieve the API Context name from the session, assuming is has
been successfully authenticated. However, if there are no api
contexts defined, we'll use 'default' (set in
auth.validateAPIKey()).
Retrieve the API Context name from the session, assuming is has
been successfully authenticated. However, if there are no api
contexts defined, we'll use 'default' (set in
auth.validateAPIKey()).
If there's no apicontext in the session, assume unauth user, return ""
If there's no apicontext in the session, assume unauth user, return ""
*/
func SessionGetApicontext(c *fiber.Ctx) (string, error) {
sess, err := Sessionstore.Get(c)
if err != nil {
return "", fmt.Errorf("Unable to initialize session store from context: " + err.Error())
return "", fmt.Errorf("Unable to initialize session store from context: %s", err.Error())
}
apicontext := sess.Get("apicontext")
@@ -69,14 +70,14 @@ func SessionGetApicontext(c *fiber.Ctx) (string, error) {
}
/*
Retrieve the formid (aka onetime api key) from the session. It is
configured if an upload request has been successfully authenticated
using a onetime key.
Retrieve the formid (aka onetime api key) from the session. It is
configured if an upload request has been successfully authenticated
using a onetime key.
*/
func SessionGetFormId(c *fiber.Ctx) (string, error) {
sess, err := Sessionstore.Get(c)
if err != nil {
return "", fmt.Errorf("Unable to initialize session store from context: " + err.Error())
return "", fmt.Errorf("Unable to initialize session store from context: %s", err.Error())
}
formid := sess.Get("formid")
@@ -88,12 +89,13 @@ func SessionGetFormId(c *fiber.Ctx) (string, error) {
}
/*
Calculate if time is up based on start time.Time and
duration. Returns true if time is expired. Start time comes from
the database.
Calculate if time is up based on start time.Time and
duration. Returns true if time is expired. Start time comes from
the database.
aka:
if(now - start) >= duration { time is up}
if(now - start) >= duration { time is up}
*/
func IsExpired(conf *cfg.Config, start time.Time, duration string) bool {
var expiretime int // seconds