TestFactory creates a Factory for testing. Returns (factory, stdout buffer, stderr buffer, http mock registry).
(t *testing.T, config *core.CliConfig)
| 30 | // TestFactory creates a Factory for testing. |
| 31 | // Returns (factory, stdout buffer, stderr buffer, http mock registry). |
| 32 | func TestFactory(t *testing.T, config *core.CliConfig) (*Factory, *bytes.Buffer, *bytes.Buffer, *httpmock.Registry) { |
| 33 | t.Helper() |
| 34 | |
| 35 | reg := &httpmock.Registry{} |
| 36 | t.Cleanup(func() { reg.Verify(t) }) |
| 37 | |
| 38 | stdoutBuf := &bytes.Buffer{} |
| 39 | stderrBuf := &bytes.Buffer{} |
| 40 | |
| 41 | mockClient := httpmock.NewClient(reg) |
| 42 | sdkMockClient := &http.Client{ |
| 43 | Transport: &UserAgentTransport{Base: reg}, |
| 44 | } |
| 45 | |
| 46 | var testLarkClient *lark.Client |
| 47 | if config != nil && config.AppID != "" { |
| 48 | opts := []lark.ClientOptionFunc{ |
| 49 | lark.WithEnableTokenCache(false), |
| 50 | lark.WithLogLevel(larkcore.LogLevelError), |
| 51 | lark.WithHttpClient(sdkMockClient), |
| 52 | lark.WithHeaders(BaseSecurityHeaders()), |
| 53 | } |
| 54 | if config.Brand != "" { |
| 55 | opts = append(opts, lark.WithOpenBaseUrl(core.ResolveOpenBaseURL(config.Brand))) |
| 56 | } |
| 57 | testLarkClient = lark.NewClient(config.AppID, credential.RuntimeAppSecret(config.AppSecret), opts...) |
| 58 | } |
| 59 | |
| 60 | testCred := credential.NewCredentialProvider( |
| 61 | nil, |
| 62 | &testDefaultAcct{config: config}, |
| 63 | &testDefaultToken{}, |
| 64 | func() (*http.Client, error) { return mockClient, nil }, |
| 65 | ) |
| 66 | |
| 67 | f := &Factory{ |
| 68 | Config: func() (*core.CliConfig, error) { return config, nil }, |
| 69 | HttpClient: func() (*http.Client, error) { return mockClient, nil }, |
| 70 | LarkClient: func() (*lark.Client, error) { return testLarkClient, nil }, |
| 71 | IOStreams: &IOStreams{In: nil, Out: stdoutBuf, ErrOut: stderrBuf}, |
| 72 | Keychain: &noopKeychain{}, |
| 73 | Credential: testCred, |
| 74 | FileIOProvider: fileio.GetProvider(), |
| 75 | } |
| 76 | return f, stdoutBuf, stderrBuf, reg |
| 77 | } |
| 78 | |
| 79 | type testDefaultAcct struct { |
| 80 | config *core.CliConfig |
no test coverage detected