github.com/merlinepedra/gopphish-attack@v0.9.0/config/config_test.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "os" 7 "testing" 8 9 "github.com/stretchr/testify/suite" 10 ) 11 12 type ConfigSuite struct { 13 suite.Suite 14 ConfigFile *os.File 15 } 16 17 var validConfig = []byte(`{ 18 "admin_server": { 19 "listen_url": "127.0.0.1:3333", 20 "use_tls": true, 21 "cert_path": "gophish_admin.crt", 22 "key_path": "gophish_admin.key" 23 }, 24 "phish_server": { 25 "listen_url": "0.0.0.0:8080", 26 "use_tls": false, 27 "cert_path": "example.crt", 28 "key_path": "example.key" 29 }, 30 "db_name": "sqlite3", 31 "db_path": "gophish.db", 32 "migrations_prefix": "db/db_", 33 "contact_address": "" 34 }`) 35 36 func (s *ConfigSuite) SetupTest() { 37 f, err := ioutil.TempFile("", "gophish-config") 38 s.Nil(err) 39 s.ConfigFile = f 40 } 41 42 func (s *ConfigSuite) TearDownTest() { 43 err := s.ConfigFile.Close() 44 s.Nil(err) 45 } 46 47 func (s *ConfigSuite) TestLoadConfig() { 48 _, err := s.ConfigFile.Write(validConfig) 49 s.Nil(err) 50 // Load the valid config 51 conf, err := LoadConfig(s.ConfigFile.Name()) 52 s.Nil(err) 53 54 expectedConfig := &Config{} 55 err = json.Unmarshal(validConfig, &expectedConfig) 56 s.Nil(err) 57 expectedConfig.MigrationsPath = expectedConfig.MigrationsPath + expectedConfig.DBName 58 expectedConfig.TestFlag = false 59 s.Equal(expectedConfig, conf) 60 61 // Load an invalid config 62 conf, err = LoadConfig("bogusfile") 63 s.NotNil(err) 64 } 65 66 func TestConfigSuite(t *testing.T) { 67 suite.Run(t, new(ConfigSuite)) 68 }