|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestM365ReadOnlyPilotCommandsAreExposed(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + args []string |
| 13 | + want string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "outlook search", |
| 17 | + args: []string{"--json", "--read-only", "m365", "outlook", "search", "--query", "from:felipe"}, |
| 18 | + want: "m365.outlook.search", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "outlook message get", |
| 22 | + args: []string{"--json", "--read-only", "m365", "outlook", "message", "get", "AAMk-message-id"}, |
| 23 | + want: "m365.outlook.message.get", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "calendar events", |
| 27 | + args: []string{"--json", "--read-only", "m365", "calendar", "events", "--from", "2026-05-31T00:00:00Z", "--to", "2026-06-01T00:00:00Z"}, |
| 28 | + want: "m365.calendar.events", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "calendar freebusy", |
| 32 | + args: []string{"--json", "--read-only", "m365", "calendar", "freebusy", "--users", "bernardo@example.com,felipe@example.com"}, |
| 33 | + want: "m365.calendar.freebusy", |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tt := range tests { |
| 38 | + t.Run(tt.name, func(t *testing.T) { |
| 39 | + out := captureStdout(t, func() { |
| 40 | + _ = captureStderr(t, func() { |
| 41 | + if err := Execute(tt.args); err != nil { |
| 42 | + t.Fatalf("Execute(%v): %v", tt.args, err) |
| 43 | + } |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + var got map[string]any |
| 48 | + if err := json.Unmarshal([]byte(out), &got); err != nil { |
| 49 | + t.Fatalf("json output: %v\n%s", err, out) |
| 50 | + } |
| 51 | + if got["operation"] != tt.want { |
| 52 | + t.Fatalf("operation = %v, want %s; output=%s", got["operation"], tt.want, out) |
| 53 | + } |
| 54 | + if got["provider"] != "microsoft_graph" { |
| 55 | + t.Fatalf("provider = %v, want microsoft_graph; output=%s", got["provider"], out) |
| 56 | + } |
| 57 | + if got["mode"] != "read_only_pilot" { |
| 58 | + t.Fatalf("mode = %v, want read_only_pilot; output=%s", got["mode"], out) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestM365PilotCommandsRequireExplicitReadOnlyFlag(t *testing.T) { |
| 65 | + _ = captureStderr(t, func() { |
| 66 | + err := Execute([]string{"--json", "m365", "outlook", "search", "--query", "from:felipe"}) |
| 67 | + if err == nil { |
| 68 | + t.Fatal("expected m365 pilot command without --read-only to fail closed") |
| 69 | + } |
| 70 | + if !strings.Contains(err.Error(), "--read-only") { |
| 71 | + t.Fatalf("expected --read-only error, got: %v", err) |
| 72 | + } |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func TestAuthServicesJSONIncludesM365PilotReadOnlyScopes(t *testing.T) { |
| 77 | + out := captureStdout(t, func() { |
| 78 | + _ = captureStderr(t, func() { |
| 79 | + if err := Execute([]string{"--json", "auth", "services"}); err != nil { |
| 80 | + t.Fatalf("auth services: %v", err) |
| 81 | + } |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + var payload struct { |
| 86 | + Services []struct { |
| 87 | + Service string `json:"service"` |
| 88 | + Scopes []string `json:"scopes"` |
| 89 | + } `json:"services"` |
| 90 | + } |
| 91 | + if err := json.Unmarshal([]byte(out), &payload); err != nil { |
| 92 | + t.Fatalf("json output: %v\n%s", err, out) |
| 93 | + } |
| 94 | + |
| 95 | + var scopes []string |
| 96 | + for _, service := range payload.Services { |
| 97 | + if service.Service == "m365" { |
| 98 | + scopes = service.Scopes |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | + if len(scopes) == 0 { |
| 103 | + t.Fatalf("auth services missing m365 service: %s", out) |
| 104 | + } |
| 105 | + for _, scope := range []string{"User.Read", "Mail.Read", "Calendars.Read"} { |
| 106 | + if !stringSliceContains(scopes, scope) { |
| 107 | + t.Fatalf("m365 auth services missing %s: %#v", scope, scopes) |
| 108 | + } |
| 109 | + } |
| 110 | + for _, forbidden := range []string{"Mail.Send", "Calendars.ReadWrite"} { |
| 111 | + if stringSliceContains(scopes, forbidden) { |
| 112 | + t.Fatalf("m365 auth services exposed write scope %s: %#v", forbidden, scopes) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func stringSliceContains(values []string, want string) bool { |
| 118 | + for _, value := range values { |
| 119 | + if value == want { |
| 120 | + return true |
| 121 | + } |
| 122 | + } |
| 123 | + return false |
| 124 | +} |
0 commit comments