package notify

import (
	"context"
	"log"
	"net/http"
	"net/url"
	"testing"
	"time"

	"github.com/appleboy/gorush/config"
	"github.com/appleboy/gorush/status"

	"github.com/buger/jsonparser"
	"github.com/sideshow/apns2"
	"github.com/stretchr/testify/assert"
)

const (
	//nolint
	certificateValidP12 = `MIIKlgIBAzCCClwGCSqGSIb3DQEHAaCCCk0EggpJMIIKRTCCBMcGCSqGSIb3DQEHBqCCBLgwggS0AgEAMIIErQYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQYwDgQID/GJtcRhjvwCAggAgIIEgE5ralQoQBDgHgdp5+EwBaMjcZEJUXmYRdVCttIwfN2OxlIs54tob3/wpUyWGqJ+UXy9X+4EsWpDPUfTN/w88GMgj0kftpTqG0+3Hu/9pkZO4pdLCiyMGOJnXCOdhHFirtTXAR3QvnKKIpXIKrmZ4rcr/24Uvd/u669Tz8VDgcGOQazKeyvtdW7TJBxMFRv+IsQi/qCj5PkQ0jBbZ1LAc4C8mCMwOcH+gi/e471mzPWihQmynH2yJlZ4jb+taxQ/b8Dhlni2vcIMn+HknRk3Cyo8jfFvvO0BjvVvEAPxPJt7X96VFFS2KlyXjY3zt0siGrzQpczgPB/1vTqhQUvoOBw6kcXWgOjwt+gR8Mmo2DELnQqGhbYuWu52doLgVvD+zGr5vLYXHz6gAXnI6FVyHb+oABeBet3cer3EzGR7r+VoLmWSBm8SyRHwi0mxE63S7oD1j22jaTo7jnQBFZaY+cPaATcFjqW67x4j8kXh9NRPoINSgodLJrgmet2D1iOKuLTkCWf0UTi2HUkn9Zf0y+IIViZaVE4mWaGb9xTBClfa4KwM5gSz3jybksFKbtnzzPFuzClu+2mdthJs/58Ao40eyaykNmzSPhDv1F8Mai8bfaAqSdcBl5ZB2PF33xhuNSS4j2uIh1ICGv9DueyN507iEMQO2yCcaQTMKejV7/52h9LReS5/QPXDJhWMVpTb5FGCP7EmO0lZTeBNO5MlDzDQfz5xcFqHqfoby2sfAMU8HNB8wzdcwHtacgKGLBjLkapxyT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9WSGZTc0t4c2VXa2ZxaQp6Z1haNTFrUVRyVDZaYjZIWVJmd0MxbU1YSFdSS1J5WWpBbkN4VmltNllRZCtLVlQ0OWlSRERBaUlGb01HQTRpCnZ2Y0lsbmVxT1paUERJb0tKNjBJak8vRFpIV2t3NW1MamFJclQrcVEzWEFHZEpBMTNoY20KLS0tLS1FTkQgUlNBIFBSSVZBVEUgS0VZLS0tLS0K`
	//nolint
	authkeyInvalidP8 = `TUlHSEFnRUFNQk1HQnlxR1NNNDlBZ0VHQ0NxR1NNNDlBd0VIQkcwd2F3SUJBUVFnRWJWemZQblpQeGZBeXhxRQpaVjA1bGFBb0pBbCsvNlh0Mk80bU9CNjExc09oUkFOQ0FBU2dGVEtqd0pBQVU5NWcrKy92ektXSGt6QVZtTk1JCnRCNXZUalpPT0l3bkViNzBNc1daRkl5VUZEMVA5R3dzdHo0K2FrSFg3dkk4Qkg2aEhtQm1mWlpaCg==`
	//nolint
	authkeyValidP8 = `LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR0hBZ0VBTUJNR0J5cUdTTTQ5QWdFR0NDcUdTTTQ5QXdFSEJHMHdhd0lCQVFRZ0ViVnpmUG5aUHhmQXl4cUUKWlYwNWxhQW9KQWwrLzZYdDJPNG1PQjYxMXNPaFJBTkNBQVNnRlRLandKQUFVOTVnKysvdnpLV0hrekFWbU5NSQp0QjV2VGpaT09Jd25FYjcwTXNXWkZJeVVGRDFQOUd3c3R6NCtha0hYN3ZJOEJINmhIbUJtZmVRbAotLS0tLUVORCBQUklWQVRFIEtFWS0tLS0tCg==`
)

