示例#1
0
文件: handler_test.go 项目: tochti/sj
func Test_GET_LastWatchedList_OK(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatal(err)
	}

	userID := int64(1)

	expect := LastWatchedList{
		{userID, int64(1), 2, 3},
		{userID, int64(2), 4, 5},
	}

	s := "SELECT Series_ID, Session, Episode FROM %v"
	q := fmt.Sprintf(s, LastWatchedTable)
	rows := sqlmock.NewRows([]string{
		"Series_ID", "Session", "Episode",
	})

	for _, s := range expect {
		rows.AddRow(s.SeriesID, s.Session, s.Episode)
	}

	mock.ExpectQuery(q).WillReturnRows(rows)
	sessionStore := smem.NewStore()
	expires := time.Now().Add(1 * time.Hour)
	tmp := strconv.FormatInt(userID, 10)
	session, err := sessionStore.NewSession(tmp, expires)
	if err != nil {
		t.Fatal(err)
	}

	app := AppCtx{
		DB: db,
	}

	srv := gin.New()
	signedIn := kauth.SignedIn(&sessionStore)
	h := NewAppHandler(app, LastWatchedListHandler)
	srv.GET("/", signedIn(h))

	req := TestRequest{
		Body:    "",
		Handler: srv,
		Header:  http.Header{},
	}
	resp := req.SendWithToken("GET", "/", session.Token())

	if 200 != resp.Code {
		t.Fatal("Expect 200 was", resp.Code)
	}

	expectResp := NewSuccessResponse(expect)
	err = EqualResponse(expectResp, resp.Body)
	if err != nil {
		t.Fatal(err)
	}
}
示例#2
0
文件: handler_test.go 项目: tochti/sj
func Test_PATCH_SeriesList_OK(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatal(err)
	}

	userID := int64(1)
	seriesID := int64(2)

	q := fmt.Sprintf("INSERT INTO %v", SeriesListTable)
	mock.ExpectExec(q).
		WithArgs(userID, seriesID).
		WillReturnResult(sqlmock.NewResult(0, 1))

	app := AppCtx{
		DB: db,
	}

	sessionStore := smem.NewStore()
	expires := time.Now().Add(1 * time.Hour)
	tmp := strconv.FormatInt(userID, 10)
	session, err := sessionStore.NewSession(tmp, expires)
	if err != nil {
		t.Fatal(err)
	}

	srv := gin.New()
	signedIn := kauth.SignedIn(&sessionStore)
	h := NewAppHandler(app, AppendSeriesListHandler)
	srv.POST("/", signedIn(h))

	body := `
	{
		"Data": {
			"UserID": 1, 
			"SeriesID": 2
		}
	}
	`

	req := TestRequest{
		Body:    body,
		Handler: srv,
		Header:  http.Header{},
	}
	resp := req.SendWithToken("POST", "/", session.Token())

	if 200 != resp.Code {
		t.Fatal("Expect 200 was", resp.Code)
	}

	expect := NewSuccessResponse("")
	err = EqualResponse(expect, resp.Body)
	if err != nil {
		t.Fatal(err)
	}
}
示例#3
0
文件: srv.go 项目: tochti/sj-http
func main() {
	app, err := sj.NewApp("sj")
	handleErr(err)

	store := smem.NewStore()
	userStore := sj.NewUserStore(app.DB)

	signedIn := kauth.SignedIn(&store)
	signIn := kauth.SignIn(&store, userStore)

	newSeries := sj.NewAppHandler(app, sj.NewSeriesHandler)
	readSeries := sj.NewAppHandler(app, sj.ReadSeriesHandler)
	removeSeries := sj.NewAppHandler(app, sj.RemoveSeriesHandler)

	appendSeries := sj.NewAppHandler(app, sj.AppendSeriesListHandler)
	readSeriesList := sj.NewAppHandler(app, sj.ReadSeriesListHandler)

	lastWatchedList := sj.NewAppHandler(app, sj.LastWatchedListHandler)
	updatedLastWatched := sj.NewAppHandler(app, sj.UpdateLastWatchedHandler)

	newUser := sj.NewAppHandler(app, sj.NewUserHandler)

	publicDir := app.Specs.PublicDir
	htmlDir := path.Join(publicDir, "html")

	srv := gin.New()
	srv.POST("/Series", signedIn(newSeries))
	srv.GET("/Series/:id", signedIn(readSeries))
	srv.DELETE("/Series/:id", signedIn(removeSeries))
	srv.GET("/SignIn/:name/:password", signIn)
	srv.POST("/User", newUser)
	srv.PATCH("/AppendSeries", signedIn(appendSeries))
	srv.GET("/ReadSeriesList", signedIn(readSeriesList))
	srv.POST("/LastWatched", signedIn(updatedLastWatched))
	srv.GET("/LastWatchedList", signedIn(lastWatchedList))

	srv.Use(ginstatic.Serve("/", ginstatic.LocalFile(htmlDir, false)))
	srv.Static("/public", publicDir)
	srv.Static("/images", app.Specs.ImageDir)

	addr := fmt.Sprintf("%v:%v", app.Specs.Host, app.Specs.Port)
	err = srv.Run(addr)
	handleErr(err)

	os.Exit(0)
}
示例#4
0
文件: srv.go 项目: tochti/tasks-http
func main() {

	gumspecs.AppName = "tasks"

	srv := gumspecs.ReadHTTPServer()

	mysql := gumspecs.ReadMySQL()
	sqlDB, err := mysql.DB()
	if err != nil {
		log.Fatal(err)
	}

	db := &gorp.DbMap{
		Db: sqlDB,
		Dialect: gorp.MySQLDialect{
			"InnonDB",
			"UTF8",
		},
	}

	router := gin.New()

	s2tore := smem.NewStore()
	signedIn := gumauth.SignedIn(&s2tore)

	tasksAPI := router.Group("/v1/tasks")
	{
		g := gumwrap.Gorp
		tasksAPI.GET("/", signedIn(g(tasks.ReadAll, db)))
		tasksAPI.GET("/:id", signedIn(g(tasks.ReadOne, db)))
		tasksAPI.POST("/", signedIn(g(tasks.Create, db)))
		tasksAPI.PUT("/:id", signedIn(g(tasks.Update, db)))
		tasksAPI.DELETE("/:id", signedIn(g(tasks.Delete, db)))
	}

	uStore := gumauth.SQLUserStore{db.Db}
	router.POST("/signin/:name/:password", gumauth.SignIn(&s2tore, uStore))

	router.POST("/signup", gumauth.CreateUserSQL(db.Db))

	router.Run(srv.String())
}
示例#5
0
文件: handler_test.go 项目: tochti/sj
func Test_POST_UpdateLastWatched_OK(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatal(err)
	}

	userID := int64(1)
	seriesID := int64(2)
	lastSession := 3
	lastEpisode := 4

	q := fmt.Sprintf("REPLACE INTO %v", LastWatchedTable)
	mock.ExpectExec(q).
		WithArgs(userID, seriesID, lastSession, lastEpisode).
		WillReturnResult(sqlmock.NewResult(0, 1))

	sessionStore := smem.NewStore()
	expires := time.Now().Add(1 * time.Hour)
	tmp := strconv.FormatInt(userID, 10)
	session, err := sessionStore.NewSession(tmp, expires)
	if err != nil {
		t.Fatal(err)
	}

	app := AppCtx{
		DB: db,
	}
	srv := gin.New()
	signedIn := kauth.SignedIn(&sessionStore)
	lastWatchedHandler := NewAppHandler(app, UpdateLastWatchedHandler)
	srv.POST("/", signedIn(lastWatchedHandler))

	body := `
	{
		"Data": {
			"SeriesID": 2,
			"Session": 3,
			"Episode": 4
		}
	}
	`

	req := TestRequest{
		Body:    body,
		Handler: srv,
		Header:  http.Header{},
	}
	resp := req.SendWithToken("POST", "/", session.Token())

	if 200 != resp.Code {
		t.Fatal("Expect 200 was", resp.Code)
	}

	expect := NewSuccessResponse(LastWatched{
		UserID:   userID,
		SeriesID: seriesID,
		Session:  lastSession,
		Episode:  lastEpisode,
	})
	err = EqualResponse(expect, resp.Body)
	if err != nil {
		t.Fatal(err)
	}
}
示例#6
0
文件: handler_test.go 项目: tochti/sj
func Test_ReadSeriesListHandler_OK(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatal(err)
	}

	userID := int64(1)
	expect := SeriesList{
		{0, "Mr. Robot", "robot.png"},
		{1, "Narcos", "narcos.png"},
	}

	m := `SELECT series.ID as ID, series.Title as Title, series.Image as Image FROM %v as series, %v as list`
	q := fmt.Sprintf(m, SeriesTable, SeriesListTable)
	rows := sqlmock.NewRows([]string{"ID", "Title", "Image"})

	for _, s := range expect {
		rows.AddRow(s.ID, s.Title, s.Image)
	}
	mock.ExpectQuery(q).WillReturnRows(rows)

	app := AppCtx{
		DB: db,
	}

	sessionStore := smem.NewStore()
	expires := time.Now().Add(1 * time.Hour)
	tmp := strconv.FormatInt(userID, 10)
	session, err := sessionStore.NewSession(tmp, expires)
	if err != nil {
		t.Fatal(err)
	}

	srv := gin.New()
	readSeriesHandler := NewAppHandler(app, ReadSeriesListHandler)
	signedIn := kauth.SignedIn(&sessionStore)
	h := signedIn(readSeriesHandler)
	srv.GET("/", h)

	body := `
	{
		"Data": [
			{
				"ID": 0,
				"Title": "Mr. Robot", 
				"Image": "robot.png"
			},
			{
				"ID": 1,
				"Title": "Narcos",
				"Image": "narcos.png"
			}
		]
	}
	`

	req := TestRequest{
		Body:    body,
		Handler: srv,
		Header:  http.Header{},
	}
	resp := req.SendWithToken("GET", "/", session.Token())

	if 200 != resp.Code {
		t.Fatal("Expect 200 was", resp.Code)
	}

	expectResp := NewSuccessResponse(expect)
	err = EqualResponse(expectResp, resp.Body)
	if err != nil {
		t.Fatal(err)
	}
}