memos/test/store/idp_test.go

61 lines
1.7 KiB
Go
Raw Permalink Normal View History

package teststore
import (
"context"
"testing"
"github.com/stretchr/testify/require"
2024-04-13 10:50:25 +08:00
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
func TestIdentityProviderStore(t *testing.T) {
ctx := context.Background()
ts := NewTestingStore(ctx, t)
2024-04-17 08:56:52 +08:00
createdIDP, err := ts.CreateIdentityProvider(ctx, &storepb.IdentityProvider{
Name: "GitHub OAuth",
2024-04-13 10:50:25 +08:00
Type: storepb.IdentityProvider_OAUTH2,
IdentifierFilter: "",
2024-04-13 10:50:25 +08:00
Config: &storepb.IdentityProviderConfig{
Config: &storepb.IdentityProviderConfig_Oauth2Config{
Oauth2Config: &storepb.OAuth2Config{
ClientId: "client_id",
ClientSecret: "client_secret",
AuthUrl: "https://github.com/auth",
TokenUrl: "https://github.com/token",
UserInfoUrl: "https://github.com/user",
Scopes: []string{"login"},
FieldMapping: &storepb.FieldMapping{
Identifier: "login",
DisplayName: "name",
Email: "email",
},
},
},
},
})
require.NoError(t, err)
2024-04-17 08:56:52 +08:00
idp, err := ts.GetIdentityProvider(ctx, &store.FindIdentityProvider{
2024-04-13 10:50:25 +08:00
ID: &createdIDP.Id,
})
require.NoError(t, err)
2023-07-06 22:53:38 +08:00
require.NotNil(t, idp)
require.Equal(t, createdIDP, idp)
2023-06-26 23:46:01 +08:00
newName := "My GitHub OAuth"
2024-04-17 08:56:52 +08:00
updatedIdp, err := ts.UpdateIdentityProvider(ctx, &store.UpdateIdentityProviderV1{
2024-04-13 10:50:25 +08:00
ID: idp.Id,
2023-06-26 23:46:01 +08:00
Name: &newName,
})
require.NoError(t, err)
require.Equal(t, newName, updatedIdp.Name)
err = ts.DeleteIdentityProvider(ctx, &store.DeleteIdentityProvider{
2024-04-13 10:50:25 +08:00
ID: idp.Id,
})
require.NoError(t, err)
2024-04-17 08:56:52 +08:00
idpList, err := ts.ListIdentityProviders(ctx, &store.FindIdentityProvider{})
require.NoError(t, err)
require.Equal(t, 0, len(idpList))
ts.Close()
}