var (
	testMessage    = "test"
	testKeyPathP8  = "../certificate/authkey-valid.p8"
	testKeyPath    = "../certificate/certificate-valid.pem"
	welcomeMessage = "Welcome notification Server"
)

func TestDisabledAndroidIosConf(t *testing.T) {
	cfg, _ := config.LoadConf()
	cfg.Android.Enabled = false
	cfg.Huawei.Enabled = false

	err := CheckPushConf(cfg)

	assert.Error(t, err)
	assert.Equal(t, "please enable iOS, Android or Huawei config in yml config", err.Error())
}

func TestMissingIOSCertificate(t *testing.T) {
	cfg, _ := config.LoadConf()

	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = ""
	cfg.Ios.KeyBase64 = ""
	err := CheckPushConf(cfg)

	assert.Error(t, err)
	assert.Equal(t, "missing iOS certificate key", err.Error())

	cfg.Ios.KeyPath = "test.pem"
	err = CheckPushConf(cfg)

	assert.Error(t, err)
	assert.Equal(t, "certificate file does not exist", err.Error())
}

func TestIOSNotificationStructure(t *testing.T) {
	var dat map[string]interface{}
	unix := time.Now().Unix()

	expectBadge := 0
	message := welcomeMessage
	expiration := time.Now().Unix()
	req := &PushNotification{
		ApnsID:     testMessage,
		Topic:      testMessage,
		Expiration: &expiration,
		Priority:   "normal",
		Message:    message,
		Badge:      &expectBadge,
		Sound: Sound{
			Critical: 1,
			Name:     testMessage,
			Volume:   1.0,
		},
		ContentAvailable: true,
		Data: D{
			"key1": "test",
			"key2": 2,
		},
		Category: testMessage,
		URLArgs:  []string{"a", "b"},
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		panic(err)
	}

	alert, _ := jsonparser.GetString(data, "aps", "alert")
	badge, _ := jsonparser.GetInt(data, "aps", "badge")
	soundName, _ := jsonparser.GetString(data, "aps", "sound", "name")
	soundCritical, _ := jsonparser.GetInt(data, "aps", "sound", "critical")
	soundVolume, _ := jsonparser.GetFloat(data, "aps", "sound", "volume")
	contentAvailable, _ := jsonparser.GetInt(data, "aps", "content-available")
	category, _ := jsonparser.GetString(data, "aps", "category")
	key1 := dat["key1"]
	key2 := dat["key2"]
	aps := dat["aps"].(map[string]interface{})
	urlArgs := aps["url-args"].([]interface{})

	assert.Equal(t, testMessage, notification.ApnsID)
	assert.Equal(t, testMessage, notification.Topic)
	assert.Equal(t, unix, notification.Expiration.Unix())
	assert.Equal(t, ApnsPriorityLow, notification.Priority)
	assert.Equal(t, message, alert)
	assert.Equal(t, expectBadge, int(badge))
	assert.Equal(t, expectBadge, *req.Badge)
	assert.Equal(t, testMessage, soundName)
	assert.Equal(t, 1.0, soundVolume)
	assert.Equal(t, int64(1), soundCritical)
	assert.Equal(t, 1, int(contentAvailable))
	assert.Equal(t, "test", key1)
	assert.Equal(t, 2, int(key2.(float64)))
	assert.Equal(t, testMessage, category)
	assert.Contains(t, urlArgs, "a")
	assert.Contains(t, urlArgs, "b")
}

