func TestUpdateUser(t *testing.T) {
	form := url.Values{}
	form.Add("name", names[1])
	body := strings.NewReader(form.Encode())
	r := httptest.NewRequest("POST", "/users/1/update", body)
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	w := httptest.NewRecorder()
	err := resource.AddUserSessionCookie(w, r, 1)
	if err != nil {
		t.Errorf("useractions: error setting session %s", err)
	}
	err = HandleUpdate(w, r)
	if err != nil {
		t.Errorf("useractions: error handling HandleUpdateUser %s", err)
	}
	if w.Code != http.StatusFound {
		t.Errorf("useractions: unexpected response code for HandleUpdateUser expected:%d got:%d", http.StatusFound, w.Code)
	}
	user, err := users.Find(1)
	if err != nil {
		t.Fatalf("useractions: error finding updated user %s", err)
	}
	if user.ID != 1 || user.Name != names[1] {
		t.Errorf("useractions: error with updated user values: %v", user)
	}
}
Test POST /users/123/update
package useractions
import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"
	"github.com/fragmenta/auth"
	"github.com/fragmenta/mux"
	"github.com/fragmenta/query"
	"github.com/fragmenta/fragmenta-cms/src/lib/resource"
	"github.com/fragmenta/fragmenta-cms/src/users"
)
var names = []string{"foo", "bar"}
func TestSetup(t *testing.T) {
	err := resource.SetupTestDatabase(3)
	if err != nil {
		fmt.Printf("users: Setup db failed %s", err)
	}
	resource.SetupAuthorisation()
	resource.SetupView(3)
	router := mux.New()
	mux.SetDefault(router)
	router.Add("/users", nil)
	router.Add("/users/create", nil)
	router.Add("/users/create", nil).Post()
	router.Add("/users/login", nil)
	router.Add("/users/login", nil).Post()
	router.Add("/users/login", nil).Post()
	router.Add("/users/logout", nil).Post()
	router.Add("/users/{id:\\d+}/update", nil)
	router.Add("/users/{id:\\d+}/update", nil).Post()
	router.Add("/users/{id:\\d+}/destroy", nil).Post()
	router.Add("/users/{id:\\d+}", nil)
	_, err = query.ExecSQL("delete from users;")
	if err != nil {
		t.Fatalf("error setting up:%s", err)
	}
	_, err = query.ExecSQL("INSERT INTO users (id,email,name,status,role,password_hash) VALUES(1,'example@example.com','test',100,100,'$2a$10$2IUzpI/yH0Xc.qs9Z5UUL.3f9bqi0ThvbKs6Q91UOlyCEGY8hdBw6');")
	if err != nil {
		t.Fatalf("error setting up:%s", err)
	}
	_, err = query.ExecSQL("INSERT INTO users (id,email,name,status,role,password_hash) VALUES(2,'example@example.com','test',100,0,'$2a$10$2IUzpI/yH0Xc.qs9Z5UUL.3f9bqi0ThvbKs6Q91UOlyCEGY8hdBw6');")
	if err != nil {
		t.Fatalf("error setting up:%s", err)
	}
	_, err = query.ExecSQL("ALTER SEQUENCE users_id_seq RESTART WITH 3;")
	if err != nil {
		t.Fatalf("error setting up:%s", err)
	}
}
func TestShowCreateUser(t *testing.T) {
	r := httptest.NewRequest("GET", "/users/create", nil)
	w := httptest.NewRecorder()
	err := resource.AddUserSessionCookie(w, r, 1)
	if err != nil {
		t.Errorf("useractions: error setting session %s", err)
	}
	err = HandleCreateShow(w, r)
	if err != nil || w.Code != http.StatusOK {
		t.Errorf("useractions: error handling HandleCreateShow %s", err)
	}
	pattern := "resource-update-form"
	if !strings.Contains(w.Body.String(), pattern) {
		t.Errorf("useractions: unexpected response for HandleCreateShow expected:%s got:%s", pattern, w.Body.String())
	}
}
func TestCreateUser(t *testing.T) {
	form := url.Values{}
	form.Add("name", names[0])
	body := strings.NewReader(form.Encode())
	r := httptest.NewRequest("POST", "/users/create", body)
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	w := httptest.NewRecorder()
	err := resource.AddUserSessionCookie(w, r, 1)
	if err != nil {
		t.Errorf("useractions: error setting session %s", err)
	}
	err = HandleCreate(w, r)
	if err != nil {
		t.Errorf("useractions: error handling HandleCreate %s", err)
	}
	if w.Code != http.StatusFound {
		t.Errorf("useractions: unexpected response code for HandleCreate expected:%d got:%d", http.StatusFound, w.Code)
	}
	allUsers, err := users.FindAll(users.Query().Order("id desc"))
	if err != nil || len(allUsers) == 0 {
		t.Fatalf("useractions: error finding created user %s", err)
	}
	newUser := allUsers[0]
	if newUser.ID < 2 || newUser.Name != names[0] {
		t.Errorf("useractions: error with created user values: %v %s", newUser.ID, newUser.Name)
	}
}
func TestListUsers(t *testing.T) {
	r := httptest.NewRequest("GET", "/users", nil)
	w := httptest.NewRecorder()
	err := resource.AddUserSessionCookie(w, r, 1)
	if err != nil {
		t.Errorf("useractions: error setting session %s", err)
	}
	err = HandleIndex(w, r)
	if err != nil || w.Code != http.StatusOK {
		t.Errorf("useractions: error handling HandleIndex %s", err)
	}
	pattern := "data-table-head"
	if !strings.Contains(w.Body.String(), pattern) {
		t.Errorf("useractions: unexpected response for HandleIndex expected:%s got:%s", pattern, w.Body.String())
	}
}
func TestShowUser(t *testing.T) {
	r := httptest.NewRequest("GET", "/users/1", nil)
	w := httptest.NewRecorder()
	err := resource.AddUserSessionCookie(w, r, 1)
	if err != nil {
		t.Errorf("useractions: error setting session %s", err)
	}
	err = HandleShow(w, r)
	if err != nil || w.Code != http.StatusOK {
		t.Errorf("useractions: error handling HandleShow %s", err)
	}
	pattern := names[0]
	if !strings.Contains(w.Body.String(), names[0]) {
		t.Errorf("useractions: unexpected response for HandleShow expected:%s got:%s", pattern, w.Body.String())
	}
}
func TestShowUpdateUser(t *testing.T) {
	r := httptest.NewRequest("GET", "/users/1/update", nil)
	w := httptest.NewRecorder()
	err := resource.AddUserSessionCookie(w, r, 1)
	if err != nil {
		t.Errorf("useractions: error setting session %s", err)
	}
	err = HandleUpdateShow(w, r)
	if err != nil || w.Code != http.StatusOK {
		t.Errorf("useractions: error handling HandleCreateShow %s", err)
	}
	pattern := "resource-update-form"
	if !strings.Contains(w.Body.String(), pattern) {
		t.Errorf("useractions: unexpected response for HandleCreateShow expected:%s got:%s", pattern, w.Body.String())
	}
}