package cache_test

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/adminroles"
	"github.com/fiskerinc/cloud-services/pkg/cache"
	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/db"
	"github.com/fiskerinc/cloud-services/pkg/db/queries"
	"github.com/fiskerinc/cloud-services/pkg/redis"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestIntegration(t *testing.T) {
	t.Skip()
	testKey := "YYYYYYY"
	q := queries.APITokens{}
	client := q.GetClient()
	client.GetConn().AddQueryHook(db.SQLLogger{})
	err := client.InitSchema([]interface{}{
		(*common.APIToken)(nil),
	})
	if err != nil {
		t.Error(err)
	}

	_, err = q.Insert(common.APIToken{
		Token: testKey,
		Roles: string(adminroles.RoleCreate),
	})
	if err != nil {
		t.Error(err)
	}

	type testCase struct {
		Name          string
		Key           string
		ExpectedRoles string
		ExpectedError string
		Setup         func(client redis.Client, db queries.APITokensInterface) error
		Teardown      func(client redis.Client, db queries.APITokensInterface) error
	}

	tests := []testCase{
		{
			Name:          "Invalid token",
			Key:           "XXXXXXXX",
			ExpectedError: "token not found",
		},
		{
			Name:          "From DB",
			Key:           testKey,
			ExpectedRoles: string(adminroles.RoleCreate),
		},
		{
			Name:          "From Cache",
			Key:           testKey,
			ExpectedRoles: string(adminroles.RoleCreate),
		},
		{
			Name:          "No token",
			Key:           "",
			ExpectedError: "token required",
		},
	}

	apitokens := cache.APITokenCache{
		APITokens: &q,
	}
	defer apitokens.Close()

	for _, test := range tests {
		value, err := apitokens.Get(test.Key)
		if err != nil && err.Error() != test.ExpectedError {
			t.Errorf(testhelper.TestErrorTemplate, test.Name, test.ExpectedError, err.Error())
		}
		if value != test.ExpectedRoles {
			t.Errorf(testhelper.TestErrorTemplate, test.Name, test.ExpectedRoles, value)
		}
	}

	q.Delete(testKey)
}
