cacert-boardvoting/internal/models/motions_test.go
Jan Dittberner a1a66b7245 Finish new motion create implementation
- rename config block mail_server to mail_config
- rename smtp server settings
- move mail notification settings to mail_config
- improve navigation templates
- prepare routes for user management
2022-05-26 15:27:25 +02:00

111 lines
2.6 KiB
Go

/*
Copyright 2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package models_test
import (
"context"
"database/sql"
"log"
"os"
"path"
"testing"
"time"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"git.cacert.org/cacert-boardvoting/internal"
"git.cacert.org/cacert-boardvoting/internal/models"
)
func prepareTestDb(t *testing.T) (*sqlx.DB, *log.Logger) {
t.Helper()
testDir := t.TempDir()
db, err := sql.Open("sqlite3", path.Join(testDir, "test.sqlite"))
require.NoError(t, err)
dbx := sqlx.NewDb(db, "sqlite3")
logger := log.New(os.Stdout, "", log.LstdFlags)
err = internal.InitializeDb(dbx.DB, logger)
require.NoError(t, err)
return dbx, logger
}
func TestDecisionModel_Create(t *testing.T) {
dbx, logger := prepareTestDb(t)
dm := models.MotionModel{DB: dbx, InfoLog: logger}
v := &models.User{
ID: 1, // sqlite does not check referential integrity. Might fail with a foreign key index.
Name: "test voter",
Reminder: "test+voter@example.com",
}
id, err := dm.Create(
context.Background(),
v,
models.VoteTypeMotion,
"test motion",
"I move that we should test more",
time.Now(),
time.Now().AddDate(0, 0, 7),
)
assert.NoError(t, err)
assert.NotEmpty(t, id)
}
func TestDecisionModel_GetNextPendingDecisionDue(t *testing.T) {
dbx, logger := prepareTestDb(t)
dm := models.MotionModel{DB: dbx, InfoLog: logger}
var (
nextDue *time.Time
err error
)
nextDue, err = dm.NextPendingDecisionDue(context.Background())
assert.NoError(t, err)
assert.Empty(t, nextDue)
v := &models.User{
ID: 1, // sqlite does not check referential integrity. Might fail with a foreign key index.
Name: "test voter",
Reminder: "test+voter@example.com",
}
due := time.Now().Add(10 * time.Minute)
ctx := context.Background()
_, err = dm.Create(ctx, v, models.VoteTypeMotion, "test motion", "I move that we should test more", time.Now(), due)
require.NoError(t, err)
nextDue, err = dm.NextPendingDecisionDue(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, nextDue)
assert.Equal(t, due.UTC(), *nextDue)
}