<commit_before>package scaleway

import (
	"encoding/json"
	"fmt"
	"net/http"
	"path/filepath"
	"reflect"
	"testing"
	"time"
)

func TestTokensService_Create(t *testing.T) {

	setup()
	defer teardown()

	inBody := &TokenRequest{
		"foo@bar",
		"foobar",
		true,
	}

	data := testOpenFixture(t, filepath.Join(fixtureDir, "tokens_create.json"))

	mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
		v := new(TokenRequest)
		json.NewDecoder(r.Body).Decode(v)

		testMethod(t, r, "POST")
		if !reflect.DeepEqual(v, inBody) {
			t.Errorf("Request body = %+v, want %+v", v, inBody)
		}
		w.Header().Add("Content-Type", contentType)

		fmt.Fprint(w, string(data))
	})

	token, _, err := client.Tokens.Create(inBody)
	if err != nil {
		t.Errorf("Tokens.Create returned error: %v", err)
	}
	creationDate, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-22T08:06:51.742826+00:00")
	expires, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-20T14:05:06.393875+00:00")
	want := &Token{
		CreationDate:      Ntime(creationDate),
		Expires:           Ntime(expires),
		ID:                "654c95b0-2cf5-41a3-b3cc-733<PASSWORD>",
		InheritsUserPerms: true,
		UserID:            "5bea0358-db40-429e-bd82-953016a7e2s7",
		Permission:        []string{},
	}
	if !reflect.DeepEqual(token, want) {
		t.Errorf("Tokens.Create returned %+v\n, want %+v", token, want)
	}
}

func TestTokensService_List(t *testing.T) {
	setup()
	defer teardown()

	client.AuthToken = "<PASSWORD>"

	data := testOpenFixture(t, filepath.Join(fixtureDir, "tokens_list.json"))

	mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		w.Header().Add("Content-Type", contentType)
		fmt.Fprint(w, string(data))
	})

	tokens, _, err := client.Tokens.List()
	if err != nil {
		t.Errorf("Tokens.List returned error: %v", err)
	}
	creationDate, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-22T08:06:51.742826+00:00")
	want := []*Token{
		&Token{
			CreationDate:      Ntime(creationDate),
			Expires:           Ntime{},
			ID:                "654c95b0-2cf5-41a3-b3cc-733ffba4b4b7",
			InheritsUserPerms: true,
			UserID:            "5bea0358-db40-429e-bd82-953016a7e2s7",
			Permission:        []string{},
		},
	}
	if !reflect.DeepEqual(tokens, want) {
		t.Errorf("Tokens.Create returned %+v\n, want %+v", tokens, want)
	}
}

func TestTokensService_Get(t *testing.T) {
	setup()
	defer teardown()

	client.AuthToken = "<PASSWORD>"

	data := testOpenFixture(t, filepath.Join(fixtureDir, "tokens_get.json"))

	creationDate, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-22T08:06:51.742826+00:00")
	expires, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-20T14:05:06.393875+00:00")
	want := &Token{
		CreationDate:      Ntime(creationDate),
		Expires:           Ntime(expires),
		ID:                "654c95b0-2cf5-41a3-b3cc-733ffba4b4b7",
		InheritsUserPerms: true,
		UserID:            "5bea0358-db40-429e-bd82-953016a7e2s7",
		Permission:        []string{},
	}

	mux.HandleFunc(fmt.Sprintf("/tokens/%s", want.ID), func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		w.Header().Add("Content-Type", contentType)
		fmt.Fprint(w, string(data))
	})

	token, _, err := client.Tokens.Get(want.ID)
	if err != nil {
		t.Errorf("Tokens.Get returned error: %v", err)
	}
	if !reflect.DeepEqual(token, want) {
		t.Errorf("Tokens.Create returned %+v\n, want %+v", token, want)
	}
}

