2023-06-17 22:35:17 +08:00
|
|
|
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"
|
2023-06-17 22:35:17 +08:00
|
|
|
"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{
|
2023-06-17 22:35:17 +08:00
|
|
|
Name: "GitHub OAuth",
|
2024-04-13 10:50:25 +08:00
|
|
|
Type: storepb.IdentityProvider_OAUTH2,
|
2023-06-17 22:35:17 +08:00
|
|
|
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",
|
|
|
|
},
|
2023-06-17 22:35:17 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
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,
|
2023-06-17 22:35:17 +08:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2023-07-06 22:53:38 +08:00
|
|
|
require.NotNil(t, idp)
|
2023-06-17 22:35:17 +08:00
|
|
|
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,
|
2023-06-17 22:35:17 +08:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2024-04-17 08:56:52 +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))
|
2024-01-16 13:51:26 +08:00
|
|
|
ts.Close()
|
2023-06-17 22:35:17 +08:00
|
|
|
}
|