Skip to content
Snippets Groups Projects
Select Git revision
  • 53137f562521ef0641b3954aec2d39e23a365f5d
  • ballinvoher default protected
  • client-http-server-for-token
  • master
  • gitlab-auth-issue
  • windows
  • microsoft
  • message
  • azure_auth
  • prometheus
  • permission-templates
  • no-datastore
  • save-public-keys
  • gitlab-group-level-start
  • v1.1.0
  • v1.0.0
  • v0.1
17 results

handlers_test.go

Blame
  • handlers_test.go 3.99 KiB
    package main
    
    import (
    	"bytes"
    	"encoding/json"
    	"io/ioutil"
    	"net/http"
    	"net/http/httptest"
    	"net/url"
    	"os"
    	"strings"
    	"testing"
    	"time"
    
    	"golang.org/x/crypto/ssh"
    	"golang.org/x/oauth2"
    
    	"github.com/gorilla/sessions"
    	"github.com/nsheridan/cashier/lib"
    	"github.com/nsheridan/cashier/server/auth"
    	"github.com/nsheridan/cashier/server/auth/testprovider"
    	"github.com/nsheridan/cashier/server/config"
    	"github.com/nsheridan/cashier/server/signer"
    	"github.com/nsheridan/cashier/server/store"
    	"github.com/nsheridan/cashier/testdata"
    	"github.com/stripe/krl"
    )
    
    func newContext(t *testing.T) *appContext {
    	f, err := ioutil.TempFile(os.TempDir(), "signing_key_")
    	if err != nil {
    		t.Error(err)
    	}
    	defer os.Remove(f.Name())
    	f.Write(testdata.Priv)
    	f.Close()
    	signer, err := signer.New(&config.SSH{
    		SigningKey: f.Name(),
    		MaxAge:     "1h",
    	})
    	if err != nil {
    		t.Error(err)
    	}
    	return &appContext{
    		cookiestore:  sessions.NewCookieStore([]byte("secret")),
    		authprovider: testprovider.New(),
    		certstore:    store.NewMemoryStore(),
    		authsession:  &auth.Session{AuthURL: "https://www.example.com/auth"},
    		sshKeySigner: signer,
    	}
    }
    
    func TestLoginHandler(t *testing.T) {
    	req, _ := http.NewRequest("GET", "/auth/login", nil)
    	resp := httptest.NewRecorder()
    	loginHandler(newContext(t), resp, req)
    	if resp.Code != http.StatusFound && resp.Header().Get("Location") != "https://www.example.com/auth" {
    		t.Error("Unexpected response")
    	}
    }
    
    func TestCallbackHandler(t *testing.T) {
    	req, _ := http.NewRequest("GET", "/auth/callback", nil)
    	req.Form = url.Values{"state": []string{"state"}, "code": []string{"abcdef"}}
    	resp := httptest.NewRecorder()
    	ctx := newContext(t)
    	ctx.setAuthStateCookie(resp, req, "state")
    	callbackHandler(ctx, resp, req)
    	if resp.Code != http.StatusFound && resp.Header().Get("Location") != "/" {
    		t.Error("Unexpected response")