func TestIOSSoundAndVolume(t *testing.T) {
	var dat map[string]interface{}

	message := welcomeMessage
	req := &PushNotification{
		ApnsID:   testMessage,
		Topic:    testMessage,
		Priority: "normal",
		Message:  message,
		Sound: Sound{
			Critical: 3,
			Name:     testMessage,
			Volume:   4.5,
		},
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		panic(err)
	}

	alert, _ := jsonparser.GetString(data, "aps", "alert")
	soundName, _ := jsonparser.GetString(data, "aps", "sound", "name")
	soundCritical, _ := jsonparser.GetInt(data, "aps", "sound", "critical")
	soundVolume, _ := jsonparser.GetFloat(data, "aps", "sound", "volume")

	assert.Equal(t, testMessage, notification.ApnsID)
	assert.Equal(t, testMessage, notification.Topic)
	assert.Equal(t, ApnsPriorityLow, notification.Priority)
	assert.Equal(t, message, alert)
	assert.Equal(t, testMessage, soundName)
	assert.Equal(t, 4.5, soundVolume)
	assert.Equal(t, int64(3), soundCritical)

	req.SoundName = "foobar"
	req.SoundVolume = 5.5
	notification = GetIOSNotification(req)
	dump, _ = json.Marshal(notification.Payload)
	data = []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		panic(err)
	}

	soundName, _ = jsonparser.GetString(data, "aps", "sound", "name")
	soundVolume, _ = jsonparser.GetFloat(data, "aps", "sound", "volume")
	soundCritical, _ = jsonparser.GetInt(data, "aps", "sound", "critical")
	assert.Equal(t, 5.5, soundVolume)
	assert.Equal(t, int64(1), soundCritical)
	assert.Equal(t, "foobar", soundName)

	req = &PushNotification{
		ApnsID:   testMessage,
		Topic:    testMessage,
		Priority: "normal",
		Message:  message,
		Sound: map[string]interface{}{
			"critical": 3,
			"name":     "test",
			"volume":   4.5,
		},
	}

	notification = GetIOSNotification(req)
	dump, _ = json.Marshal(notification.Payload)
	data = []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		panic(err)
	}

	soundName, _ = jsonparser.GetString(data, "aps", "sound", "name")
	soundVolume, _ = jsonparser.GetFloat(data, "aps", "sound", "volume")
	soundCritical, _ = jsonparser.GetInt(data, "aps", "sound", "critical")
	assert.Equal(t, 4.5, soundVolume)
	assert.Equal(t, int64(3), soundCritical)
	assert.Equal(t, "test", soundName)

	req = &PushNotification{
		ApnsID:   testMessage,
		Topic:    testMessage,
		Priority: "normal",
		Message:  message,
		Sound:    "default",
	}

	notification = GetIOSNotification(req)
	dump, _ = json.Marshal(notification.Payload)
	data = []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		panic(err)
	}

	soundName, _ = jsonparser.GetString(data, "aps", "sound")
	assert.Equal(t, "default", soundName)
}

func TestIOSSummaryArg(t *testing.T) {
	var dat map[string]interface{}

	message := welcomeMessage
	req := &PushNotification{
		ApnsID:   testMessage,
		Topic:    testMessage,
		Priority: "normal",
		Message:  message,
		Alert: Alert{
			SummaryArg:      "test",
			SummaryArgCount: 3,
		},
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		panic(err)
	}

	assert.Equal(t, testMessage, notification.ApnsID)
	assert.Equal(t, testMessage, notification.Topic)
	assert.Equal(t, ApnsPriorityLow, notification.Priority)
	assert.Equal(t, "test", dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["summary-arg"])
	assert.Equal(t, float64(3), dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["summary-arg-count"])
}

