func TestDoubleRegisterUser(t *testing.T) { defer models.Db.Exec("DELETE FROM users") body := `{ "username": "******", "password": "******" }` recorder := registerUser(body) if recorder.Code != 200 { t.Errorf("Bad return code : %d", recorder.Code) } if recorder.Body.String() == "" { t.Error("No token") } recorder = registerUser(body) if recorder.Code != 401 { t.Errorf("Bad return code : %d", recorder.Code) } body = `{ "username": "******", "password": "******" }` recorder = registerUser(body) if recorder.Code != 200 { t.Errorf("Bad return code : %d", recorder.Code) } if recorder.Body.String() == "" { t.Error("No token") } count, err := models.CountUsers() if err != nil { t.Errorf("Error count user: %s", err) } if count != 2 { t.Errorf("Number user %d != 2 : ", count) } }
func TestUser(t *testing.T) { user := models.User{ Username: "******", Password: [64]byte{2, 3, 5, 7, 11, 13}, } defer models.Db.Exec("DELETE FROM users") //Check creation models.CreateUser(&user) count, err := models.CountUsers() if err != nil { t.Errorf("Error count user: %s", err) } if count != 1 { t.Errorf("Number user %d != 1 : ", count) } //Check find user2, err := models.FindUserByUsername("test") if err != nil { t.Errorf("Error finding user: %s", err) } if user2 == nil { t.Errorf("User return is null") } for i := 0; i < 64; i++ { if user2.Password[i] != user.Password[i] { t.Errorf("Bad password") break } } }