<commit_before>package handler_test

import (
	"encoding/json"
	"net/http"
	"testing"

	"github.com/gitpods/gitpods"
	"github.com/gitpods/gitpods/handler"
	"github.com/gitpods/gitpods/store"
	"github.com/stretchr/testify/assert"
)

func TestUserList(t *testing.T) {
	routerStore := DefaultRouterStore()
	r := DefaultTestAuthRouterWithStore(routerStore)

	res, content, err := Request(r, http.MethodGet, "/api/users", nil)

	assert.NoError(t, err)
	assert.Equal(t, http.StatusOK, res.StatusCode)
	assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
	assert.JSONEq(
		t,
		`[{"id":"25558000-2565-48dc-84eb-18754da2b0a2","username":"metalmatze","name":"<NAME>","email":"<EMAIL>"},{"id":"911d24ae-ad9b-4e50-bf23-9dcbdc8134c6","username":"tboerger","name":"<NAME>","email":"<EMAIL>"}]`,
		string(content),
	)
}

func TestUserListUnauthorized(t *testing.T) {
	r := DefaultTestRouter()
	res, content, err := Request(r, http.MethodGet, "/api/users", nil)

	assert.NoError(t, err)
	assertUnauthorized(t, res, content)
}

func TestUser(t *testing.T) {
	r := DefaultTestAuthRouter()
	res, content, err := Request(r, http.MethodGet, "/api/users/metalmatze", nil)

	assert.NoError(t, err)
	assert.Equal(t, http.StatusOK, res.StatusCode)
	assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
	assert.JSONEq(
		t,
		`{"id":"25558000-2565-48dc-84eb-18754da2b0a2","username":"metalmatze","name":"<NAME>","email":"<EMAIL>"}`,
		string(content),
	)
}

func TestUserNotFound(t *testing.T) {
	r := DefaultTestAuthRouter()
	res, content, err := Request(r, http.MethodGet, "/api/users/foobar", nil)

	assert.NoError(t, err)
	assert.Equal(t, http.StatusNotFound, res.StatusCode)
	assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
	assert.JSONEq(t, string(handler.JsonNotFound), string(content))
}

func TestUserCreate(t *testing.T) {
	routerStore := DefaultRouterStore()
	r := DefaultTestAuthRouterWithStore(routerStore)

	payloadUser := gitpod.User{
		ID:       "28195928-2e77-431b-b1fc-43f543cfdc2a",
		Username: "foobar",
		Name:     "<NAME>",<commit_msg>Fix import of gitpods root package<commit_after>package handler_test

import (
	"encoding/json"
	"net/http"
	"testing"

	"github.com/gitpods/gitpods"
	"github.com/gitpods/gitpods/handler"
	"github.com/gitpods/gitpods/store"
	"github.com/stretchr/testify/assert"
)

func TestUserList(t *testing.T) {
	routerStore := DefaultRouterStore()
	r := DefaultTestAuthRouterWithStore(routerStore)

	res, content, err := Request(r, http.MethodGet, "/api/users", nil)

	assert.NoError(t, err)
	assert.Equal(t, http.StatusOK, res.StatusCode)
	assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
	assert.JSONEq(
		t,
		`[{"id":"25558000-2565-48dc-84eb-18754da2b0a2","username":"metalmatze","name":"<NAME>","email":"<EMAIL>"},{"id":"911d24ae-ad9b-4e50-bf23-9dcbdc8134c6","username":"tboerger","name":"<NAME>","email":"<EMAIL>"}]`,
		string(content),
	)
}

func TestUserListUnauthorized(t *testing.T) {
	r := DefaultTestRouter()
	res, content, err := Request(r, http.MethodGet, "/api/users", nil)

	assert.NoError(t, err)
	assertUnauthorized(t, res, content)
}

func TestUser(t *testing.T) {
	r := DefaultTestAuthRouter()
	res, content, err := Request(r, http.MethodGet, "/api/users/metalmatze", nil)

	assert.NoError(t, err)
	assert.Equal(t, http.StatusOK, res.StatusCode)
	assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
	assert.JSONEq(
		t,
		`{"id":"25558000-2565-48dc-84eb-18754da2b0a2","username":"metalmatze","name":"<NAME>","email":"<EMAIL>"}`,
		string(content),
	)
}

func TestUserNotFound(t *testing.T) {
	r := DefaultTestAuthRouter()
	res, content, err := Request(r, http.MethodGet, "/api/users/foobar", nil)

	assert.NoError(t, err)
	assert.Equal(t, http.StatusNotFound, res.StatusCode)
	assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
	assert.JSONEq(t, string(handler.JsonNotFound), string(content))
}

func TestUserCreate(t *testing.T) {
	routerStore := DefaultRouterStore()
	r := DefaultTestAuthRouterWithStore(routerStore)

	payloadUser := gitpods.User{
		ID:       "28195928-2e77-431b-b1fc-43f543cfdc2a",
		Username: "foobar",
		Name:     "<NAME>",
{'fraction_non_alphanumeric': 0.18700327255726976, 'fraction_numerical': 0.041608228143992516, 'mean_word_length': 5.923948220064725, 'pattern_counts': {'":': 24, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7108882', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1638, 'n_words': 240}