Example #1
0
// NewUser creates a user
func NewUser(ds *datastore.Datastore) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		body, err := ioutil.ReadAll(r.Body)
		if err != nil {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		var rd registerData
		err = json.Unmarshal(body, &rd)
		if err != nil {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		user, err := model.CreateUser(rd.Username, rd.Email, rd.Password)
		if err != nil {
			util.JSONError(w, err, http.StatusInternalServerError)
			return
		}

		err = ds.CreateUser(user)
		if err != nil {
			util.JSONError(w, err, http.StatusInternalServerError)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]string{"message": "user created successfully"})
	}
}
Example #2
0
func TestUserService(t *testing.T) {
	ds, err := NewDatastore()
	if err != nil {
		t.Fatalf("NewDatastore failed, %s", err)
	}

	u, err := model.CreateUser("luke", "*****@*****.**", "iminlovewithmysister")
	if err != nil {
		t.Errorf("model.CreateUser returned error %s", err)
	}

	err = ds.CreateUser(u)
	if err != nil {
		t.Errorf("create user returned error %s", err)
	}

	u, err = ds.GetUserByUsername("luke")
	if err != nil {
		t.Errorf("get user returned error %s", err)
	}

	u.Username = "******"
	err = ds.UpdateUser(u)
	if err != nil {
		t.Errorf("update user returned error %s", err)
	}

	u, err = ds.GetUserByUsername("dark luke")
	if err != nil {
		t.Errorf("get user %s returned error %s", u.Username, err)
	}

	u, err = ds.GetUserByID(u.ID)
	if err != nil {
		t.Errorf("get user by ID %s returned error %s", u.Username, err)
	}

	return
}
Example #3
0
func TestPostService(t *testing.T) {
	ds, err := NewDatastore()
	if err != nil {
		t.Fatalf("NewDatastore failed, %s", err)
	}

	board := &model.Board{
		Name:    "AskMeAnything",
		Creator: "fortytw2",
		Mods:    []string{"mod 1", "mod 2"},
	}

	// if this errors due to Unique constraint we're OK
	_ = ds.CreateBoard(board)

	b, err := ds.GetBoardByName("AskMeAnything")
	if err != nil {
		t.Errorf("something went wrong getting a board for post service, %s", err)
	}

	u, err := model.CreateUser("yolo", "*****@*****.**", "iminlovewiththecoco")
	if err != nil {
		t.Errorf("model.CreateUser returned error %s", err)
	}

	err = ds.CreateUser(u)
	if err != nil {
		t.Errorf("create user returned error %s", err)
	}

	u, err = ds.GetUserByUsername("yolo")
	if err != nil {
		t.Errorf("get user returned error %s", err)
	}

	posts := []*model.Post{
		&model.Post{
			Board: b.ID,
			OpID:  u.ID,
			Title: "reddit dies",
			Link:  "ellen.pao",
			Body:  "dis crazy",
		},
		&model.Post{
			Board: b.ID,
			OpID:  u.ID,
			Title: "reddit dies",
			Link:  "ellen.pao",
			Body:  "dis crazy",
		},
		&model.Post{
			Board: b.ID,
			OpID:  u.ID,
			Title: "reddit dies",
			Link:  "ellen.pao",
			Body:  "dis crazy",
		},
		&model.Post{
			Board: b.ID,
			OpID:  u.ID,
			Title: "reddit dies",
			Link:  "ellen.pao",
			Body:  "dis crazy",
		},
	}

	for _, post := range posts {
		err = ds.CreatePost(post)
		if err != nil {
			t.Errorf("creating a post doesn't seem to work right %s", err)
		}
	}

	ps, err := ds.GetUserPosts(u.ID, 0)
	if err != nil {
		t.Errorf("getting a users posts doesn't work... , %s", err)
	}

	if len(ps) != 4 {
		t.Error("something is wrong with the number of posts User1 has...")
	}

	ps, err = ds.GetBoardPostsByID(b.ID, 0)
	if err != nil {
		t.Errorf("getting board posts by id doesn't work... , %s", err)
	}

	if len(ps) != 4 {
		t.Error("something is wrong with the number of posts the board has...")
	}

	ps, err = ds.GetBoardPostsByName(b.Name, 0)
	if err != nil {
		t.Errorf("getting a boards posts by name doesn't work... , %s", err)
	}

	if len(ps) != 4 {
		t.Error("something is wrong with the number of posts the board has...")
	}

}
Example #4
0
func TestCommentService(t *testing.T) {
	ds, err := NewDatastore()
	if err != nil {
		t.Fatalf("NewDatastore failed, %s", err)
	}

	board := &model.Board{
		Name:    "AskMeNothing",
		Creator: "fortytw2",
		Mods:    []string{"mod 1", "mod 2"},
	}

	// if this errors due to Unique constraint we're OK
	_ = ds.CreateBoard(board)

	b, err := ds.GetBoardByName("AskMeNothing")
	if err != nil {
		t.Errorf("something went wrong getting a board for comment service, %s", err)
	}

	u, err := model.CreateUser("yoko", "*****@*****.**", "iminlovewiththecoco")
	if err != nil {
		t.Errorf("model.CreateUser returned error %s", err)
	}

	err = ds.CreateUser(u)
	if err != nil {
		t.Errorf("create user returned error %s", err)
	}

	u, err = ds.GetUserByUsername("yoko")
	if err != nil {
		t.Errorf("get user returned error %s", err)
	}

	err = ds.CreatePost(&model.Post{
		Board: b.ID,
		OpID:  u.ID,
		Title: "whoa dude",
		Link:  "this is crazy",
		Body:  "oh my god",
	})
	if err != nil {
		t.Errorf("error creating post, %s", err)
	}

	ps, err := ds.GetUserPosts(u.ID, 0)
	if err != nil {
		t.Errorf("getting a users posts doesn't work... , %s", err)
	}

	comments := []*model.Comment{
		model.NewComment(ps[0].ID, 0, u.ID, "root comment #1"),
		model.NewComment(ps[0].ID, 0, u.ID, "root comment #2"),
		model.NewComment(ps[0].ID, 0, u.ID, "root comment #3"),
	}

	for _, c := range comments {
		err = ds.CreateComment(c)
		if err != nil {
			t.Errorf("error creating comment, %s", err)
		}
	}

	cs, err := ds.GetPostComments(ps[0].ID)
	if err != nil {
		t.Errorf("error getting comments on a post, %s", err)
	}

	if cs == nil {
		t.Errorf("no comments returned for post")
	}

	if len(cs) != 3 && cs != nil {
		t.Errorf("should be 3 comments for post ID - %d, instead %d", ps[0].ID, len(cs))
	}

	err = ds.CreateComment(model.NewComment(ps[0].ID, cs[0].ID, u.ID, "CHILD COMMENT #1 CHECK IT"))
	if err != nil {
		t.Errorf("error creating comment, %s", err)
	}

	cs, err = ds.GetPostComments(ps[0].ID)
	if err != nil {
		t.Errorf("error getting comments on a post, %s", err)
	}

	if cs == nil {
		t.Errorf("no comments returned for post")
	}

}