示例#1
0
文件: login_test.go 项目: elos/gaia
func TestLoginPOST(t *testing.T) {
	ctx := context.Background()
	db := mem.NewDB()
	logger := services.NewTestLogger(t)
	m := http.NewServeMux()
	m.Handle("/login/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx, ok := routes.Authenticate(ctx, w, r, logger, db)
		if !ok {
			t.Fatal("bad authentication")
		}
		routes.LoginPOST(ctx, w, r, db, logger)
	}))
	m.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusAccepted)
	}))
	s := httptest.NewServer(http.HandlerFunc(m.ServeHTTP))
	defer s.Close()

	_, _, err := user.Create(db, "username", "password")
	if err != nil {
		t.Fatalf("user.Create(db, \"username\", \"password\") error: %s", err)
	}

	req, err := http.NewRequest("GET", s.URL+"/login/", new(bytes.Buffer))
	if err != nil {
		t.Fatalf("http.NewRequest error: %s", err)
	}
	req.SetBasicAuth("username", "password")

	jar, err := cookiejar.New(new(cookiejar.Options))
	if err != nil {
		t.Fatalf("cookiejar.New error: %s", err)
	}
	client := &http.Client{
		Jar: jar,
	}

	resp, err := client.Do(req)

	if err != nil {
		t.Fatalf("error posting to LoginPOST: %s", err)
	}

	t.Logf("Response:\n\t%v", resp)

	if got, want := resp.StatusCode, http.StatusAccepted; got != want {
		t.Errorf("resp.StatusCode: got %d, want %d", got, want)
	}

	iter, err := db.Query(models.SessionKind).Execute()
	if err != nil {
		t.Fatalf("db.Query(models.SessionKind).Execute(): %s", err)
	}

	seshes := mem.Slice(iter, func() data.Record { return new(models.Session) })

	if got, want := len(seshes), 1; got != want {
		t.Fatalf("len(seshes): got %d, want %d", got, want)
	}

	uri, err := url.Parse(s.URL)
	if err != nil {
		t.Fatalf("url.Prase(s.URL) error: %s", err)
	}

	t.Logf("URL: %s", uri)

	cookies := jar.Cookies(uri)
	if got, want := len(cookies), 1; got != want {
		t.Fatalf("len(cookies): got %d, want %d", got, want)
	}

	if got, want := cookies[0].Value, seshes[0].(*models.Session).Token; got != want {
		t.Errorf("cookies[0].Value: got %s, want %s", got, want)
	}
}
示例#2
0
文件: router.go 项目: elos/gaia
func router(ctx context.Context, m *Middleware, s *Services) (http.Handler, context.CancelFunc) {
	mux := http.NewServeMux()
	requestBackground, cancelAll := context.WithCancel(ctx)

	mux.Handle("/app/", http.StripPrefix("/app/", http.FileServer(s.AppFileSystem)))

	mux.HandleFunc(routes.Index, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			w.Write([]byte("Who is John Galt?"))
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /records/query/
	mux.HandleFunc(routes.RecordsQuery, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			routes.Records.QueryGET(ctx, w, r, s.WebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /records/new/
	mux.HandleFunc(routes.RecordsNew, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			routes.Records.NewGET(ctx, w, r, s.WebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /records/create/
	mux.HandleFunc(routes.RecordsCreate, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			routes.Records.CreateGET(ctx, w, r, s.WebUIClient)
		case "POST":
			routes.Records.CreatePOST(ctx, w, r, s.WebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /records/edit/
	mux.HandleFunc(routes.RecordsEdit, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			routes.Records.EditGET(ctx, w, r, s.WebUIClient)
		case "POST":
			routes.Records.EditPOST(ctx, w, r, s.WebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /records/view/
	mux.HandleFunc(routes.RecordsView, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			routes.Records.ViewGET(ctx, w, r, s.WebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /records/delete/
	mux.HandleFunc(routes.RecordsDelete, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "POST":
			routes.Records.DeletePOST(ctx, w, r, s.WebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /register/
	mux.HandleFunc(routes.Register, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "POST":
			routes.RegisterPOST(requestBackground, w, r, s.WebUIClient)
		case "GET":
			routes.RegisterGET(requestBackground, w, r, s.WebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /login/
	mux.HandleFunc(routes.Login, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "POST":
			routes.LoginPOST(ctx, w, r, s.WebUIClient)
		case "GET":
			routes.LoginGET(requestBackground, w, r, s.WebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /record/
	mux.HandleFunc(routes.Record, logRequest(cors(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "OPTIONS" {
			routes.RecordOPTIONS(requestBackground, w, r)
			return
		}

		ctx, ok := routes.Authenticate(requestBackground, w, r, s.Logger, s.DB)
		if !ok {
			return
		}

		switch r.Method {
		case "GET":
			routes.RecordGET(ctx, w, r, s.Logger, s.DB)
		case "POST":
			routes.RecordPOST(ctx, w, r, s.Logger, s.DB)
		case "DELETE":
			routes.RecordDELETE(ctx, w, r, s.Logger, s.DB)
		case "OPTIONS":
			routes.RecordOPTIONS(ctx, w, r)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}), s.Logger))

	// /record/query/
	mux.HandleFunc(routes.RecordQuery, logRequest(cors(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "OPTIONS" {
			routes.RecordOPTIONS(requestBackground, w, r)
			return
		}

		ctx, ok := routes.Authenticate(requestBackground, w, r, s.Logger, s.DB)
		if !ok {
			return
		}

		switch r.Method {
		case "POST":
			routes.RecordQueryPOST(ctx, w, r, s.Logger, s.DB)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}), s.Logger))

	// /event/
	mux.HandleFunc(routes.Event, logRequest(cors(func(w http.ResponseWriter, r *http.Request) {
		ctx, ok := routes.Authenticate(requestBackground, w, r, s.Logger, s.DB)
		if !ok {
			return
		}

		switch r.Method {
		case "POST":
			routes.EventPOST(ctx, w, r, s.DB, s.Logger)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}), s.Logger))

	// /record/changes/
	mux.HandleFunc(routes.RecordChanges, logRequest(websocket.Handler(
		routes.ContextualizeRecordChangesGET(requestBackground, s.DB, s.Logger),
	).ServeHTTP, s.Logger))

	// /command/sms/
	mux.HandleFunc(routes.CommandSMS, logRequest(func(w http.ResponseWriter, r *http.Request) {
		ctx := requestBackground

		switch r.Method {
		case "POST":
			routes.CommandSMSPOST(ctx, w, r, s.Logger, s.SMSCommandSessions)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /command/web/
	mux.HandleFunc(routes.CommandWeb, logRequest(websocket.Handler(
		routes.ContextualizeCommandWebGET(requestBackground, s.DB, s.Logger),
	).ServeHTTP, s.Logger))

	// /mobile/location/
	mux.HandleFunc(routes.MobileLocation, logRequest(func(w http.ResponseWriter, r *http.Request) {
		ctx, ok := routes.Authenticate(requestBackground, w, r, s.Logger, s.DB)
		if !ok {
			return
		}

		switch r.Method {
		case "POST":
			routes.MobileLocationPOST(ctx, w, r, s.Logger, s.DB)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// /cal/week/
	mux.HandleFunc(routes.CalWeek, logRequest(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			cal.WeekGET(ctx, w, r, s.CalWebUIClient)
		default:
			http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
			return
		}
	}, s.Logger))

	// Handle letsencrypt
	fs := http.FileServer(http.Dir("/var/www/elos/"))
	mux.Handle("/.well-known/", logRequest(func(w http.ResponseWriter, r *http.Request) {
		fs.ServeHTTP(w, r)
	}, s.Logger))

	return mux, cancelAll
}