Files
probo/pkg/accessreview/drivers/sendgrid_test.go
Bryan Frimin 4b64e59da4 Introduce access-review source snapshot and normalize naming
Decouple each campaign from the live access-review sources it was started
with by introducing a per-campaign source snapshot table
(access_review_campaign_sources). The snapshot captures the source name,
category, and connector at start time, so a review remains coherent even
after the underlying source is edited or deleted. Fetch tracking becomes
an append-only log (access_review_campaign_source_fetch_attempts) that
preserves every attempt with its own status and error rather than
overwriting a single row.

Rename the shared access-review tables and enums to use a consistent
access_review_ prefix throughout:

  access_entries          → access_review_entries
  access_sources          → access_review_sources
  access_source_category  → access_review_source_category
  access_entry_*          → access_review_entry_*

The same rename propagates to every coredata type, service, GraphQL
schema, MCP specification, CLI command, frontend component, and e2e test.
The accessreview package gains dedicated actions.go and policies.go files
for its own IAM policy set, mirroring the agentrun package pattern.

Signed-off-by: Bryan Frimin <bryan@probo.com>
2026-06-15 15:33:22 +02:00

152 lines
5.3 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"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.gearno.de/kit/log"
"go.probo.inc/probo/pkg/coredata"
)
func TestSendGridDriver(t *testing.T) {
t.Parallel()
rec := newRecorder(t, "testdata/sendgrid", "SENDGRID_API_KEY")
client := newVCRClient(rec, bearerAuth(os.Getenv("SENDGRID_API_KEY")))
driver := NewSendGridDriver(client, log.NewLogger(log.WithName("test")))
records, err := driver.ListAccounts(context.Background())
require.NoError(t, err)
// Two records: the owner + a restricted teammate. A third list row with an
// empty email is skipped.
require.Len(t, records, 2)
// The owner record is the real recording. The list endpoint carries no
// scopes, so the driver fetches the teammate detail to read them.
owner := records[0]
assert.Equal(t, "owner@example.com", owner.Email)
assert.Empty(t, owner.FullName)
assert.Equal(t, "Owner", owner.Role)
assert.True(t, owner.IsAdmin)
assert.Equal(t, "owner@example.com", owner.ExternalID)
assert.Equal(t, coredata.AccessReviewEntryAccountTypeUser, owner.AccountType)
// is_sso=false on the owner -> authenticates with SendGrid credentials.
assert.Equal(t, coredata.AccessReviewEntryAuthMethodPassword, owner.AuthMethod)
// The owner is a full-access user whose scope catalog contains BOTH
// 2fa_exempt and 2fa_required, so the MFA signal is ambiguous and the
// driver reports Unknown rather than guessing from scope ordering.
assert.Equal(t, coredata.MFAStatusUnknown, owner.MFAStatus)
// A restricted teammate, synthetic (the trial account has only the owner)
// but modelled on the real detail shape: a BARE object whose scopes carry
// a single 2fa flag. This makes the N+1 detail fetch load-bearing — an
// Enabled MFA here is reachable ONLY by correctly decoding the detail
// response, so it guards against the {"result":...}-envelope regression.
teammate := records[1]
assert.Equal(t, "taylor@example.com", teammate.Email)
assert.Equal(t, "Taylor Teammate", teammate.FullName)
assert.Equal(t, "Teammate", teammate.Role)
assert.False(t, teammate.IsAdmin)
// Non-unified teammate: username is a handle distinct from the email.
assert.Equal(t, "taylor-teammate", teammate.ExternalID)
assert.Equal(t, coredata.AccessReviewEntryAuthMethodSSO, teammate.AuthMethod)
assert.Equal(t, coredata.MFAStatusEnabled, teammate.MFAStatus)
}
func TestSendGridRole(t *testing.T) {
t.Parallel()
tests := []struct {
name string
userType string
isAdmin bool
want string
}{
{name: "owner", userType: "owner", isAdmin: true, want: "Owner"},
{name: "admin", userType: "admin", isAdmin: true, want: "Admin"},
{name: "teammate", userType: "teammate", isAdmin: false, want: "Teammate"},
{name: "empty admin", userType: "", isAdmin: true, want: "Admin"},
{name: "empty teammate", userType: "", isAdmin: false, want: "Teammate"},
{name: "unknown", userType: "custom-role", isAdmin: false, want: "custom-role"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, sendGridRole(tt.userType, tt.isAdmin))
})
}
}
func TestSendGridResponseItems(t *testing.T) {
t.Parallel()
t.Run("prefers result", func(t *testing.T) {
t.Parallel()
items := sendGridResponseItems(&sendGridTeammatesResponse{
Result: []sendGridTeammate{
{Email: "owner@example.com"},
},
Results: []sendGridTeammate{
{Email: "fallback@example.com"},
},
})
require.Len(t, items, 1)
assert.Equal(t, "owner@example.com", items[0].Email)
})
t.Run("falls back to results", func(t *testing.T) {
t.Parallel()
items := sendGridResponseItems(&sendGridTeammatesResponse{
Results: []sendGridTeammate{
{Email: "fallback@example.com"},
},
})
require.Len(t, items, 1)
assert.Equal(t, "fallback@example.com", items[0].Email)
})
}
func TestSendGridMFAStatus(t *testing.T) {
t.Parallel()
tests := []struct {
name string
scopes []string
want coredata.MFAStatus
}{
{name: "required", scopes: []string{"mail.send", "2fa_required"}, want: coredata.MFAStatusEnabled},
{name: "exempt", scopes: []string{"mail.send", "2fa_exempt"}, want: coredata.MFAStatusDisabled},
{name: "both is ambiguous", scopes: []string{"2fa_exempt", "2fa_required", "mail.send"}, want: coredata.MFAStatusUnknown},
{name: "neither", scopes: []string{"mail.send"}, want: coredata.MFAStatusUnknown},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, sendGridMFAStatus(tt.scopes))
})
}
}