func runCmdServe(cmd *cobra.Command, args []string) { session, err := r.Connect(r.ConnectOpts{ Address: serveOpts.ConnectionAddr, }) if err != nil { fmt.Printf("Opening RethinkDB session failed: %s", err) os.Exit(1) } userStore := users.NewUserStoreRethinkDB(session) wsContainer := restful.NewContainer() certs, err := auth.GenerateTestCerts() if err != nil { fmt.Printf("Opening RethinkDB session failed: %s", err) os.Exit(1) } jwtAuth := api.BuildJWTAuthFunc(userStore, certs) ar := api.NewAuthResource(userStore, jwtAuth, certs) ar.Register(wsContainer) ur := api.NewUserResource(userStore, jwtAuth) ur.Register(wsContainer) log.Printf("start listening on localhost:9090") server := &http.Server{Addr: ":9090", Handler: wsContainer} log.Fatal(server.ListenAndServe()) }
func TestAuthenticateUser(t *testing.T) { certs, err := auth.GenerateTestCerts() if err != nil { t.Errorf("error generating test certs %v", err) } store := users.NewUserStoreLocal() store.Create(NewUser()) ws := NewAuthResource(store, nil, certs) req := newFormRequest("POST", "http://api.his.com/users", bytes.NewBufferString("login=wolfeidau&password=Somewh3r3 there is a cow!")) recorder, resp := newResponse() ws.authenticateUser(req, resp) if recorder.Code != 200 { t.Errorf("expected 200 got %d %s", recorder.Code, recorder.Body.String()) } if recorder.Header().Get("Authorization") == "" { t.Errorf("expected authorization header to exist") } }