|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/automagik-dev/workit/internal/msauth" |
| 11 | + "github.com/automagik-dev/workit/internal/secrets" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAuthAddM365UsesOAuthAndStoresToken(t *testing.T) { |
| 15 | + origOpen := openSecretsStore |
| 16 | + origAuth := authorizeM365 |
| 17 | + origKeychain := ensureKeychainAccess |
| 18 | + t.Cleanup(func() { |
| 19 | + openSecretsStore = origOpen |
| 20 | + authorizeM365 = origAuth |
| 21 | + ensureKeychainAccess = origKeychain |
| 22 | + }) |
| 23 | + |
| 24 | + store := newMemSecretsStore() |
| 25 | + openSecretsStore = func() (secrets.Store, error) { return store, nil } |
| 26 | + ensureKeychainAccess = func() error { return nil } |
| 27 | + authorizeM365 = func(context.Context, msauth.AuthorizeOptions) (msauth.AuthorizeResult, error) { |
| 28 | + return msauth.AuthorizeResult{Email: "bernardo@hapvida.com.br", RefreshToken: "m365-refresh-token"}, nil |
| 29 | + } |
| 30 | + |
| 31 | + out := captureStdout(t, func() { |
| 32 | + _ = captureStderr(t, func() { |
| 33 | + if err := Execute([]string{"--json", "auth", "add", "bernardo@hapvida.com.br", "--services", "m365", "--readonly"}); err != nil { |
| 34 | + t.Fatalf("auth add m365: %v", err) |
| 35 | + } |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + var payload map[string]any |
| 40 | + if err := json.Unmarshal([]byte(out), &payload); err != nil { |
| 41 | + t.Fatalf("json output: %v\n%s", err, out) |
| 42 | + } |
| 43 | + if payload["provider"] != "microsoft_graph" || payload["stored"] != true { |
| 44 | + t.Fatalf("unexpected output: %#v", payload) |
| 45 | + } |
| 46 | + tok, err := store.GetToken(msauth.ClientName, "bernardo@hapvida.com.br") |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("stored m365 token: %v", err) |
| 49 | + } |
| 50 | + if tok.RefreshToken != "m365-refresh-token" { |
| 51 | + t.Fatalf("refresh token = %q", tok.RefreshToken) |
| 52 | + } |
| 53 | + if !stringSliceContainsForM365AuthTest(tok.Services, "m365") { |
| 54 | + t.Fatalf("services = %#v", tok.Services) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestAuthAddM365RequiresReadonly(t *testing.T) { |
| 59 | + _ = captureStderr(t, func() { |
| 60 | + err := Execute([]string{"--json", "auth", "add", "bernardo@hapvida.com.br", "--services", "m365"}) |
| 61 | + if err == nil { |
| 62 | + t.Fatal("expected missing --readonly to fail closed") |
| 63 | + } |
| 64 | + if !strings.Contains(err.Error(), "--readonly") { |
| 65 | + t.Fatalf("expected --readonly error, got: %v", err) |
| 66 | + } |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func TestAuthAddM365RemoteStepOnePrintsMicrosoftURL(t *testing.T) { |
| 71 | + origURL := m365ManualAuthURL |
| 72 | + t.Cleanup(func() { m365ManualAuthURL = origURL }) |
| 73 | + m365ManualAuthURL = func(context.Context, msauth.ManualAuthURLOptions) (msauth.ManualAuthURLResult, error) { |
| 74 | + return msauth.ManualAuthURLResult{URL: "https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?client_id=test", State: "state"}, nil |
| 75 | + } |
| 76 | + |
| 77 | + out := captureStdout(t, func() { |
| 78 | + _ = captureStderr(t, func() { |
| 79 | + if err := Execute([]string{"--json", "auth", "add", "bernardo@hapvida.com.br", "--services", "m365", "--readonly", "--remote", "--step", "1"}); err != nil { |
| 80 | + t.Fatalf("remote step 1: %v", err) |
| 81 | + } |
| 82 | + }) |
| 83 | + }) |
| 84 | + if !strings.Contains(out, "login.microsoftonline.com") || !strings.Contains(out, "auth_url") { |
| 85 | + t.Fatalf("unexpected output: %s", out) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestAuthManageM365PrintURLIsNonTechnicalOAuthHandoff(t *testing.T) { |
| 90 | + origURL := m365ManualAuthURL |
| 91 | + t.Cleanup(func() { m365ManualAuthURL = origURL }) |
| 92 | + m365ManualAuthURL = func(context.Context, msauth.ManualAuthURLOptions) (msauth.ManualAuthURLResult, error) { |
| 93 | + return msauth.ManualAuthURLResult{URL: "https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?client_id=test", State: "state", ExpiresIn: int((5 * time.Minute).Seconds())}, nil |
| 94 | + } |
| 95 | + |
| 96 | + out := captureStdout(t, func() { |
| 97 | + _ = captureStderr(t, func() { |
| 98 | + if err := Execute([]string{"--json", "auth", "manage", "--services", "m365", "--print-url"}); err != nil { |
| 99 | + t.Fatalf("auth manage m365 print-url: %v", err) |
| 100 | + } |
| 101 | + }) |
| 102 | + }) |
| 103 | + if !strings.Contains(out, "login.microsoftonline.com") || !strings.Contains(out, "microsoft_graph") { |
| 104 | + t.Fatalf("unexpected output: %s", out) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func stringSliceContainsForM365AuthTest(values []string, want string) bool { |
| 109 | + for _, value := range values { |
| 110 | + if value == want { |
| 111 | + return true |
| 112 | + } |
| 113 | + } |
| 114 | + return false |
| 115 | +} |
0 commit comments