package feishuapi

import (
	"context"
	"os"
	"testing"

	"github.com/onsi/gomega"
)

func Test_FetchTenantToken(t *testing.T) {
	g := gomega.NewWithT(t)
	appID := os.Getenv("APP_ID")
	appSecret := os.Getenv("APP_SECRET")
	if appID == "" || appSecret == "" {
		t.Skip("Set APP_ID and APP_SECRET to run this test")
	}

	cli, err := New(appID, appSecret)
	if err != nil {
		g.Expect(err).Should(gomega.Succeed())
	}
	token, err := cli.fetchTenantAccessToken(context.Background())
	if err != nil {
		g.Expect(err).Should(gomega.Succeed())
	}
	g.Expect(len(token) == 0).Should(gomega.BeFalse())
}

func TestClient_GetUserID(t *testing.T) {
	g := gomega.NewWithT(t)
	appID := os.Getenv("APP_ID")
	appSecret := os.Getenv("APP_SECRET")
	if appID == "" || appSecret == "" {
		t.Skip("Set APP_ID and APP_SECRET to run this test")
	}
	cli, err := New(appID, appSecret)
	g.Expect(err).Should(gomega.Succeed())
	{
		id, found, err := cli.GetUserID(context.Background(), "mashenjun@pingcap.com")
		g.Expect(err).Should(gomega.Succeed())
		g.Expect(found).Should(gomega.BeTrue())
		g.Expect(len(id) == 0).Should(gomega.BeFalse())
	}
	{
		id, found, err := cli.GetUserID(context.Background(), "wuyulin@pingcap.com")
		g.Expect(err).Should(gomega.Succeed())
		g.Expect(found).Should(gomega.BeFalse())
		g.Expect(len(id) == 0).Should(gomega.BeTrue())
	}
}

func TestClient_GetUserInfo(t *testing.T) {
	g := gomega.NewWithT(t)
	appID := os.Getenv("APP_ID")
	appSecret := os.Getenv("APP_SECRET")
	if appID == "" || appSecret == "" {
		t.Skip("Set APP_ID and APP_SECRET to run this test")
	}

	cli, err := New(appID, appSecret)
	g.Expect(err).Should(gomega.Succeed())
	id, _, err := cli.GetUserID(context.Background(), "mashenjun@pingcap.com")
	g.Expect(err).Should(gomega.Succeed())
	data, err := cli.GetUserInfo(context.Background(), id)
	g.Expect(err).Should(gomega.Succeed())
	g.Expect(data.IsResigned()).Should(gomega.BeFalse())
}
