more tests and db fixes

This commit is contained in:
2023-03-26 13:29:33 +02:00
parent 3964ffc7cb
commit 0af31bb0d9
10 changed files with 205 additions and 28 deletions

View File

@@ -42,7 +42,7 @@ func DeleteExpiredUploads(conf *cfg.Config, db *Db) error {
return fmt.Errorf("unable to unmarshal json: %s", err)
}
if IsExpired(conf, upload.Uploaded.Time, upload.Expire) {
if IsExpired(conf, upload.Created.Time, upload.Expire) {
if err := bucket.Delete([]byte(id)); err != nil {
return nil
}

View File

@@ -102,7 +102,7 @@ func (db *Db) Delete(apicontext string, id string) error {
return err
}
func (db *Db) UploadsList(apicontext string, filter string, t int) (*common.Response, error) {
func (db *Db) List(apicontext string, filter string, t int) (*common.Response, error) {
response := &common.Response{}
err := db.bolt.View(func(tx *bolt.Tx) error {
@@ -119,12 +119,12 @@ func (db *Db) UploadsList(apicontext string, filter string, t int) (*common.Resp
var entryContext string
if t == common.TypeUpload {
entryContext = entry.(common.Upload).Context
entryContext = entry.(*common.Upload).Context
} else {
entryContext = entry.(common.Form).Context
entryContext = entry.(*common.Form).Context
}
fmt.Printf("apicontext: %s, filter: %s\n", apicontext, filter)
//fmt.Printf("apicontext: %s, filter: %s\n", apicontext, filter)
if apicontext != "" && db.cfg.Super != apicontext {
// only return the uploads for this context
if apicontext == entryContext {
@@ -150,6 +150,7 @@ func (db *Db) UploadsList(apicontext string, filter string, t int) (*common.Resp
}
// we only return one obj here, but could return more later
// FIXME: turn the id into a filter and call (Uploads|Forms)List(), same code!
func (db *Db) Get(apicontext string, id string, t int) (*common.Response, error) {
response := &common.Response{}

View File

@@ -18,9 +18,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package api
import (
//"github.com/alecthomas/repr"
"github.com/maxatome/go-testdeep/td"
"github.com/tlinden/cenophane/cfg"
"github.com/tlinden/cenophane/common"
"os"
"testing"
"time"
)
func finalize(db *Db) {
@@ -35,7 +39,7 @@ func finalize(db *Db) {
func TestNew(t *testing.T) {
var tests = []struct {
name string
file string
dbfile string
wantfail bool
}{
{"opennew", "test.db", false},
@@ -43,7 +47,7 @@ func TestNew(t *testing.T) {
}
for _, tt := range tests {
c := &cfg.Config{DbFile: tt.file}
c := &cfg.Config{DbFile: tt.dbfile}
t.Run(tt.name, func(t *testing.T) {
db, err := NewDb(c)
defer finalize(db)
@@ -57,3 +61,171 @@ func TestNew(t *testing.T) {
})
}
}
const timeformat string = "2006-01-02T15:04:05.000Z"
var dbtests = []struct {
name string
dbfile string
wantfail bool
id string
context string
ts string
filter string
upload common.Upload
form common.Form
}{
{
"upload", "test.db", false, "1", "foo",
"2023-03-10T11:45:00.000Z", "",
common.Upload{
Id: "1", Expire: "asap", File: "none", Context: "foo",
Created: common.Timestamp{}},
common.Form{},
},
{
"form", "test.db", false, "2", "foo",
"2023-03-10T11:45:00.000Z", "",
common.Upload{},
common.Form{
Id: "1", Expire: "asap", Description: "none", Context: "foo",
Created: common.Timestamp{}},
},
}
/*
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 {
c := &cfg.Config{DbFile: tt.dbfile}
t.Run(tt.name, func(t *testing.T) {
// create new bbolt db
db, err := NewDb(c)
defer finalize(db)
if err != nil {
t.Errorf("Could not open new DB: " + err.Error())
}
if tt.upload.Id != "" {
// set ts
ts, err := time.Parse(timeformat, tt.ts)
tt.upload.Created = common.Timestamp{Time: ts}
// 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())
}
// fetch it
response, err := db.Get(tt.context, tt.id, common.TypeUpload)
if err != nil {
t.Errorf("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")
}
// compare times
if !tt.upload.Created.Time.Equal(response.Uploads[0].Created.Time) {
t.Errorf("Timestamps don't match!\ngot: %s\nexp: %s\n",
response.Uploads[0].Created, tt.upload.Created)
}
// equal them artificially, because otherwise td will
// fail because of time.Time.wall+ext, or TZ is missing
response.Uploads[0].Created = tt.upload.Created
// compare
td.Cmp(t, response.Uploads[0], &tt.upload, tt.name)
// fetch list
response, err = db.List(tt.context, tt.filter, common.TypeUpload)
if err != nil {
t.Errorf("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]")
}
// delete
err = db.Delete(tt.context, tt.id)
if err != nil {
t.Errorf("Could not delete upload obj: " + err.Error())
}
// fetch again, shall return empty
response, err = db.Get(tt.context, tt.id, common.TypeUpload)
if err == nil {
t.Errorf("Could fetch upload object again although we deleted it")
}
}
if tt.form.Id != "" {
// set ts
ts, err := time.Parse(timeformat, tt.ts)
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())
}
// fetch it
response, err := db.Get(tt.context, tt.id, common.TypeForm)
if err != nil {
t.Errorf("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")
}
// compare times
if !tt.form.Created.Time.Equal(response.Forms[0].Created.Time) {
t.Errorf("Timestamps don't match!\ngot: %s\nexp: %s\n",
response.Forms[0].Created, tt.form.Created)
}
// equal them artificially, because otherwise td will
// fail because of time.Time.wall+ext, or TZ is missing
response.Forms[0].Created = tt.form.Created
// compare
td.Cmp(t, response.Forms[0], &tt.form, tt.name)
// fetch list
response, err = db.List(tt.context, tt.filter, common.TypeForm)
if err != nil {
t.Errorf("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]")
}
// delete
err = db.Delete(tt.context, tt.id)
if err != nil {
t.Errorf("Could not delete form obj: " + err.Error())
}
// fetch again, shall return empty
response, err = db.Get(tt.context, tt.id, common.TypeForm)
if err == nil {
t.Errorf("Could fetch form object again although we deleted it")
}
}
})
}
}

View File

@@ -62,7 +62,7 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
}
// init upload obj
entry := &common.Upload{Id: id, Uploaded: common.Timestamp{Time: time.Now()}}
entry := &common.Upload{Id: id, Created: common.Timestamp{Time: time.Now()}}
// retrieve the API Context name from the session
apicontext, err := GetApicontext(c)
@@ -233,7 +233,7 @@ func UploadsList(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
}
// get list
uploads, err := db.UploadsList(apicontext, filter, common.TypeUpload)
uploads, err := db.List(apicontext, filter, common.TypeUpload)
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Unable to list uploads: "+err.Error())