package main

import (
	"github.com/matryer/is"
	"testing"
)

const (
	EmailServiceProviderYahoo   = "yahoo"
	EmailServiceProviderGmail   = "gmail"
	EmailServiceProviderOutlook = "outlook"
	EmailServiceProviderYandex  = "yandex"
	EmailServiceProviderGmx     = "gmx"
	EmailServiceProviderNaver   = "naver"
	EmailServiceProviderZimbra  = "zimbra"
	EmailServiceProvider163     = "163"
	EmailServiceProviderSina    = "sina"
	EmailServiceProviderFalse   = ""
)

func TestEmailServiceProviderYahoo(t *testing.T) {
	is := is.New(t)
	ref := `https://mail.yahoo.com/`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderYahoo)
}

func TestEmailServiceProviderGmail(t *testing.T) {
	is := is.New(t)
	ref := `https://www.google.com/url?q=https://help.xsolla.com&source=gmail&ust=1510761510002000&usg=AFQjCNEygl--g8EbuEXnab1Jx8pc-CO0LA&sa=D`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderGmail)
}

func TestEmailServiceProviderGmail2(t *testing.T) {
	is := is.New(t)
	ref := `https://mail.google.com/mail/u/0/?tab=wm`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderGmail)
}

func TestEmailServiceProviderGmailFalse(t *testing.T) {
	is := is.New(t)
	ref := `https://www.google.com`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderFalse)
}

func TestEmailServiceProviderOutlook(t *testing.T) {
	is := is.New(t)
	ref := `https://outlook.live.com/`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderOutlook)
}

func TestEmailServiceProviderYandex(t *testing.T) {
	is := is.New(t)
	ref := `https://mail.yandex.ru/lite/message/163818436445604909`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderYandex)
}

func TestEmailServiceProviderGmx(t *testing.T) {
	is := is.New(t)
	ref := `https://deref-gmx.net/mail/client/uc8bzK_3f4Q/dereferrer/?redirectUrl=https%3A%2F%2Fhelp.xsolla.com`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderGmx)
}

func TestEmailServiceProviderGmxFalse(t *testing.T) {
	is := is.New(t)
	ref := `https://deref-gmx.net/search`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderFalse)
}

func TestEmailServiceProviderNaver(t *testing.T) {
	is := is.New(t)
	ref := `https://m.mail.naver.com/m/list/`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderNaver)
}

func TestEmailServiceProviderZimbra(t *testing.T) {
	is := is.New(t)
	ref := `https://web.mail.comcast.net/zimbra/m/zmain?app=mail&app=mail`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderZimbra)
}

func TestEmailServiceProvider163(t *testing.T) {
	is := is.New(t)
	ref := `https://mail.163.com/js6/read/readhtml.jsp?mid=45:1tbiLR+e8VSIUnvBIgAAsL&font=15&color=34783B`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProvider163)
}

func TestEmailServiceProviderSina(t *testing.T) {
	is := is.New(t)
	ref := `http://m1.mail.sina.com.cn/classic/index.php`

	hit := map[string]string{}
	hit["Referer"] = ref
	hit["URL"] = "https://xsolla.com"

	Referrer := parseUrlParams(ref)

	emailServiceProvider(hit, Referrer)

	val, ok := hit["EmailServiceProvider"]
	is.True(ok)
	is.Equal(val, EmailServiceProviderSina)
}
