Skip to content

Commit

Permalink
Corrected some tests and added my own tests WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ALCooper12 committed Dec 24, 2024
1 parent ecd951f commit e568316
Show file tree
Hide file tree
Showing 3 changed files with 389 additions and 44 deletions.
2 changes: 2 additions & 0 deletions cmd/api/src/api/v2/auth/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestManagementResource_CreateOIDCProvider(t *testing.T) {
defer mockCtrl.Finish()

t.Run("successfully create a new OIDCProvider", func(t *testing.T) {
mockDB.EXPECT().GetRole(gomock.Any(), int32(0)).Return(model.Role{}, nil)
mockDB.EXPECT().CreateOIDCProvider(gomock.Any(), "Bloodhound gang", "https://localhost/auth", "bloodhound", config).Return(model.OIDCProvider{
ClientID: "bloodhound",
Issuer: "https://localhost/auth",
Expand Down Expand Up @@ -86,6 +87,7 @@ func TestManagementResource_CreateOIDCProvider(t *testing.T) {
})

t.Run("error creating oidc provider db entry", func(t *testing.T) {
mockDB.EXPECT().GetRole(gomock.Any(), int32(0)).Return(model.Role{}, nil)
mockDB.EXPECT().CreateOIDCProvider(gomock.Any(), "test", "https://localhost/auth", "bloodhound", config).Return(model.OIDCProvider{}, fmt.Errorf("error"))

test.Request(t).
Expand Down
190 changes: 158 additions & 32 deletions cmd/api/src/database/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ import (
const (
userPrincipal = "[email protected]"
user2Principal = "[email protected]"
roleToDelete = auth.RoleReadOnly
user3Principal = "[email protected]"
user4Principal = "[email protected]"

roleToDelete = auth.RoleReadOnly
)

func initAndGetRoles(t *testing.T) (database.Database, model.Roles) {
Expand Down Expand Up @@ -486,39 +489,162 @@ func TestDatabase_SetUserSessionFlag(t *testing.T) {
}

func TestDatabase_GetUserSSOSession(t *testing.T) {
var (
testCtx = context.Background()
dbInst, user = initAndCreateUser(t)
samlProvider = model.SAMLProvider{
Name: "provider",
DisplayName: "provider name",
IssuerURI: "https://idp.example.com/idp.xml",
SingleSignOnURI: "https://idp.example.com/sso",
t.Run("Successful GetUserSSOSession (SAML)", func(t *testing.T) {
var (
testCtx = context.Background()
dbInst, user = initAndCreateUser(t)
samlProvider = model.SAMLProvider{
Name: "provider",
DisplayName: "provider name",
IssuerURI: "https://idp.example.com/idp.xml",
SingleSignOnURI: "https://idp.example.com/sso",
}
)

// Initialize the SAMLProvider without setting SSOProviderID
newSAMLProvider, err := dbInst.CreateSAMLIdentityProvider(testCtx, samlProvider, model.SSOProviderConfig{})
require.Nil(t, err)

user.SSOProviderID = newSAMLProvider.SSOProviderID
err = dbInst.UpdateUser(testCtx, user)
require.Nil(t, err)

userSession := model.UserSession{
AuthProviderID: newSAMLProvider.ID,
AuthProviderType: model.SessionAuthProviderSAML,
User: user,
UserID: user.ID,
ExpiresAt: time.Now().UTC().Add(time.Hour),
}
config = model.SSOProviderConfig{}
)

// Initialize the SAMLProvider without setting SSOProviderID
newSAMLProvider, err := dbInst.CreateSAMLIdentityProvider(testCtx, samlProvider, config)
require.Nil(t, err)

user.SSOProviderID = newSAMLProvider.SSOProviderID
err = dbInst.UpdateUser(testCtx, user)
require.Nil(t, err)

userSession := model.UserSession{
AuthProviderID: newSAMLProvider.ID,
AuthProviderType: model.SessionAuthProviderSAML,
User: user,
UserID: user.ID,
ExpiresAt: time.Now().UTC().Add(time.Hour),
}
newUserSession, err := dbInst.CreateUserSession(testCtx, userSession)
require.Nil(t, err)

dbSess, err := dbInst.GetUserSession(testCtx, newUserSession.ID)
require.Nil(t, err)
require.NotNil(t, dbSess.User.SSOProvider)
require.NotNil(t, dbSess.User.SSOProvider.SAMLProvider)
})

t.Run("Successful GetUserSSOSession (SAML) with config values", func(t *testing.T) {
var (
testCtx = context.Background()
dbInst, user = initAndCreateUser(t)
samlProvider = model.SAMLProvider{
Name: "provider",
DisplayName: "provider name",
IssuerURI: "https://idp.example.com/idp.xml",
SingleSignOnURI: "https://idp.example.com/sso",
}
config = model.SSOProviderConfig{
AutoProvision: model.AutoProvision{
Enabled: true,
DefaultRole: 3,
RoleProvision: true,
},
}
)

newUserSession, err := dbInst.CreateUserSession(testCtx, userSession)
require.Nil(t, err)
// Initialize the SAMLProvider without setting SSOProviderID
newSAMLProvider, err := dbInst.CreateSAMLIdentityProvider(testCtx, samlProvider, config)
require.Nil(t, err)

dbSess, err := dbInst.GetUserSession(testCtx, newUserSession.ID)
require.Nil(t, err)
require.NotNil(t, dbSess.User.SSOProvider)
require.NotNil(t, dbSess.User.SSOProvider.SAMLProvider)
user.SSOProviderID = newSAMLProvider.SSOProviderID
err = dbInst.UpdateUser(testCtx, user)
require.Nil(t, err)

userSession := model.UserSession{
AuthProviderID: newSAMLProvider.ID,
AuthProviderType: model.SessionAuthProviderSAML,
User: user,
UserID: user.ID,
ExpiresAt: time.Now().UTC().Add(time.Hour),
}

newUserSession, err := dbInst.CreateUserSession(testCtx, userSession)
require.Nil(t, err)

dbSess, err := dbInst.GetUserSession(testCtx, newUserSession.ID)
require.Nil(t, err)
require.NotNil(t, dbSess.User.SSOProvider)
require.NotNil(t, dbSess.User.SSOProvider.SAMLProvider)
})

t.Run("Successful GetUserSSOSession (OIDC)", func(t *testing.T) {
var (
testCtx = context.Background()
dbInst, user = initAndCreateUser(t)
oidcProvider = model.OIDCProvider{
ClientID: "bloodhound",
Issuer: "https://localhost/auth",
}
config = model.SSOProviderConfig{}
)

// Initialize the OIDCProvider without setting SSOProviderID
newOIDCProvider, err := dbInst.CreateOIDCProvider(testCtx, "test", oidcProvider.Issuer, oidcProvider.ClientID, config)
require.Nil(t, err)

user.SSOProviderID = null.Int32From(int32(newOIDCProvider.SSOProviderID))
err = dbInst.UpdateUser(testCtx, user)
require.Nil(t, err)

userSession := model.UserSession{
AuthProviderID: newOIDCProvider.ID,
AuthProviderType: model.SessionAuthProviderOIDC,
User: user,
UserID: user.ID,
ExpiresAt: time.Now().UTC().Add(time.Hour),
}

newUserSession, err := dbInst.CreateUserSession(testCtx, userSession)
require.Nil(t, err)

dbSess, err := dbInst.GetUserSession(testCtx, newUserSession.ID)
require.Nil(t, err)
require.NotNil(t, dbSess.User.SSOProvider)
require.NotNil(t, dbSess.User.SSOProvider.OIDCProvider)
})

t.Run("Successful GetUserSSOSession (OIDC) with config values", func(t *testing.T) {
var (
testCtx = context.Background()
dbInst, user = initAndCreateUser(t)
oidcProvider = model.OIDCProvider{
ClientID: "bloodhound",
Issuer: "https://localhost/auth",
}
config = model.SSOProviderConfig{
AutoProvision: model.AutoProvision{
Enabled: true,
DefaultRole: 3,
RoleProvision: true,
},
}
)

// Initialize the OIDCProvider without setting SSOProviderID
newOIDCProvider, err := dbInst.CreateOIDCProvider(testCtx, "test", oidcProvider.Issuer, oidcProvider.ClientID, config)
require.Nil(t, err)

user.SSOProviderID = null.Int32From(int32(newOIDCProvider.SSOProviderID))
err = dbInst.UpdateUser(testCtx, user)
require.Nil(t, err)

userSession := model.UserSession{
AuthProviderID: newOIDCProvider.ID,
AuthProviderType: model.SessionAuthProviderOIDC,
User: user,
UserID: user.ID,
ExpiresAt: time.Now().UTC().Add(time.Hour),
}

newUserSession, err := dbInst.CreateUserSession(testCtx, userSession)
require.Nil(t, err)

dbSess, err := dbInst.GetUserSession(testCtx, newUserSession.ID)
require.Nil(t, err)
require.NotNil(t, dbSess.User.SSOProvider)
require.NotNil(t, dbSess.User.SSOProvider.OIDCProvider)
})
}
Loading

0 comments on commit e568316

Please sign in to comment.