// NewServer builds a new empire.Empire instance and returns an httptest.Server // running the empire API. func NewServer(t testing.TB, e *empire.Empire) *httptest.Server { if e == nil { e = NewEmpire(t) } return httptest.NewServer(server.New(e, server.DefaultOptions)) }
func newServer(c *Context, e *empire.Empire) http.Handler { var opts server.Options opts.GitHub.Webhooks.Secret = c.String(FlagGithubWebhooksSecret) opts.GitHub.Deployments.Environments = strings.Split(c.String(FlagGithubDeploymentsEnvironments), ",") opts.GitHub.Deployments.ImageBuilder = newImageBuilder(c) opts.GitHub.Deployments.TugboatURL = c.String(FlagGithubDeploymentsTugboatURL) s := server.New(e, opts) s.URL = c.URL(FlagURL) s.Heroku.Auth = newAuth(c, e) s.Heroku.Secret = []byte(c.String(FlagSecret)) sp, err := c.SAMLServiceProvider() if err != nil { panic(err) } if sp != nil { s.ServiceProvider = sp s.Heroku.Unauthorized = heroku.SAMLUnauthorized(c.String(FlagURL) + "/saml/login") } h := middleware.Common(s) return middleware.Handler(c, h) }
func newServer(c *cli.Context, e *empire.Empire) http.Handler { opts := server.Options{} opts.GitHub.ClientID = c.String(FlagGithubClient) opts.GitHub.ClientSecret = c.String(FlagGithubSecret) opts.GitHub.Organization = c.String(FlagGithubOrg) opts.GitHub.ApiURL = c.String(FlagGithubApiURL) return server.New(e, opts) }
// NewServer builds a new empire.Empire instance and returns an httptest.Server // running the empire API. func NewServer(t testing.TB, e *empire.Empire) *httptest.Server { if e == nil { e = NewEmpire(t) } server.DefaultOptions.GitHub.Webhooks.Secret = "abcd" server.DefaultOptions.GitHub.Deployments.Environment = "test" return httptest.NewServer(server.New(e, server.DefaultOptions)) }
// NewServer builds a new empire.Empire instance and returns an httptest.Server // running the empire API. func NewServer(t testing.TB, e *empire.Empire) *httptest.Server { if e == nil { e = NewEmpire(t) } server.DefaultOptions.GitHub.Webhooks.Secret = "abcd" server.DefaultOptions.GitHub.Deployments.Environment = "test" server.DefaultOptions.Authenticator = auth.Anyone(&empire.User{Name: "fake"}) return httptest.NewServer(server.New(e, server.DefaultOptions)) }
func newServer(c *cli.Context, e *empire.Empire) http.Handler { var opts server.Options opts.Authenticator = newAuthenticator(c, e) opts.GitHub.Webhooks.Secret = c.String(FlagGithubWebhooksSecret) opts.GitHub.Deployments.Environments = strings.Split(c.String(FlagGithubDeploymentsEnvironments), ",") opts.GitHub.Deployments.ImageBuilder = newImageBuilder(c) opts.GitHub.Deployments.TugboatURL = c.String(FlagGithubDeploymentsTugboatURL) return server.New(e, opts) }
// NewTestServer returns a new httptest.Server for testing empire's http server. func NewTestServer(t testing.TB, e *empire.Empire, opts server.Options) *Server { if e == nil { e = NewEmpire(t) } s := server.New(e, opts) return &Server{ Empire: e, Server: httptest.NewServer(middleware.Handler(context.Background(), s)), } }
// NewServer builds a new empire.Empire instance and returns an httptest.Server // running the empire API. func NewServer(t testing.TB, e *empire.Empire) *httptest.Server { if e == nil { e = NewEmpire(t) } server.DefaultOptions.GitHub.Webhooks.Secret = "abcd" server.DefaultOptions.Authenticator = auth.Anyone(&empire.User{Name: "fake"}) server.DefaultOptions.GitHub.Deployments.Environments = []string{"test"} server.DefaultOptions.GitHub.Deployments.ImageBuilder = github.ImageFromTemplate(template.Must(template.New("image").Parse(github.DefaultTemplate))) return httptest.NewServer(server.New(e, server.DefaultOptions)) }
func newServer(c *cli.Context, e *empire.Empire) http.Handler { opts := server.Options{} opts.GitHub.ClientID = c.String(FlagGithubClient) opts.GitHub.ClientSecret = c.String(FlagGithubClientSecret) opts.GitHub.Organization = c.String(FlagGithubOrg) opts.GitHub.ApiURL = c.String(FlagGithubApiURL) opts.GitHub.Webhooks.Secret = c.String(FlagGithubWebhooksSecret) opts.GitHub.Deployments.Environment = c.String(FlagGithubDeploymentsEnvironment) opts.GitHub.Deployments.ImageTemplate = c.String(FlagGithubDeploymentsImageTemplate) opts.GitHub.Deployments.TugboatURL = c.String(FlagGithubDeploymentsTugboatURL) return server.New(e, opts) }
func newServer(c *cli.Context, e *empire.Empire) (http.Handler, error) { rootCtx, err := newRootContext(c) if err != nil { return nil, err } var opts server.Options opts.Authenticator = newAuthenticator(c, e) opts.GitHub.Webhooks.Secret = c.String(FlagGithubWebhooksSecret) opts.GitHub.Deployments.Environments = strings.Split(c.String(FlagGithubDeploymentsEnvironments), ",") opts.GitHub.Deployments.ImageBuilder = newImageBuilder(c) opts.GitHub.Deployments.TugboatURL = c.String(FlagGithubDeploymentsTugboatURL) h := middleware.Common(server.New(e, opts)) return middleware.Handler(rootCtx, h), nil }
// newTestServer returns a new httptest.Server for testing empire's http server. func newTestServer(t testing.TB, e *empire.Empire, opts server.Options) *Server { if e == nil { e = NewEmpire(t) } // Log reporter errors to stderr ctx := reporter.WithReporter(context.Background(), reporter.ReporterFunc(func(ctx context.Context, err error) error { fmt.Fprintf(os.Stderr, "reported error: %v\n", err) return nil })) s := server.New(e, opts) svr := httptest.NewUnstartedServer(middleware.Handler(ctx, s)) u, _ := url.Parse(svr.URL) s.URL = u return &Server{ Empire: e, Server: s, svr: svr, } }
func TestLoginTwoFactor(t *testing.T) { e := empiretest.NewEmpire(t) s := httptest.NewServer(server.New(e, server.Options{ Authenticator: auth.StaticAuthenticator("twofactor", "bar", "code", &empire.User{Name: "fake"}), })) defer s.Close() input := "twofactor\nbar\ncode\n" cmd := NewCmd(s.URL, "login") cmd.Stdin = strings.NewReader(input) out, err := cmd.CombinedOutput() if err != nil { t.Fatal(err) } if got, want := string(out), "Enter email: Enter two-factor auth code: Logged in.\n"; got != want { t.Fatalf("%q", got) } }
func TestLoginUnauthorized(t *testing.T) { e := empiretest.NewEmpire(t) s := httptest.NewServer(server.New(e, server.Options{ Authenticator: auth.StaticAuthenticator("fake", "bar", "", &empire.User{Name: "fake"}), })) defer s.Close() input := "foo\nbar\n" cmd := NewCmd(s.URL, "login") cmd.Stdin = strings.NewReader(input) out, err := cmd.CombinedOutput() if err == nil { t.Fatal("Expected an error") } if got, want := string(out), "Enter email: error: Request not authenticated, API token is missing, invalid or expired Log in with `emp login`.\n"; got != want { t.Fatalf("%q", got) } }