loginsrv

Unnamed repository; edit this file 'description' to name the repository.
git clone git@jamesshield.xyz:repos/loginsrv.git
Log | Files | Refs | README | LICENSE

backend_test.go (1303B)


      1 package osiam
      2 
      3 import (
      4 	. "github.com/stretchr/testify/assert"
      5 	"github.com/tarent/loginsrv/model"
      6 	"net/http"
      7 	"net/http/httptest"
      8 	"testing"
      9 )
     10 
     11 func TestBackend_Authenticate(t *testing.T) {
     12 	server := httptest.NewServer(http.HandlerFunc(osiamMockHandler))
     13 	defer server.Close()
     14 
     15 	// positive case
     16 	backend, err := NewBackend(server.URL, "example-client", "secret")
     17 	NoError(t, err)
     18 	authenticated, userInfo, err := backend.Authenticate("admin", "koala")
     19 
     20 	NoError(t, err)
     21 	True(t, authenticated)
     22 	Equal(t,
     23 		model.UserInfo{
     24 			Origin: "osiam",
     25 			Sub:    "admin",
     26 		},
     27 		userInfo)
     28 
     29 	// wrong client credentials
     30 	backend, err = NewBackend(server.URL, "example-client", "XXX")
     31 	NoError(t, err)
     32 	authenticated, _, err = backend.Authenticate("admin", "koala")
     33 	Error(t, err)
     34 	False(t, authenticated)
     35 
     36 	// wrong user credentials
     37 	backend, err = NewBackend(server.URL, "example-client", "secret")
     38 	NoError(t, err)
     39 	authenticated, _, err = backend.Authenticate("admin", "XXX")
     40 	NoError(t, err)
     41 	False(t, authenticated)
     42 
     43 }
     44 
     45 func TestBackend_AuthenticateErrorCases(t *testing.T) {
     46 	_, err := NewBackend("://", "example-client", "secret")
     47 	Error(t, err)
     48 
     49 	_, err = NewBackend("http://example.com", "", "secret")
     50 	Error(t, err)
     51 
     52 	_, err = NewBackend("http://example.com", "example-client", "")
     53 	Error(t, err)
     54 }