2023-06-17 22:35:17 +08:00
|
|
|
package teststore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIdentityProviderStore(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ts := NewTestingStore(ctx, t)
|
2023-06-26 23:46:01 +08:00
|
|
|
createdIDP, err := ts.CreateIdentityProvider(ctx, &store.IdentityProvider{
|
2023-06-17 22:35:17 +08:00
|
|
|
Name: "GitHub OAuth",
|
|
|
|
Type: store.IdentityProviderOAuth2,
|
|
|
|
IdentifierFilter: "",
|
|
|
|
Config: &store.IdentityProviderConfig{
|
|
|
|
OAuth2Config: &store.IdentityProviderOAuth2Config{
|
2023-06-26 23:46:01 +08:00
|
|
|
ClientID: "client_id",
|
|
|
|
ClientSecret: "client_secret",
|
2023-06-17 22:35:17 +08:00
|
|
|
AuthURL: "https://github.com/auth",
|
|
|
|
TokenURL: "https://github.com/token",
|
|
|
|
UserInfoURL: "https://github.com/user",
|
|
|
|
Scopes: []string{"login"},
|
|
|
|
FieldMapping: &store.FieldMapping{
|
|
|
|
Identifier: "login",
|
|
|
|
DisplayName: "name",
|
|
|
|
Email: "emai",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-06-26 23:46:01 +08:00
|
|
|
idp, err := ts.GetIdentityProvider(ctx, &store.FindIdentityProvider{
|
2023-06-17 22:35:17 +08:00
|
|
|
ID: &createdIDP.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, createdIDP, idp)
|
2023-06-26 23:46:01 +08:00
|
|
|
newName := "My GitHub OAuth"
|
|
|
|
updatedIdp, err := ts.UpdateIdentityProvider(ctx, &store.UpdateIdentityProvider{
|
|
|
|
ID: idp.ID,
|
|
|
|
Name: &newName,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, newName, updatedIdp.Name)
|
|
|
|
err = ts.DeleteIdentityProvider(ctx, &store.DeleteIdentityProvider{
|
2023-06-17 22:35:17 +08:00
|
|
|
ID: idp.ID,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-06-26 23:46:01 +08:00
|
|
|
idpList, err := ts.ListIdentityProviders(ctx, &store.FindIdentityProvider{})
|
2023-06-17 22:35:17 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(idpList))
|
|
|
|
}
|