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