user_auth_test.go 1.1 KB
package userAuth

import (
	"fmt"
	"github.com/tiptok/gocomm/pkg/redis"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
	"testing"
)

func initRedis() {
	redisSource := fmt.Sprintf("%v:%v", constant.REDIS_HOST, constant.REDIS_PORT)
	err := redis.InitWithDb(100, redisSource, constant.REDIS_AUTH, "0")
	if err != nil {
		panic(err)
	}
}

func TestRedisUserAuth(t *testing.T) {
	initRedis()

	userAuth := NewRedisUserAuth(WithUserId(1),
		WithAccessToken("abc"),
		WithRefreshToken("cdf"),
	)
	err := userAuth.AddAuth()
	if err != nil {
		t.Fatal(err)
	}
	_, err = userAuth.GetAuth()
	if err != nil {
		t.Fatal("get auth", err)
	}
	if !userAuth.Exist() {
		t.Fatal("except:true")
	}
	if err = userAuth.Check(NewOptions(WithAccessToken("abc"))); err != nil {
		t.Fatal("check accessToken:", err)
	}
	if err = userAuth.Check(NewOptions(WithRefreshToken("cdf"))); err != nil {
		t.Fatal("check refreshToken:", err)
	}
	if err = userAuth.RemoveAuth(); err != nil {
		t.Fatal("remove:", err)
	}
	if userAuth.field() != "1" {
		t.Fatal("except:1")
	}
	if userAuth.Exist() {
		t.Fatal("except:false")
	}
}