2019-12-07 16:40:42 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-04-05 12:37:21 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
"github.com/authelia/authelia/internal/authorization"
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/mocks"
|
2019-12-07 16:40:42 +00:00
|
|
|
)
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
type SecondFactorAvailableMethodsFixture struct {
|
2019-12-07 16:40:42 +00:00
|
|
|
suite.Suite
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) SetupTest() {
|
2019-12-07 16:40:42 +00:00
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
2020-06-21 13:40:37 +00:00
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
Rules: []schema.ACLRule{},
|
|
|
|
})
|
2019-12-07 16:40:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TearDownTest() {
|
2019-12-07 16:40:42 +00:00
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldServeDefaultMethods() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
|
|
|
},
|
|
|
|
}
|
2020-04-03 23:11:33 +00:00
|
|
|
expectedBody := ConfigurationBody{
|
2020-06-21 13:40:37 +00:00
|
|
|
AvailableMethods: []string{"totp", "u2f"},
|
|
|
|
SecondFactorEnabled: false,
|
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
2020-04-04 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), expectedBody)
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldServeDefaultMethodsAndMobilePush() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
DuoAPI: &schema.DuoAPIConfiguration{},
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
|
|
|
},
|
|
|
|
}
|
2020-04-04 23:28:09 +00:00
|
|
|
expectedBody := ConfigurationBody{
|
2020-06-21 13:40:37 +00:00
|
|
|
AvailableMethods: []string{"totp", "u2f", "mobile_push"},
|
|
|
|
SecondFactorEnabled: false,
|
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
2020-04-03 23:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), expectedBody)
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:40:37 +00:00
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldCheckSecondFactorIsDisabledWhenNoRuleIsSetToTwoFactor() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "bypass",
|
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "deny",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domains: []string{"abc.example.com"},
|
|
|
|
Policy: "single_factor",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domains: []string{"def.example.com"},
|
|
|
|
Policy: "bypass",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), ConfigurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "u2f"},
|
|
|
|
SecondFactorEnabled: false,
|
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldCheckSecondFactorIsEnabledWhenDefaultPolicySetToTwoFactor() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "two_factor",
|
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "deny",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domains: []string{"abc.example.com"},
|
|
|
|
Policy: "single_factor",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domains: []string{"def.example.com"},
|
|
|
|
Policy: "bypass",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), ConfigurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "u2f"},
|
|
|
|
SecondFactorEnabled: true,
|
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SecondFactorAvailableMethodsFixture) TestShouldCheckSecondFactorIsEnabledWhenSomePolicySetToTwoFactor() {
|
|
|
|
s.mock.Ctx.Configuration = schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
|
|
|
Period: schema.DefaultTOTPConfiguration.Period,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.mock.Ctx.Providers.Authorizer = authorization.NewAuthorizer(schema.AccessControlConfiguration{
|
|
|
|
DefaultPolicy: "bypass",
|
|
|
|
Rules: []schema.ACLRule{
|
|
|
|
{
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
Policy: "deny",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domains: []string{"abc.example.com"},
|
|
|
|
Policy: "two_factor",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domains: []string{"def.example.com"},
|
|
|
|
Policy: "bypass",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ConfigurationGet(s.mock.Ctx)
|
|
|
|
s.mock.Assert200OK(s.T(), ConfigurationBody{
|
|
|
|
AvailableMethods: []string{"totp", "u2f"},
|
|
|
|
SecondFactorEnabled: true,
|
|
|
|
TOTPPeriod: schema.DefaultTOTPConfiguration.Period,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunSuite(t *testing.T) {
|
|
|
|
s := new(SecondFactorAvailableMethodsFixture)
|
2020-04-03 23:11:33 +00:00
|
|
|
suite.Run(t, s)
|
|
|
|
}
|