2019-04-25 04:52:08 +07:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-18 05:48:48 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-25 04:52:08 +07:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/authelia/authelia/internal/authentication"
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldInitializerSession(t *testing.T) {
|
|
|
|
ctx := &fasthttp.RequestCtx{}
|
|
|
|
configuration := schema.SessionConfiguration{}
|
2020-05-02 23:20:40 +07:00
|
|
|
configuration.Domain = testDomain
|
|
|
|
configuration.Name = testName
|
|
|
|
configuration.Expiration = testExpiration
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
provider := NewProvider(configuration)
|
2020-01-18 05:48:48 +07:00
|
|
|
session, err := provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
assert.Equal(t, NewDefaultUserSession(), session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldUpdateSession(t *testing.T) {
|
|
|
|
ctx := &fasthttp.RequestCtx{}
|
|
|
|
configuration := schema.SessionConfiguration{}
|
2020-05-02 23:20:40 +07:00
|
|
|
configuration.Domain = testDomain
|
|
|
|
configuration.Name = testName
|
|
|
|
configuration.Expiration = testExpiration
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
provider := NewProvider(configuration)
|
|
|
|
session, _ := provider.GetSession(ctx)
|
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
session.Username = testUsername
|
2019-04-25 04:52:08 +07:00
|
|
|
session.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
|
2020-01-18 05:48:48 +07:00
|
|
|
err := provider.SaveSession(ctx, session)
|
|
|
|
require.NoError(t, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-01-18 05:48:48 +07:00
|
|
|
session, err = provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
assert.Equal(t, UserSession{
|
2020-05-02 23:20:40 +07:00
|
|
|
Username: testUsername,
|
2019-04-25 04:52:08 +07:00
|
|
|
AuthenticationLevel: authentication.TwoFactor,
|
|
|
|
}, session)
|
|
|
|
}
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
func TestShouldDestroySessionAndWipeSessionData(t *testing.T) {
|
|
|
|
ctx := &fasthttp.RequestCtx{}
|
|
|
|
configuration := schema.SessionConfiguration{}
|
2020-05-02 23:20:40 +07:00
|
|
|
configuration.Domain = testDomain
|
|
|
|
configuration.Name = testName
|
|
|
|
configuration.Expiration = testExpiration
|
2020-01-18 05:48:48 +07:00
|
|
|
|
|
|
|
provider := NewProvider(configuration)
|
|
|
|
session, err := provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
session.Username = testUsername
|
2020-01-18 05:48:48 +07:00
|
|
|
session.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
|
|
|
|
err = provider.SaveSession(ctx, session)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newUserSession, err := provider.GetSession(ctx)
|
|
|
|
require.NoError(t, err)
|
2020-05-02 23:20:40 +07:00
|
|
|
assert.Equal(t, testUsername, newUserSession.Username)
|
2020-01-18 05:48:48 +07:00
|
|
|
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)
|
|
|
|
}
|