aboutsummaryrefslogtreecommitdiff
path: root/application_context_test.go
blob: 11d1ed466b33ff2b8ccf0f2edad5c2099836ac2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * This file is part of caronte (https://github.com/eciavatta/caronte).
 * Copyright (c) 2020 Emiliano Ciavatta.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestCreateApplicationContext(t *testing.T) {
	wrapper := NewTestStorageWrapper(t)
	wrapper.AddCollection(Settings)

	appContext, err := CreateApplicationContext(wrapper.Storage, "test")
	assert.NoError(t, err)
	assert.False(t, appContext.IsConfigured)
	assert.Zero(t, appContext.Config)
	assert.Len(t, appContext.Accounts, 0)
	assert.Nil(t, appContext.PcapImporter)
	assert.Nil(t, appContext.RulesManager)

	notificationController := NewNotificationController(appContext)
	appContext.SetNotificationController(notificationController)
	assert.Equal(t, notificationController, appContext.NotificationController)

	config := Config{
		ServerAddress: "10.10.10.10",
		FlagRegex:     "FLAG{test}",
		AuthRequired:  true,
	}
	accounts := gin.Accounts{
		"username": "password",
	}
	appContext.SetConfig(config)
	appContext.SetAccounts(accounts)
	assert.Equal(t, appContext.Config, config)
	assert.Equal(t, appContext.Accounts, accounts)
	assert.NotNil(t, appContext.PcapImporter)
	assert.NotNil(t, appContext.RulesManager)
	assert.True(t, appContext.IsConfigured)

	config.FlagRegex = "FLAG{test2}"
	accounts["username"] = "password2"
	appContext.SetConfig(config)
	appContext.SetAccounts(accounts)

	checkAppContext, err := CreateApplicationContext(wrapper.Storage, "test")
	assert.NoError(t, err)
	checkAppContext.SetNotificationController(notificationController)
	checkAppContext.Configure()
	assert.True(t, checkAppContext.IsConfigured)
	assert.Equal(t, checkAppContext.Config, config)
	assert.Equal(t, checkAppContext.Accounts, accounts)
	assert.NotNil(t, checkAppContext.PcapImporter)
	assert.NotNil(t, checkAppContext.RulesManager)
	assert.Equal(t, notificationController, appContext.NotificationController)

	wrapper.Destroy(t)
}