loginsrv

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

commit 88d3f39c61977554aa61af8adef8ffad9fc5b523
parent d3d9047320034a21adb828ae29b274bfea4ac6ff
Author: Sebastian Mancke <s.mancke@tarent.de>
Date:   Sat, 19 Nov 2016 22:12:43 +0100

added integration test

Diffstat:
Mmain.go | 2+-
Amain_test.go | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/main.go b/main.go @@ -41,7 +41,7 @@ func logShutdownEvent() { }() } -func exit(signal os.Signal, err error) { +var exit = func(signal os.Signal, err error) { logging.LifecycleStop(applicationName, signal, err) if err == nil { os.Exit(0) diff --git a/main_test.go b/main_test.go @@ -0,0 +1,52 @@ +package main + +import ( + //"github.com/dgrijalva/jwt-go" + "github.com/stretchr/testify/assert" + //"github.com/tarent/loginsrv/login" + //"io/ioutil" + "net/http" + "os" + "strings" + "testing" + "time" +) + +func Test_BasicEndToEnd(t *testing.T) { + originalArgs := os.Args + + os.Args = []string{"loginsrv", "-host=localhost", "-port=3000", "-backend=provider=simple,bob=secret"} + defer func() { os.Args = originalArgs }() + + go main() + + time.Sleep(time.Second) + + // success + req, err := http.NewRequest("POST", "http://localhost:3000/context/login", strings.NewReader(`{"username": "bob", "password": "secret"}`)) + assert.NoError(t, err) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/jwt") + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + + assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, r.Header.Get("Content-Type"), "application/jwt") + + /** + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + fmt.Println(login.DefaultConfig.JwtSecret) + fmt.Println(string(b)) + token, err := jwt.Parse(string(b), func(*jwt.Token) (interface{}, error) { + return []byte(login.DefaultConfig.JwtSecret), nil + }) + assert.NoError(t, err) + + if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { + assert.Equal(t, "bob", claims["sub"]) + } else { + t.Fail() + } + **/ +}