|
| 1 | +package svc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/owncloud/ocis-pkg/v2/middleware" |
| 8 | + "github.com/owncloud/ocis-settings/pkg/proto/v0" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + ctxWithUUID = context.WithValue(context.Background(), middleware.UUIDKey, "61445573-4dbe-4d56-88dc-88ab47aceba7") |
| 14 | + ctxWithEmptyUUID = context.WithValue(context.Background(), middleware.UUIDKey, "") |
| 15 | + emptyCtx = context.Background() |
| 16 | + |
| 17 | + scenarios = []struct { |
| 18 | + name string |
| 19 | + identifier *proto.Identifier |
| 20 | + ctx context.Context |
| 21 | + expect *proto.Identifier |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "context with UUID; identifier = 'me'", |
| 25 | + ctx: ctxWithUUID, |
| 26 | + identifier: &proto.Identifier{ |
| 27 | + AccountUuid: "me", |
| 28 | + }, |
| 29 | + expect: &proto.Identifier{ |
| 30 | + AccountUuid: ctxWithUUID.Value(middleware.UUIDKey).(string), |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "context without UUID; identifier = 'me'", |
| 35 | + ctx: ctxWithEmptyUUID, |
| 36 | + identifier: &proto.Identifier{ |
| 37 | + AccountUuid: "me", |
| 38 | + }, |
| 39 | + expect: &proto.Identifier{ |
| 40 | + AccountUuid: "", |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "context with UUID; identifier not 'me'", |
| 45 | + ctx: ctxWithUUID, |
| 46 | + identifier: &proto.Identifier{}, |
| 47 | + expect: &proto.Identifier{ |
| 48 | + AccountUuid: "", |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +) |
| 53 | + |
| 54 | +func TestGetFailsafeIdentifier(t *testing.T) { |
| 55 | + for _, s := range scenarios { |
| 56 | + scenario := s |
| 57 | + t.Run(scenario.name, func(t *testing.T) { |
| 58 | + got := getFailsafeIdentifier(scenario.ctx, scenario.identifier) |
| 59 | + assert.NotPanics(t, func() { |
| 60 | + getFailsafeIdentifier(emptyCtx, scenario.identifier) |
| 61 | + }) |
| 62 | + assert.Equal(t, scenario.expect, got) |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
0 commit comments