| 1 | package assist |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // offFilter keeps everything but the providers and operations named. |
| 10 | type offFilter struct{ providers, ops map[string]bool } |
| 11 | |
| 12 | func (f offFilter) Provider(name string) bool { return !f.providers[name] } |
| 13 | func (f offFilter) Operation(op Operation) bool { return !f.ops[op.Name] } |
| 14 | |
| 15 | // look leaves out what the person can't use, so the model isn't offered |
| 16 | // it, and describe doesn't find it either. |
| 17 | func TestLookHidesOffOperations(t *testing.T) { |
| 18 | a := &fake{name: "oficina", ops: map[string][]Operation{"p": {{Name: "calendar_list", ReadOnly: true}, {Name: "calendar_add"}}}} |
| 19 | r := NewRegistry(a) |
| 20 | ctx := WithFilter(context.Background(), offFilter{ops: map[string]bool{"calendar_add": true}}) |
| 21 | out, _ := r.Look(ctx, "p", "") |
| 22 | if !strings.Contains(out, "calendar_list") || strings.Contains(out, "calendar_add") { |
| 23 | t.Errorf("look:\n%s", out) |
| 24 | } |
| 25 | if _, err := r.Describe(ctx, "p", "calendar_add"); err == nil { |
| 26 | t.Error("describe found an operation the person can't use") |
| 27 | } |
| 28 | // Without a filter, as before. |
| 29 | if out, _ := r.Look(context.Background(), "p", ""); !strings.Contains(out, "calendar_add") { |
| 30 | t.Errorf("unfiltered look:\n%s", out) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // A provider whose whole ability is off is never asked to list: a paused |
| 35 | // connection isn't used, even to list. |
| 36 | func TestListSkipsOffProvider(t *testing.T) { |
| 37 | a := &fake{name: "oficina", ops: map[string][]Operation{"p": {{Name: "calendar_list", ReadOnly: true}}}} |
| 38 | b := &fake{name: "base", ops: map[string][]Operation{"p": {{Name: "base_apps", ReadOnly: true}}}} |
| 39 | r := NewRegistry(a, b) |
| 40 | ctx := WithFilter(context.Background(), offFilter{providers: map[string]bool{"oficina": true}}) |
| 41 | out, _ := r.Look(ctx, "p", "") |
| 42 | if a.lists.Load() != 0 || strings.Contains(out, "calendar_list") || !strings.Contains(out, "base_apps") { |
| 43 | t.Errorf("lists %d, look:\n%s", a.lists.Load(), out) |
| 44 | } |
| 45 | if _, err := r.Invoke(ctx, "p", "calendar_list", nil, ""); err == nil || a.lists.Load() != 0 { |
| 46 | t.Errorf("invoke reached a provider that is off: %v", err) |
| 47 | } |
| 48 | } |
| 49 | |