package sso

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestDecodeSuccessfulSalesforceResponse(t *testing.T) {
	samlResponse := `[encoded data, 11716 characters]`
	auth, err := DecodeAuthResponse(samlResponse)
	require.Nil(t, err)
	require.NotNil(t, auth)
	info, ok := auth.(*resp)
	require.True(t, ok)
	status, err := info.status()
	assert.Nil(t, err)
	assert.Equal(t, Success, status)
	assert.Equal(t, "john@kolide.co", auth.UserID())
}

func TestDecodeWithCommentInName(t *testing.T) {
	// Testing for vuln described at
	// https://duo.com/blog/duo-finds-saml-vulnerabilities-affecting-multiple-implementations
	// Relevant XML snippets:
	// <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
	// <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">john@kol<!---->ide.co</saml:NameID>
	samlResponse := `[encoded data, 11836 characters]`
	auth, err := DecodeAuthResponse(samlResponse)
	require.Nil(t, err)
	require.NotNil(t, auth)
	info, ok := auth.(*resp)
	require.True(t, ok)
	status, err := info.status()
	assert.Nil(t, err)
	assert.Equal(t, Success, status)
	assert.Equal(t, "john@kol<!---->ide.co", auth.UserID())
}

func TestDecodeSuccessfulGoogleResponse(t *testing.T) {
	samlResponse :=
		`[encoded data, 5996 characters]`
	auth, err := DecodeAuthResponse(samlResponse)
	require.Nil(t, err)
	require.NotNil(t, auth)
	info, ok := auth.(*resp)
	require.True(t, ok)
	status, err := info.status()
	assert.Nil(t, err)
	assert.Equal(t, Success, status)
	assert.Equal(t, "john@edilok.net", auth.UserID())

}
