2019-04-24 21:52:08 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/authentication"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-17 22:48:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldInitializerSession(t *testing.T) {
|
|
|
|
ctx := &fasthttp.RequestCtx{}
|
|
|
|
configuration := schema.SessionConfiguration{}
|
|
|
|
configuration.Domain = "example.com"
|
|
|
|
configuration.Name = "my_session"
|
2020-04-03 23:11:33 +00:00
|
|
|
// TODO(james-d-elliott): Convert to duration notation
|
2019-04-24 21:52:08 +00:00
|
|
|
configuration.Expiration = 40
|
|
|
|
|
|
|
|
provider := NewProvider(configuration)
|
2020-01-17 22:48:48 +00:00
|
|
|
session, err := provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
assert.Equal(t, NewDefaultUserSession(), session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldUpdateSession(t *testing.T) {
|
|
|
|
ctx := &fasthttp.RequestCtx{}
|
|
|
|
configuration := schema.SessionConfiguration{}
|
|
|
|
configuration.Domain = "example.com"
|
|
|
|
configuration.Name = "my_session"
|
2020-04-03 23:11:33 +00:00
|
|
|
// TODO(james-d-elliott): Convert to duration notation
|
2019-04-24 21:52:08 +00:00
|
|
|
configuration.Expiration = 40
|
|
|
|
|
|
|
|
provider := NewProvider(configuration)
|
|
|
|
session, _ := provider.GetSession(ctx)
|
|
|
|
|
|
|
|
session.Username = "john"
|
|
|
|
session.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
|
2020-01-17 22:48:48 +00:00
|
|
|
err := provider.SaveSession(ctx, session)
|
|
|
|
require.NoError(t, err)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-17 22:48:48 +00:00
|
|
|
session, err = provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
assert.Equal(t, UserSession{
|
|
|
|
Username: "john",
|
|
|
|
AuthenticationLevel: authentication.TwoFactor,
|
|
|
|
}, session)
|
|
|
|
}
|
2020-01-17 22:48:48 +00:00
|
|
|
|
|
|
|
func TestShouldDestroySessionAndWipeSessionData(t *testing.T) {
|
|
|
|
ctx := &fasthttp.RequestCtx{}
|
|
|
|
configuration := schema.SessionConfiguration{}
|
|
|
|
configuration.Domain = "example.com"
|
|
|
|
configuration.Name = "my_session"
|
2020-04-03 23:11:33 +00:00
|
|
|
// TODO(james-d-elliott): Convert to duration notation
|
2020-01-17 22:48:48 +00:00
|
|
|
configuration.Expiration = 40
|
|
|
|
|
|
|
|
provider := NewProvider(configuration)
|
|
|
|
session, err := provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
session.Username = "john"
|
|
|
|
session.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
|
|
|
|
err = provider.SaveSession(ctx, session)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newUserSession, err := provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "john", newUserSession.Username)
|
|
|
|
assert.Equal(t, authentication.TwoFactor, newUserSession.AuthenticationLevel)
|
|
|
|
|
|
|
|
err = provider.DestroySession(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newUserSession, err = provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "", newUserSession.Username)
|
|
|
|
assert.Equal(t, authentication.NotAuthenticated, newUserSession.AuthenticationLevel)
|
|
|
|
}
|