<commit_before>// Copyright © 2014-2015 <NAME> <<EMAIL>>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package api

import (
	"bytes"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"github.com/rande/goapp"
	"github.com/rande/gonode/commands/server"
	"github.com/rande/gonode/core"
	"github.com/rande/gonode/plugins/user"
	"github.com/rande/gonode/test"
	"github.com/stretchr/testify/assert"
	"io"
	"net/http/httptest"
	"net/url"
	"testing"
)

func Test_Create_Username(t *testing.T) {
	test.RunHttpTest(t, func(t *testing.T, ts *httptest.Server, app *goapp.App) {

		configuration := app.Get("gonode.configuration").(*server.ServerConfig)

		// WITH
		// create a valid user into the database ...
		manager := app.Get("gonode.manager").(*core.PgNodeManager)

		u := app.Get("gonode.handler_collection").(core.HandlerCollection).NewNode("core.user")
		data := u.Data.(*user.User)
		data.Email = "<EMAIL>"
		data.Enabled = true
		data.FirstName = "Thomas"
		data.LastName = "Rxxxx"
		data.NewPassword = "<PASSWORD>"
		data.Username = "rande"

		meta := u.Meta.(*user.UserMeta)
		meta.PasswordCost = 1 // save test time

		manager.Save(u, false)

		res, _ := test.RunRequest("POST", fmt.Sprintf("%s/login", ts.URL), url.Values{
			"username": {data.Username},
			"password": {"<PASSWORD>"},
		})

		assert.Equal(t, 200, res.StatusCode)

		b := bytes.NewBuffer([]byte(""))
		io.Copy(b, res.Body)

		token, err := jwt.Parse(b.String(), func(token *jwt.Token) (interface{}, error) {
			// Don't forget to validate the alg is what you expect:
			if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
				return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
			}

			return []byte(configuration.Auth.Key), nil
		})

		assert.NotNil(t, configuration.Auth.Key)
		assert.True(t, len(configuration.Auth.Key) > 0)
		assert.Nil(t, err)
		assert.True(t, token.Valid)

		fmt.Printf("%v", token)
	})
}
<commit_msg>fix tests<commit_after>// Copyright © 2014-2015 <NAME> <<EMAIL>>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package api

import (
	"bytes"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"github.com/rande/goapp"
	"github.com/rande/gonode/core/config"
	"github.com/rande/gonode/core"
	"github.com/rande/gonode/plugins/user"
	"github.com/rande/gonode/test"
	"github.com/stretchr/testify/assert"
	"io"
	"net/http/httptest"
	"net/url"
	"testing"
)

func Test_Create_Username(t *testing.T) {
	test.RunHttpTest(t, func(t *testing.T, ts *httptest.Server, app *goapp.App) {

		configuration := app.Get("gonode.configuration").(*config.ServerConfig)

		// WITH
		// create a valid user into the database ...
		manager := app.Get("gonode.manager").(*core.PgNodeManager)

		u := app.Get("gonode.handler_collection").(core.HandlerCollection).NewNode("core.user")
		data := u.Data.(*user.User)
		data.Email = "<EMAIL>"
		data.Enabled = true
		data.FirstName = "Thomas"
		data.LastName = "Rxxxx"
		data.NewPassword = "<PASSWORD>"
		data.Username = "rande"

		meta := u.Meta.(*user.UserMeta)
		meta.PasswordCost = 1 // save test time

		manager.Save(u, false)

		res, _ := test.RunRequest("POST", fmt.Sprintf("%s/login", ts.URL), url.Values{
			"username": {data.Username},
			"password": {"<PASSWORD>"},
		})

		assert.Equal(t, 200, res.StatusCode)

		b := bytes.NewBuffer([]byte(""))
		io.Copy(b, res.Body)

		token, err := jwt.Parse(b.String(), func(token *jwt.Token) (interface{}, error) {
			// Don't forget to validate the alg is what you expect:
			if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
				return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
			}

			return []byte(configuration.Auth.Key), nil
		})

		assert.NotNil(t, configuration.Auth.Key)
		assert.True(t, len(configuration.Auth.Key) > 0)
		assert.Nil(t, err)
		assert.True(t, token.Valid)

		fmt.Printf("%v", token)
	})
}

{'fraction_non_alphanumeric': 0.18016194331983806, 'fraction_numerical': 0.006578947368421052, 'mean_word_length': 3.8207317073170732, 'pattern_counts': {'":': 4, '<': 15, '<?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': '6929752', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1447, 'n_words': 293}