Testing Api with Session gives error


#1

App initialization

func (app *App) Initialize(config *config.Config) {
	app.Echo = echo.New()

	app.Echo.Use(middleware.Logger())
	app.Echo.Use(middleware.Recover())
	app.Echo.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))

	app.migrateDB()
	app.registerRoutes()
}

This code

const sessionName = "session"

func (ss SessionService) Get (key string, c echo.Context) (interface{}, bool) {
  	sess, _ := session.Get(sessionName, c)
}

gives error “panic: interface conversion: interface is nil, not sessions.Store”, while regular api works without any errors, connected with sessions.
Any ideas?

Test code

package tests

import (
	"testing"
	"github.com/labstack/echo"
	"net/http/httptest"
	"net/url"
	"fmt"
	"github.com/MetalRex101/auth-server/app/controllers"
	"github.com/stretchr/testify/assert"
	"net/http"
	"github.com/labstack/echo-contrib/session"
	"github.com/gorilla/sessions"
)

func TestAuthorizeClient(t *testing.T) {
	q := make(url.Values)
	q.Set("client_id", "32131231")
	q.Set("password", "dmsladmskdl")
	q.Set("redirect_uri", "url")
	q.Set("email", "[email protected]")

	e := echo.New()
	e.Debug = true
	e.Use(session.Middleware(sessions.NewFilesystemStore("../storage/sessions")))

	req := httptest.NewRequest(echo.GET, fmt.Sprintf("/authorize?%s", q.Encode()), nil)
	rec := httptest.NewRecorder()

	c := e.NewContext(req, rec)

	// Assertions
	if assert.NoError(t, controllers.Oauth.AuthorizeClient(c)) {
		assert.Equal(t, http.StatusOK, rec.Code)
		assert.Contains(t, "Code", rec.Body)
	}
}