// Silent Notification which payload’s aps dictionary must not contain the alert, sound, or badge keys.
// ref: https://goo.gl/m9xyqG
func TestSendZeroValueForBadgeKey(t *testing.T) {
	var dat map[string]interface{}

	message := welcomeMessage
	req := &PushNotification{
		ApnsID:           testMessage,
		Topic:            testMessage,
		Priority:         "normal",
		Message:          message,
		Sound:            testMessage,
		ContentAvailable: true,
		MutableContent:   true,
		ThreadID:         testMessage,
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		log.Println(err)
		panic(err)
	}

	alert, _ := jsonparser.GetString(data, "aps", "alert")
	badge, _ := jsonparser.GetInt(data, "aps", "badge")
	sound, _ := jsonparser.GetString(data, "aps", "sound")
	threadID, _ := jsonparser.GetString(data, "aps", "thread-id")
	contentAvailable, _ := jsonparser.GetInt(data, "aps", "content-available")
	mutableContent, _ := jsonparser.GetInt(data, "aps", "mutable-content")

	if req.Badge != nil {
		t.Errorf("req.Badge must be nil")
	}

	assert.Equal(t, testMessage, notification.ApnsID)
	assert.Equal(t, testMessage, notification.Topic)
	assert.Equal(t, ApnsPriorityLow, notification.Priority)
	assert.Equal(t, message, alert)
	assert.Equal(t, 0, int(badge))
	assert.Equal(t, testMessage, sound)
	assert.Equal(t, testMessage, threadID)
	assert.Equal(t, 1, int(contentAvailable))
	assert.Equal(t, 1, int(mutableContent))

	// Add Bage
	expectBadge := 10
	req.Badge = &expectBadge

	notification = GetIOSNotification(req)

	dump, _ = json.Marshal(notification.Payload)
	data = []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		log.Println(err)
		panic(err)
	}

	if req.Badge == nil {
		t.Errorf("req.Badge must be equal %d", *req.Badge)
	}

	badge, _ = jsonparser.GetInt(data, "aps", "badge")
	assert.Equal(t, expectBadge, *req.Badge)
	assert.Equal(t, expectBadge, int(badge))
}

// Silent Notification:
// The payload’s aps dictionary must include the content-available key with a value of 1.
// The payload’s aps dictionary must not contain the alert, sound, or badge keys.
// ref: https://goo.gl/m9xyqG
func TestCheckSilentNotification(t *testing.T) {
	var dat map[string]interface{}

	req := &PushNotification{
		ApnsID:           testMessage,
		Topic:            testMessage,
		CollapseID:       testMessage,
		Priority:         "normal",
		ContentAvailable: true,
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		log.Println(err)
		panic(err)
	}

	assert.Equal(t, testMessage, notification.CollapseID)
	assert.Equal(t, testMessage, notification.ApnsID)
	assert.Equal(t, testMessage, notification.Topic)
	assert.Nil(t, dat["aps"].(map[string]interface{})["alert"])
	assert.Nil(t, dat["aps"].(map[string]interface{})["sound"])
	assert.Nil(t, dat["aps"].(map[string]interface{})["badge"])
}

// URL: https://goo.gl/5xFo3C
// Example 2
//
//	{
//	    "aps" : {
//	        "alert" : {
//	            "title" : "Game Request",
//	            "body" : "Bob wants to play poker",
//	            "action-loc-key" : "PLAY"
//	        },
//	        "badge" : 5
//	    },
//	    "acme1" : "bar",
//	    "acme2" : [ "bang",  "whiz" ]
//	}
func TestAlertStringExample2ForIos(t *testing.T) {
	var dat map[string]interface{}

	title := "Game Request"
	body := "Bob wants to play poker"
	actionLocKey := "PLAY"
	req := &PushNotification{
		ApnsID:   testMessage,
		Topic:    testMessage,
		Priority: "normal",
		Alert: Alert{
			Title:        title,
			Body:         body,
			ActionLocKey: actionLocKey,
		},
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		log.Println(err)
		panic(err)
	}

	assert.Equal(t, title, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["title"])
	assert.Equal(t, body, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["body"])
	assert.Equal(t, actionLocKey, dat["aps"].(map[string]interface{})["alert"].(map[string]interface{})["action-loc-key"])
}

// URL: https://goo.gl/5xFo3C
// Example 3
//
//	{
//	    "aps" : {
//	        "alert" : "You got your emails.",
//	        "badge" : 9,
//	        "sound" : "bingbong.aiff"
//	    },
//	    "acme1" : "bar",
//	    "acme2" : 42
//	}
func TestAlertStringExample3ForIos(t *testing.T) {
	var dat map[string]interface{}

	badge := 9
	sound := "bingbong.aiff"
	req := &PushNotification{
		ApnsID:           testMessage,
		Topic:            testMessage,
		Priority:         "normal",
		ContentAvailable: true,
		Message:          testMessage,
		Badge:            &badge,
		Sound:            sound,
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		log.Println(err)
		panic(err)
	}

	assert.Equal(t, sound, dat["aps"].(map[string]interface{})["sound"])
	assert.Equal(t, float64(badge), dat["aps"].(map[string]interface{})["badge"].(float64))
	assert.Equal(t, testMessage, dat["aps"].(map[string]interface{})["alert"])
}