func TestTokensService_Update(t *testing.T) {
	setup()
	defer teardown()

	client.AuthToken = "<PASSWORD>"

	data := testOpenFixture(t, filepath.Join(fixtureDir, "tokens_update.json"))<commit_msg>Fix error message<commit_after>package scaleway

import (
	"encoding/json"
	"fmt"
	"net/http"
	"path/filepath"
	"reflect"
	"testing"
	"time"
)

func TestTokensService_Create(t *testing.T) {

	setup()
	defer teardown()

	inBody := &TokenRequest{
		"foo@bar",
		"foobar",
		true,
	}

	data := testOpenFixture(t, filepath.Join(fixtureDir, "tokens_create.json"))

	mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
		v := new(TokenRequest)
		json.NewDecoder(r.Body).Decode(v)

		testMethod(t, r, "POST")
		if !reflect.DeepEqual(v, inBody) {
			t.Errorf("Request body = %+v, want %+v", v, inBody)
		}
		w.Header().Add("Content-Type", contentType)

		fmt.Fprint(w, string(data))
	})

	token, _, err := client.Tokens.Create(inBody)
	if err != nil {
		t.Errorf("Tokens.Create returned error: %v", err)
	}
	creationDate, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-22T08:06:51.742826+00:00")
	expires, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-20T14:05:06.393875+00:00")
	want := &Token{
		CreationDate:      Ntime(creationDate),
		Expires:           Ntime(expires),
		ID:                "<PASSWORD>",
		InheritsUserPerms: true,
		UserID:            "5bea0358-db40-429e-bd82-953016a7e2s7",
		Permission:        []string{},
	}
	if !reflect.DeepEqual(token, want) {
		t.Errorf("Tokens.Create returned %+v\n, want %+v", token, want)
	}
}

func TestTokensService_List(t *testing.T) {
	setup()
	defer teardown()

	client.AuthToken = "<PASSWORD>"

	data := testOpenFixture(t, filepath.Join(fixtureDir, "tokens_list.json"))

	mux.HandleFunc("/tokens", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		w.Header().Add("Content-Type", contentType)
		fmt.Fprint(w, string(data))
	})

	tokens, _, err := client.Tokens.List()
	if err != nil {
		t.Errorf("Tokens.List returned error: %v", err)
	}
	creationDate, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-22T08:06:51.742826+00:00")
	want := []*Token{
		&Token{
			CreationDate:      Ntime(creationDate),
			Expires:           Ntime{},
			ID:                "654c95b0-2cf5-41a3-b3cc-733ffba4b4b7",
			InheritsUserPerms: true,
			UserID:            "5bea0358-db40-429e-bd82-953016a7e2s7",
			Permission:        []string{},
		},
	}
	if !reflect.DeepEqual(tokens, want) {
		t.Errorf("Tokens.Create returned %+v\n, want %+v", tokens, want)
	}
}

func TestTokensService_Get(t *testing.T) {
	setup()
	defer teardown()

	client.AuthToken = "<PASSWORD>"

	data := testOpenFixture(t, filepath.Join(fixtureDir, "tokens_get.json"))

	creationDate, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-22T08:06:51.742826+00:00")
	expires, _ := time.Parse("2006-01-02T15:04:05.999999Z07:00", "2014-05-20T14:05:06.393875+00:00")
	want := &Token{
		CreationDate:      Ntime(creationDate),
		Expires:           Ntime(expires),
		ID:                "654c95b0-2cf5-41a3-b3cc-7<PASSWORD>4b7",
		InheritsUserPerms: true,
		UserID:            "5bea0358-db40-429e-bd82-953016a7e2s7",
		Permission:        []string{},
	}

	mux.HandleFunc(fmt.Sprintf("/tokens/%s", want.ID), func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		w.Header().Add("Content-Type", contentType)
		fmt.Fprint(w, string(data))
	})

	token, _, err := client.Tokens.Get(want.ID)
	if err != nil {
		t.Errorf("Tokens.Get returned error: %v", err)
	}
	if !reflect.DeepEqual(token, want) {
		t.Errorf("Tokens.Get returned %+v\n, want %+v", token, want)
	}
}

func TestTokensService_Update(t *testing.T) {
	setup()
	defer teardown()

	client.AuthToken = "<PASSWORD>"

	data := testOpenFixture(t, filepath.Join(fixtureDir, "tokens_update.json"))
{'fraction_non_alphanumeric': 0.1787373599344083, 'fraction_numerical': 0.09428805684613283, 'mean_word_length': 3.9022103148024114, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '6109306', 'n_tokens_mistral': 3457, 'n_tokens_neox': 2975, 'n_words': 465}