|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "google.golang.org/protobuf/proto" |
| 9 | + |
| 10 | + "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/common" |
| 11 | + "github.com/flyteorg/flyte/v2/runs/repository/models" |
| 12 | +) |
| 13 | + |
| 14 | +func mustMarshalIdentity(t *testing.T, id *common.EnrichedIdentity) []byte { |
| 15 | + t.Helper() |
| 16 | + b, err := proto.Marshal(id) |
| 17 | + require.NoError(t, err) |
| 18 | + return b |
| 19 | +} |
| 20 | + |
| 21 | +func fullIdentity(sub, first, last, email string) *common.EnrichedIdentity { |
| 22 | + return &common.EnrichedIdentity{Principal: &common.EnrichedIdentity_User{User: &common.User{ |
| 23 | + Id: &common.UserIdentifier{Subject: sub}, |
| 24 | + Spec: &common.UserSpec{FirstName: first, LastName: last, Email: email}, |
| 25 | + }}} |
| 26 | +} |
| 27 | + |
| 28 | +func TestActionMetadataFromModel_ExecutedBy(t *testing.T) { |
| 29 | + t.Run("full identity from executed_by", func(t *testing.T) { |
| 30 | + m := &models.Action{ExecutedBy: mustMarshalIdentity(t, fullIdentity("00u1", "Kevin", "Su", "kevin@union.ai"))} |
| 31 | + eb := actionMetadataFromModel(m).GetExecutedBy().GetUser() |
| 32 | + assert.Equal(t, "00u1", eb.GetId().GetSubject()) |
| 33 | + assert.Equal(t, "Kevin", eb.GetSpec().GetFirstName()) |
| 34 | + assert.Equal(t, "Su", eb.GetSpec().GetLastName()) |
| 35 | + assert.Equal(t, "kevin@union.ai", eb.GetSpec().GetEmail()) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("falls back to subject-only from created_by", func(t *testing.T) { |
| 39 | + m := &models.Action{} |
| 40 | + m.CreatedBy.Valid, m.CreatedBy.String = true, "00u2" |
| 41 | + eb := actionMetadataFromModel(m).GetExecutedBy().GetUser() |
| 42 | + assert.Equal(t, "00u2", eb.GetId().GetSubject()) |
| 43 | + assert.Nil(t, eb.GetSpec()) |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("corrupt executed_by falls back to created_by", func(t *testing.T) { |
| 47 | + m := &models.Action{ExecutedBy: []byte("not a valid proto\xff\xfe")} |
| 48 | + m.CreatedBy.Valid, m.CreatedBy.String = true, "00u3" |
| 49 | + assert.Equal(t, "00u3", actionMetadataFromModel(m).GetExecutedBy().GetUser().GetId().GetSubject()) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("no identity yields nil executed_by", func(t *testing.T) { |
| 53 | + assert.Nil(t, actionMetadataFromModel(&models.Action{}).GetExecutedBy()) |
| 54 | + }) |
| 55 | +} |
0 commit comments