func TestMessageAndTitle(t *testing.T) {
	var dat map[string]interface{}

	message := welcomeMessage
	title := "Welcome notification Server title"
	req := &PushNotification{
		ApnsID:           testMessage,
		Topic:            testMessage,
		Priority:         "normal",
		Message:          message,
		Title:            title,
		ContentAvailable: true,
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		log.Println(err)
		panic(err)
	}

	alert, _ := jsonparser.GetString(data, "aps", "alert")
	alertBody, _ := jsonparser.GetString(data, "aps", "alert", "body")
	alertTitle, _ := jsonparser.GetString(data, "aps", "alert", "title")

	assert.Equal(t, testMessage, notification.ApnsID)
	assert.Equal(t, ApnsPriorityLow, notification.Priority)
	assert.Equal(t, message, alertBody)
	assert.Equal(t, title, alertTitle)
	assert.NotEqual(t, message, alert)

	// Add alert body
	messageOverride := "Welcome notification Server overridden"
	req.Alert.Body = messageOverride

	notification = GetIOSNotification(req)

	dump, _ = json.Marshal(notification.Payload)
	data = []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		log.Println(err)
		panic(err)
	}

	alertBodyOverridden, _ := jsonparser.GetString(data, "aps", "alert", "body")
	alertTitle, _ = jsonparser.GetString(data, "aps", "alert", "title")
	assert.Equal(t, messageOverride, alertBodyOverridden)
	assert.NotEqual(t, message, alertBodyOverridden)
	assert.Equal(t, title, alertTitle)
}

func TestIOSAlertNotificationStructure(t *testing.T) {
	var dat map[string]interface{}
	unix := time.Now().Unix()
	staleDate := time.Now().Unix()
	dismissalDate := staleDate + 5
	timeStamp := time.Now().Unix()
	itemID := float64(12345)

	req := &PushNotification{
		Message: "Welcome",
		Title:   testMessage,
		Alert: Alert{
			Action:       testMessage,
			ActionLocKey: testMessage,
			Body:         testMessage,
			LaunchImage:  testMessage,
			LocArgs:      []string{"a", "b"},
			LocKey:       testMessage,
			Subtitle:     testMessage,
			TitleLocArgs: []string{"a", "b"},
			TitleLocKey:  testMessage,
		},
		InterruptionLevel: testMessage,
		StaleDate:         staleDate,
		DismissalDate:     dismissalDate,
		Event:             testMessage,
		Timestamp:         timeStamp,
		ContentState: D{
			"item_id":   itemID,
			"item_name": testMessage,
		},
	}

	notification := GetIOSNotification(req)

	dump, _ := json.Marshal(notification.Payload)
	data := []byte(string(dump))

	if err := json.Unmarshal(data, &dat); err != nil {
		log.Println(err)
		panic(err)
	}

	action, _ := jsonparser.GetString(data, "aps", "alert", "action")
	actionLocKey, _ := jsonparser.GetString(data, "aps", "alert", "action-loc-key")
	body, _ := jsonparser.GetString(data, "aps", "alert", "body")
	launchImage, _ := jsonparser.GetString(data, "aps", "alert", "launch-image")
	locKey, _ := jsonparser.GetString(data, "aps", "alert", "loc-key")
	title, _ := jsonparser.GetString(data, "aps", "alert", "title")
	subtitle, _ := jsonparser.GetString(data, "aps", "alert", "subtitle")
	titleLocKey, _ := jsonparser.GetString(data, "aps", "alert", "title-loc-key")
	interruptionLevel, _ := jsonparser.GetString(data, "aps", "interruption-level")
	staleDate, _ = jsonparser.GetInt(data, "aps", "stale-date")
	event, _ := jsonparser.GetString(data, "aps", "event")
	timestamp, _ := jsonparser.GetInt(data, "aps", "timestamp")
	aps := dat["aps"].(map[string]interface{})
	alert := aps["alert"].(map[string]interface{})
	titleLocArgs := alert["title-loc-args"].([]interface{})
	locArgs := alert["loc-args"].([]interface{})
	contentState := aps["content-state"].(map[string]interface{})
	contentStateItemID := contentState["item_id"]
	contentStateItemName := contentState["item_name"]

	assert.Equal(t, testMessage, action)
	assert.Equal(t, testMessage, actionLocKey)
	assert.Equal(t, testMessage, body)
	assert.Equal(t, testMessage, launchImage)
	assert.Equal(t, testMessage, locKey)
	assert.Equal(t, testMessage, title)
	assert.Equal(t, testMessage, subtitle)
	assert.Equal(t, testMessage, titleLocKey)
	assert.Equal(t, testMessage, interruptionLevel)
	assert.Equal(t, testMessage, event)
	assert.Equal(t, unix, staleDate)
	assert.Equal(t, unix, timestamp)

	// dynamic contentState content
	assert.Equal(t, contentStateItemID, itemID)
	assert.Equal(t, contentStateItemName, testMessage)
	assert.Contains(t, contentState, "item_id")
	assert.Contains(t, contentState, "item_name")

	assert.Contains(t, titleLocArgs, "a")
	assert.Contains(t, titleLocArgs, "b")
	assert.Contains(t, locArgs, "a")
	assert.Contains(t, locArgs, "b")
}

