Skip to content

Commit b0699da

Browse files
committed
Add nilaway and rework test setup
1 parent e657884 commit b0699da

File tree

15 files changed

+307
-182
lines changed

15 files changed

+307
-182
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ test:
2929
errcheck -exclude errcheck_excludes.txt ./...
3030
gocritic check -disable='#experimental,#opinionated' [email protected] 3 ./...
3131
revive -set_exit_status -exclude ./docs ./...
32+
nilaway ./...
3233
go test -v -cover ./...
3334
gosec -exclude-dir=tests ./...
3435
govulncheck ./...
@@ -43,6 +44,7 @@ setup:
4344
go install github.com/mgechev/revive@latest
4445
go install github.com/securego/gosec/v2/cmd/gosec@latest
4546
go install github.com/swaggo/swag/cmd/swag@latest
47+
go install go.uber.org/nilaway/cmd/nilaway@latest
4648
go install golang.org/x/vuln/cmd/govulncheck@latest
4749
go install honnef.co/go/tools/cmd/staticcheck@latest
4850
go install mvdan.cc/gofumpt@latest

cmd/pushbits/main.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ func setupCleanup(db *database.Database, dp *dispatcher.Dispatcher) {
2929
}()
3030
}
3131

32+
func printStarupMessage() {
33+
if len(version) == 0 {
34+
log.L.Panic("Version not set")
35+
} else {
36+
log.L.Printf("Starting PushBits %s", version)
37+
}
38+
}
39+
3240
// @title PushBits Server API Documentation
3341
// @version 0.10.5
3442
// @description Documentation for the PushBits server API.
@@ -45,11 +53,7 @@ func setupCleanup(db *database.Database, dp *dispatcher.Dispatcher) {
4553

4654
// @securityDefinitions.basic BasicAuth
4755
func main() {
48-
if len(version) == 0 {
49-
log.L.Panic("Version not set")
50-
} else {
51-
log.L.Printf("Starting PushBits %s", version)
52-
}
56+
printStarupMessage()
5357

5458
c := configuration.Get()
5559

@@ -63,6 +67,11 @@ func main() {
6367
db, err := database.Create(cm, c.Database.Dialect, c.Database.Connection)
6468
if err != nil {
6569
log.L.Fatal(err)
70+
return
71+
}
72+
if db == nil {
73+
log.L.Fatal("db is nil but error was nil")
74+
return
6675
}
6776
defer db.Close()
6877

@@ -73,6 +82,11 @@ func main() {
7382
dp, err := dispatcher.Create(c.Matrix.Homeserver, c.Matrix.Username, c.Matrix.Password, c.Formatting)
7483
if err != nil {
7584
log.L.Fatal(err)
85+
return
86+
}
87+
if dp == nil {
88+
log.L.Fatal("dp is nil but error was nil")
89+
return
7690
}
7791
defer dp.Close()
7892

@@ -81,15 +95,18 @@ func main() {
8195
err = db.RepairChannels(dp, &c.RepairBehavior)
8296
if err != nil {
8397
log.L.Fatal(err)
98+
return
8499
}
85100

86101
engine, err := router.Create(c.Debug, c.HTTP.TrustedProxies, cm, db, dp, &c.Alertmanager)
87102
if err != nil {
88103
log.L.Fatal(err)
104+
return
89105
}
90106

91107
err = runner.Run(engine, c)
92108
if err != nil {
93109
log.L.Fatal(err)
110+
return
94111
}
95112
}

internal/api/alertmanager/handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type HandlerSettings struct {
3838
// @Router /alert [post]
3939
func (h *Handler) CreateAlert(ctx *gin.Context) {
4040
application := authentication.GetApplication(ctx)
41+
if application == nil {
42+
return
43+
}
44+
4145
log.L.Printf("Sending alert notification for application %s.", application.Name)
4246

4347
var hook model.AlertmanagerWebhook

internal/api/api_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/gin-gonic/gin"
9+
"github.com/pushbits/server/internal/authentication/credentials"
10+
"github.com/pushbits/server/internal/configuration"
11+
"github.com/pushbits/server/internal/database"
12+
"github.com/pushbits/server/internal/log"
13+
"github.com/pushbits/server/internal/model"
14+
"github.com/pushbits/server/tests/mockups"
15+
)
16+
17+
// TestContext holds all test-related objects
18+
type TestContext struct {
19+
ApplicationHandler *ApplicationHandler
20+
Users []*model.User
21+
Database *database.Database
22+
NotificationHandler *NotificationHandler
23+
UserHandler *UserHandler
24+
Config *configuration.Configuration
25+
}
26+
27+
var GlobalTestContext *TestContext
28+
29+
func cleanup() {
30+
err := os.Remove("pushbits-test.db")
31+
if err != nil {
32+
log.L.Warnln("Cannot delete test database: ", err)
33+
}
34+
}
35+
36+
func TestMain(m *testing.M) {
37+
cleanup()
38+
39+
gin.SetMode(gin.TestMode)
40+
41+
GlobalTestContext = CreateTestContext(nil)
42+
43+
m.Run()
44+
45+
cleanup()
46+
}
47+
48+
// GetTestContext initializes and verifies all required test components
49+
func GetTestContext(_ *testing.T) *TestContext {
50+
if GlobalTestContext == nil {
51+
GlobalTestContext = CreateTestContext(nil)
52+
}
53+
54+
return GlobalTestContext
55+
}
56+
57+
// CreateTestContext initializes and verifies all required test components
58+
func CreateTestContext(_ *testing.T) *TestContext {
59+
ctx := &TestContext{}
60+
61+
config := configuration.Configuration{}
62+
config.Database.Connection = "pushbits-test.db"
63+
config.Database.Dialect = "sqlite3"
64+
config.Crypto.Argon2.Iterations = 4
65+
config.Crypto.Argon2.Parallelism = 4
66+
config.Crypto.Argon2.Memory = 131072
67+
config.Crypto.Argon2.SaltLength = 16
68+
config.Crypto.Argon2.KeyLength = 32
69+
config.Admin.Name = "user"
70+
config.Admin.Password = "pushbits"
71+
ctx.Config = &config
72+
73+
db, err := mockups.GetEmptyDatabase(ctx.Config.Crypto)
74+
if err != nil {
75+
cleanup()
76+
panic(fmt.Errorf("cannot set up database: %w", err))
77+
}
78+
ctx.Database = db
79+
80+
ctx.ApplicationHandler = &ApplicationHandler{
81+
DB: ctx.Database,
82+
DP: &mockups.MockDispatcher{},
83+
}
84+
85+
ctx.Users = mockups.GetUsers(ctx.Config)
86+
87+
ctx.NotificationHandler = &NotificationHandler{
88+
DB: ctx.Database,
89+
DP: &mockups.MockDispatcher{},
90+
}
91+
92+
ctx.UserHandler = &UserHandler{
93+
AH: ctx.ApplicationHandler,
94+
CM: credentials.CreateManager(false, ctx.Config.Crypto),
95+
DB: ctx.Database,
96+
DP: &mockups.MockDispatcher{},
97+
}
98+
99+
return ctx
100+
}

internal/api/application.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func (h *ApplicationHandler) generateToken(compat bool) string {
2828
}
2929

3030
func (h *ApplicationHandler) registerApplication(ctx *gin.Context, a *model.Application, u *model.User) error {
31+
if a == nil || u == nil {
32+
return errors.New("nil parameters provided")
33+
}
34+
3135
log.L.Printf("Registering application %s.", a.Name)
3236

3337
channelID, err := h.DP.RegisterApplication(a.ID, a.Name, u.MatrixID)
@@ -46,6 +50,10 @@ func (h *ApplicationHandler) registerApplication(ctx *gin.Context, a *model.Appl
4650
}
4751

4852
func (h *ApplicationHandler) createApplication(ctx *gin.Context, u *model.User, name string, compat bool) (*model.Application, error) {
53+
if u == nil {
54+
return nil, errors.New("nil parameters provided")
55+
}
56+
4957
log.L.Printf("Creating application %s.", name)
5058

5159
application := model.Application{}
@@ -71,6 +79,10 @@ func (h *ApplicationHandler) createApplication(ctx *gin.Context, u *model.User,
7179
}
7280

7381
func (h *ApplicationHandler) deleteApplication(ctx *gin.Context, a *model.Application, u *model.User) error {
82+
if a == nil || u == nil {
83+
return errors.New("nil parameters provided")
84+
}
85+
7486
log.L.Printf("Deleting application %s (ID %d).", a.Name, a.ID)
7587

7688
err := h.DP.DeregisterApplication(a, u)
@@ -87,6 +99,10 @@ func (h *ApplicationHandler) deleteApplication(ctx *gin.Context, a *model.Applic
8799
}
88100

89101
func (h *ApplicationHandler) updateApplication(ctx *gin.Context, a *model.Application, updateApplication *model.UpdateApplication) error {
102+
if a == nil || updateApplication == nil {
103+
return errors.New("nil parameters provided")
104+
}
105+
90106
log.L.Printf("Updating application %s (ID %d).", a.Name, a.ID)
91107

92108
if updateApplication.Name != nil {
@@ -186,7 +202,7 @@ func (h *ApplicationHandler) GetApplications(ctx *gin.Context) {
186202
// @Router /application/{id} [get]
187203
func (h *ApplicationHandler) GetApplication(ctx *gin.Context) {
188204
application, err := getApplication(ctx, h.DB)
189-
if err != nil {
205+
if err != nil || application == nil {
190206
return
191207
}
192208

@@ -218,7 +234,7 @@ func (h *ApplicationHandler) GetApplication(ctx *gin.Context) {
218234
// @Router /application/{id} [delete]
219235
func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
220236
application, err := getApplication(ctx, h.DB)
221-
if err != nil {
237+
if err != nil || application == nil {
222238
return
223239
}
224240

@@ -250,7 +266,7 @@ func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
250266
// @Router /application/{id} [put]
251267
func (h *ApplicationHandler) UpdateApplication(ctx *gin.Context) {
252268
application, err := getApplication(ctx, h.DB)
253-
if err != nil {
269+
if err != nil || application == nil {
254270
return
255271
}
256272

0 commit comments

Comments
 (0)