Skip to content

Commit

Permalink
Fix init of secret key
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli committed Oct 31, 2024
1 parent aaa5204 commit ef6ccdc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 45 deletions.
2 changes: 1 addition & 1 deletion internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type Server struct {
func NewServer(isDev bool, sessionsPath string) *Server {
dev = isDev
flashStore = sessions.NewCookieStore([]byte("opengist"))
encryptKey, _ := utils.GenerateSecretKey(filepath.Join(config.GetHomeDir(), "sessions", "session-encrypt.key"))
encryptKey, _ := utils.GenerateSecretKey(filepath.Join(sessionsPath, "session-encrypt.key"))
userStore = sessions.NewFilesystemStore(sessionsPath, config.SecretKey, encryptKey)
userStore.MaxLength(10 * 1024)
gothic.Store = userStore
Expand Down
24 changes: 8 additions & 16 deletions internal/web/test/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import (
)

func TestRegister(t *testing.T) {
setup(t)
s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")
s := setup(t)
defer teardown(t, s)

err = s.request("GET", "/", nil, 302)
err := s.request("GET", "/", nil, 302)
require.NoError(t, err)

err = s.request("GET", "/register", nil, 200)
Expand Down Expand Up @@ -55,12 +53,10 @@ func TestRegister(t *testing.T) {
}

func TestLogin(t *testing.T) {
setup(t)
s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")
s := setup(t)
defer teardown(t, s)

err = s.request("GET", "/login", nil, 200)
err := s.request("GET", "/login", nil, 200)
require.NoError(t, err)

user1 := db.UserDTO{Username: "thomas", Password: "thomas"}
Expand Down Expand Up @@ -101,15 +97,13 @@ type settingSet struct {
}

func TestAnonymous(t *testing.T) {
setup(t)
s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")
s := setup(t)
defer teardown(t, s)

user := db.UserDTO{Username: "thomas", Password: "azeaze"}
register(t, s, user)

err = s.request("PUT", "/admin-panel/set-config", settingSet{"require-login", "1"}, 200)
err := s.request("PUT", "/admin-panel/set-config", settingSet{"require-login", "1"}, 200)
require.NoError(t, err)

gist1 := db.GistDTO{
Expand Down Expand Up @@ -154,9 +148,7 @@ func TestAnonymous(t *testing.T) {
}

func TestGitOperations(t *testing.T) {
setup(t)
s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")
s := setup(t)
defer teardown(t, s)

admin := db.UserDTO{Username: "thomas", Password: "thomas"}
Expand All @@ -178,7 +170,7 @@ func TestGitOperations(t *testing.T) {
"yeah",
},
}
err = s.request("POST", "/", gist1, 302)
err := s.request("POST", "/", gist1, 302)
require.NoError(t, err)

gist2 := db.GistDTO{
Expand Down
24 changes: 8 additions & 16 deletions internal/web/test/gist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
)

func TestGists(t *testing.T) {
setup(t)
s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")
s := setup(t)
defer teardown(t, s)

err = s.request("GET", "/", nil, 302)
err := s.request("GET", "/", nil, 302)
require.NoError(t, err)

user1 := db.UserDTO{Username: "thomas", Password: "thomas"}
Expand Down Expand Up @@ -106,9 +104,7 @@ func TestGists(t *testing.T) {
}

func TestVisibility(t *testing.T) {
setup(t)
s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")
s := setup(t)
defer teardown(t, s)

user1 := db.UserDTO{Username: "thomas", Password: "thomas"}
Expand All @@ -123,7 +119,7 @@ func TestVisibility(t *testing.T) {
Name: []string{""},
Content: []string{"yeah"},
}
err = s.request("POST", "/", gist1, 302)
err := s.request("POST", "/", gist1, 302)
require.NoError(t, err)

gist1db, err := db.GetGistByID("1")
Expand All @@ -150,9 +146,7 @@ func TestVisibility(t *testing.T) {
}

func TestLikeFork(t *testing.T) {
setup(t)
s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")
s := setup(t)
defer teardown(t, s)

user1 := db.UserDTO{Username: "thomas", Password: "thomas"}
Expand All @@ -167,7 +161,7 @@ func TestLikeFork(t *testing.T) {
Name: []string{""},
Content: []string{"yeah"},
}
err = s.request("POST", "/", gist1, 302)
err := s.request("POST", "/", gist1, 302)
require.NoError(t, err)

s.sessionCookie = ""
Expand Down Expand Up @@ -211,9 +205,7 @@ func TestLikeFork(t *testing.T) {
}

func TestCustomUrl(t *testing.T) {
setup(t)
s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")
s := setup(t)
defer teardown(t, s)

user1 := db.UserDTO{Username: "thomas", Password: "thomas"}
Expand All @@ -229,7 +221,7 @@ func TestCustomUrl(t *testing.T) {
Name: []string{"gist1.txt", "gist2.txt", "gist3.txt"},
Content: []string{"yeah", "yeah\ncool", "yeah\ncool gist actually"},
}
err = s.request("POST", "/", gist1, 302)
err := s.request("POST", "/", gist1, 302)
require.NoError(t, err)

gist1db, err := db.GetGistByID("1")
Expand Down
29 changes: 17 additions & 12 deletions internal/web/test/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func structToURLValues(s interface{}) url.Values {
return v
}

func setup(t *testing.T) {
func setup(t *testing.T) *testServer {
var databaseDsn string
databaseType = os.Getenv("OPENGIST_TEST_DB")
switch databaseType {
Expand Down Expand Up @@ -182,6 +182,11 @@ func setup(t *testing.T) {

// err = index.Open(filepath.Join(homePath, "testsindex", "opengist.index"))
// require.NoError(t, err, "Could not open index")

s, err := newTestServer()
require.NoError(t, err, "Failed to create test server")

return s
}

func teardown(t *testing.T, s *testServer) {
Expand All @@ -190,17 +195,17 @@ func teardown(t *testing.T, s *testServer) {
//err := db.Close()
//require.NoError(t, err, "Could not close database")

err := os.RemoveAll(path.Join(config.GetHomeDir(), "tests"))
require.NoError(t, err, "Could not remove repos directory")

err = os.RemoveAll(path.Join(config.GetHomeDir(), "tmp", "repos"))
require.NoError(t, err, "Could not remove repos directory")

err = os.RemoveAll(path.Join(config.GetHomeDir(), "tmp", "sessions"))
require.NoError(t, err, "Could not remove repos directory")

err = db.TruncateDatabase()
require.NoError(t, err, "Could not truncate database")
//err := os.RemoveAll(path.Join(config.GetHomeDir(), "tests"))
//require.NoError(t, err, "Could not remove repos directory")
//
//err = os.RemoveAll(path.Join(config.GetHomeDir(), "tmp", "repos"))
//require.NoError(t, err, "Could not remove repos directory")
//
//err = os.RemoveAll(path.Join(config.GetHomeDir(), "tmp", "sessions"))
//require.NoError(t, err, "Could not remove repos directory")
//
//err = db.TruncateDatabase()
//require.NoError(t, err, "Could not truncate database")

// err = os.RemoveAll(path.Join(config.C.OpengistHome, "testsindex"))
// require.NoError(t, err, "Could not remove repos directory")
Expand Down

0 comments on commit ef6ccdc

Please sign in to comment.