func TestWrongIosCertificateExt(t *testing.T) {
	cfg, _ := config.LoadConf()

	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = "test"
	err := InitAPNSClient(context.Background(), cfg)

	assert.Error(t, err)
	assert.Equal(t, "wrong certificate key extension", err.Error())

	cfg.Ios.KeyPath = ""
	cfg.Ios.KeyBase64 = "abcd"
	cfg.Ios.KeyType = "abcd"
	err = InitAPNSClient(context.Background(), cfg)

	assert.Error(t, err)
	assert.Equal(t, "wrong certificate key type", err.Error())
}

func TestAPNSClientDevHost(t *testing.T) {
	cfg, _ := config.LoadConf()

	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = "../certificate/certificate-valid.p12"
	err := InitAPNSClient(context.Background(), cfg)
	assert.Nil(t, err)
	assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)

	cfg.Ios.KeyPath = ""
	cfg.Ios.KeyBase64 = certificateValidP12
	cfg.Ios.KeyType = "p12"
	err = InitAPNSClient(context.Background(), cfg)
	assert.Nil(t, err)
	assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)
}

func TestAPNSClientProdHost(t *testing.T) {
	cfg, _ := config.LoadConf()

	cfg.Ios.Enabled = true
	cfg.Ios.Production = true
	cfg.Ios.KeyPath = testKeyPath
	err := InitAPNSClient(context.Background(), cfg)
	assert.Nil(t, err)
	assert.Equal(t, apns2.HostProduction, ApnsClient.Host)

	cfg.Ios.KeyPath = ""
	cfg.Ios.KeyBase64 = certificateValidPEM
	cfg.Ios.KeyType = "pem"
	err = InitAPNSClient(context.Background(), cfg)
	assert.Nil(t, err)
	assert.Equal(t, apns2.HostProduction, ApnsClient.Host)
}

func TestAPNSClientInvaildToken(t *testing.T) {
	cfg, _ := config.LoadConf()

	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = "../certificate/authkey-invalid.p8"
	err := InitAPNSClient(context.Background(), cfg)
	assert.Error(t, err)

	cfg.Ios.KeyPath = ""
	cfg.Ios.KeyBase64 = authkeyInvalidP8
	cfg.Ios.KeyType = "p8"
	err = InitAPNSClient(context.Background(), cfg)
	assert.Error(t, err)

	// empty key-id or team-id
	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = testKeyPathP8
	err = InitAPNSClient(context.Background(), cfg)
	assert.Error(t, err)

	cfg.Ios.KeyID = "key-id"
	cfg.Ios.TeamID = ""
	err = InitAPNSClient(context.Background(), cfg)
	assert.Error(t, err)

	cfg.Ios.KeyID = ""
	cfg.Ios.TeamID = "team-id"
	err = InitAPNSClient(context.Background(), cfg)
	assert.Error(t, err)
}

