Files
probo/pkg/accessreview/drivers/brevo_test.go
Aurélien Sibiril a0d3806c21 Add four API-key access-review connectors
Add Pylon, OpenRouter, incident.io and Brevo as access-review connectors.
All are API-key, single-tenant providers (Pattern 3): the key identifies
one tenant, so there is no OAuth flow, picker UI, or bootstrap/helm
configuration.

- Pylon: Bearer token, GET /users; resolves each user's opaque role_id to
  a role name via GET /user-roles, with cursor pagination.
- OpenRouter: Bearer management key, GET /api/v1/organization/members. The
  endpoint requires an organization account -- a personal key authenticates
  but returns 404 -- so the connection probe rejects 404 on top of 401/403
  (doProbeRequest gained an opt-in extra-reject set) to surface a non-org
  key at connect time instead of mid-campaign.
- incident.io: Bearer token, GET /v2/users. Its OAuth is outbound-only, so
  the API key is the inbound path; live base_role/custom_roles take
  precedence over the deprecated role enum.
- Brevo: API key in the api-key header (Registration.APIKeyHeader), GET
  /v3/organization/invited/users. A live recording corrected the documented
  schema: is_owner is a JSON boolean (not a string) and an id field is
  present, so it is used as the stable ExternalID.

The OpenRouter and Brevo cassettes are anonymized live recordings; Pylon
and incident.io use hand-authored fixtures (no self-serve test tenant). The
shared three-valued active-status mapping is consolidated into
activeFromStatus in driver.go.

Each adds the enum value, migration, GraphQL binding, provider
Registration, a driver with a cassette-driven test, and a brand logo.

Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
2026-06-24 22:22:51 +02:00

108 lines
3.9 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package drivers
import (
"context"
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.probo.inc/probo/pkg/coredata"
)
func TestBrevoDriver(t *testing.T) {
t.Parallel()
rec := newRecorder(t, "testdata/brevo", "BREVO_API_KEY")
// Brevo authenticates via the api-key header, not Authorization.
client := newVCRClientWithHeader(rec, "api-key", os.Getenv("BREVO_API_KEY"))
driver := NewBrevoDriver(client)
records, err := driver.ListAccounts(context.Background())
require.NoError(t, err)
require.Len(t, records, 3)
// Cassette recorded live (api-key header), then anonymized: the owner has
// every feature at "owner"; the two members have crm/transactional "full"
// and the rest "none".
owner := records[0]
assert.Equal(t, "000000000000000000000001", owner.ExternalID)
assert.Equal(t, "owner@example.com", owner.Email)
assert.Equal(t, "owner@example.com", owner.FullName)
assert.True(t, owner.IsAdmin)
require.NotNil(t, owner.Active)
assert.True(t, *owner.Active)
assert.Equal(t, []string{"owner"}, owner.Roles)
assert.Equal(t, coredata.AccessReviewEntryAccountTypeUser, owner.AccountType)
// Non-owner; "none" levels filtered, the remaining "full" de-duplicated.
member := records[1]
assert.Equal(t, "000000000000000000000002", member.ExternalID)
assert.False(t, member.IsAdmin)
assert.Equal(t, []string{"full"}, member.Roles)
require.NotNil(t, member.Active)
assert.True(t, *member.Active)
// The third record is asserted too, so a swap or corruption is caught.
viewer := records[2]
assert.Equal(t, "000000000000000000000003", viewer.ExternalID)
assert.Equal(t, "viewer@example.com", viewer.Email)
assert.False(t, viewer.IsAdmin)
assert.Equal(t, []string{"full"}, viewer.Roles)
}
func TestBrevoExternalID(t *testing.T) {
t.Parallel()
// The stable id is preferred when present.
assert.Equal(t, "abc123", brevoExternalID(brevoInvitedUser{ID: "abc123"}, "x@example.com"))
// With no id, the email is the fallback.
assert.Equal(t, "x@example.com", brevoExternalID(brevoInvitedUser{}, "x@example.com"))
assert.Equal(t, "x@example.com", brevoExternalID(brevoInvitedUser{ID: " "}, "x@example.com"))
}
func TestBrevoIsOwner(t *testing.T) {
t.Parallel()
// The live API returns a JSON boolean; older docs/SDK show a string.
// Both must be tolerated.
assert.True(t, brevoIsOwner(json.RawMessage(`true`)))
assert.False(t, brevoIsOwner(json.RawMessage(`false`)))
assert.True(t, brevoIsOwner(json.RawMessage(`"true"`)))
assert.False(t, brevoIsOwner(json.RawMessage(`"false"`)))
assert.False(t, brevoIsOwner(nil))
}
func TestBrevoRoles(t *testing.T) {
t.Parallel()
// Distinct non-"none" levels, sorted; "none" is filtered out.
roles := brevoRoles(map[string]json.RawMessage{
"marketing": json.RawMessage(`"owner"`),
"conversations": json.RawMessage(`"owner"`),
"crm": json.RawMessage(`"none"`),
})
assert.Equal(t, []string{"owner"}, roles)
// All "none" → no roles.
assert.Empty(t, brevoRoles(map[string]json.RawMessage{"crm": json.RawMessage(`"none"`)}))
// A non-string shape is ignored rather than failing.
assert.Empty(t, brevoRoles(map[string]json.RawMessage{"crm": json.RawMessage(`{"x":1}`)}))
}