idp_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package teststore
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. storepb "github.com/usememos/memos/proto/gen/store"
  7. "github.com/usememos/memos/store"
  8. )
  9. func TestIdentityProviderStore(t *testing.T) {
  10. ctx := context.Background()
  11. ts := NewTestingStore(ctx, t)
  12. createdIDP, err := ts.CreateIdentityProvider(ctx, &storepb.IdentityProvider{
  13. Name: "GitHub OAuth",
  14. Type: storepb.IdentityProvider_OAUTH2,
  15. IdentifierFilter: "",
  16. Config: &storepb.IdentityProviderConfig{
  17. Config: &storepb.IdentityProviderConfig_Oauth2Config{
  18. Oauth2Config: &storepb.OAuth2Config{
  19. ClientId: "client_id",
  20. ClientSecret: "client_secret",
  21. AuthUrl: "https://github.com/auth",
  22. TokenUrl: "https://github.com/token",
  23. UserInfoUrl: "https://github.com/user",
  24. Scopes: []string{"login"},
  25. FieldMapping: &storepb.FieldMapping{
  26. Identifier: "login",
  27. DisplayName: "name",
  28. Email: "email",
  29. },
  30. },
  31. },
  32. },
  33. })
  34. require.NoError(t, err)
  35. idp, err := ts.GetIdentityProvider(ctx, &store.FindIdentityProvider{
  36. ID: &createdIDP.Id,
  37. })
  38. require.NoError(t, err)
  39. require.NotNil(t, idp)
  40. require.Equal(t, createdIDP, idp)
  41. newName := "My GitHub OAuth"
  42. updatedIdp, err := ts.UpdateIdentityProvider(ctx, &store.UpdateIdentityProviderV1{
  43. ID: idp.Id,
  44. Name: &newName,
  45. })
  46. require.NoError(t, err)
  47. require.Equal(t, newName, updatedIdp.Name)
  48. err = ts.DeleteIdentityProvider(ctx, &store.DeleteIdentityProvider{
  49. ID: idp.Id,
  50. })
  51. require.NoError(t, err)
  52. idpList, err := ts.ListIdentityProviders(ctx, &store.FindIdentityProvider{})
  53. require.NoError(t, err)
  54. require.Equal(t, 0, len(idpList))
  55. ts.Close()
  56. }