func TestAPNSClientVaildToken(t *testing.T) {
	cfg, _ := config.LoadConf()

	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = testKeyPathP8
	cfg.Ios.KeyID = "key-id"
	cfg.Ios.TeamID = "team-id"
	err := InitAPNSClient(context.Background(), cfg)
	assert.NoError(t, err)
	assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)

	cfg.Ios.Production = true
	err = InitAPNSClient(context.Background(), cfg)
	assert.NoError(t, err)
	assert.Equal(t, apns2.HostProduction, ApnsClient.Host)

	// test base64
	cfg.Ios.Production = false
	cfg.Ios.KeyPath = ""
	cfg.Ios.KeyBase64 = authkeyValidP8
	cfg.Ios.KeyType = "p8"
	err = InitAPNSClient(context.Background(), cfg)
	assert.NoError(t, err)
	assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)

	cfg.Ios.Production = true
	err = InitAPNSClient(context.Background(), cfg)
	assert.NoError(t, err)
	assert.Equal(t, apns2.HostProduction, ApnsClient.Host)
}

func TestAPNSClientUseProxy(t *testing.T) {
	cfg, _ := config.LoadConf()

	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = "../certificate/certificate-valid.p12"
	cfg.Core.HTTPProxy = "http://127.0.0.1:8080"
	_ = SetProxy(cfg.Core.HTTPProxy)
	err := InitAPNSClient(context.Background(), cfg)
	assert.Nil(t, err)
	assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)

	req, _ := http.NewRequestWithContext(context.Background(), "GET", apns2.HostDevelopment, nil)
	actualProxyURL, err := ApnsClient.HTTPClient.Transport.(*http.Transport).Proxy(req)
	assert.Nil(t, err)

	expectedProxyURL, _ := url.ParseRequestURI(cfg.Core.HTTPProxy)
	assert.Equal(t, expectedProxyURL, actualProxyURL)

	cfg.Ios.KeyPath = testKeyPathP8
	cfg.Ios.TeamID = "example.team"
	cfg.Ios.KeyID = "example.key"
	err = InitAPNSClient(context.Background(), cfg)
	assert.Nil(t, err)
	assert.Equal(t, apns2.HostDevelopment, ApnsClient.Host)
	assert.NotNil(t, ApnsClient.Token)

	req, _ = http.NewRequestWithContext(context.Background(), "GET", apns2.HostDevelopment, nil)
	actualProxyURL, err = ApnsClient.HTTPClient.Transport.(*http.Transport).Proxy(req)
	assert.Nil(t, err)

	expectedProxyURL, _ = url.ParseRequestURI(cfg.Core.HTTPProxy)
	assert.Equal(t, expectedProxyURL, actualProxyURL)

	http.DefaultTransport.(*http.Transport).Proxy = nil
}

func TestPushToIOS(t *testing.T) {
	cfg, _ := config.LoadConf()
	MaxConcurrentIOSPushes = make(chan struct{}, cfg.Ios.MaxConcurrentPushes)

	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = testKeyPath
	err := InitAPNSClient(context.Background(), cfg)
	assert.Nil(t, err)
	err = status.InitAppStatus(cfg)
	assert.Nil(t, err)

	req := &PushNotification{
		//nolint
		Tokens:   []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7", "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef1"},
		Platform: 1,
		Message:  "Welcome",
	}

	// send fail
	resp, err := PushToIOS(context.Background(), req, cfg)
	assert.Nil(t, err)
	assert.Len(t, resp.Logs, 2)
}

func TestApnsHostFromRequest(t *testing.T) {
	cfg, _ := config.LoadConf()

	cfg.Ios.Enabled = true
	cfg.Ios.KeyPath = testKeyPath
	err := InitAPNSClient(context.Background(), cfg)
	assert.Nil(t, err)
	err = status.InitAppStatus(cfg)
	assert.Nil(t, err)

	req := &PushNotification{
		Production: true,
	}
	client := getApnsClient(cfg, req)
	assert.Equal(t, apns2.HostProduction, client.Host)

	req = &PushNotification{
		Development: true,
	}
	client = getApnsClient(cfg, req)
	assert.Equal(t, apns2.HostDevelopment, client.Host)

	req = &PushNotification{}
	cfg.Ios.Production = true
	client = getApnsClient(cfg, req)
	assert.Equal(t, apns2.HostProduction, client.Host)

	cfg.Ios.Production = false
	client = getApnsClient(cfg, req)
	assert.Equal(t, apns2.HostDevelopment, client.Host)
}
