Skip to content

Commit b6d37a3

Browse files
committed
adds more tests
1 parent 5b6dd83 commit b6d37a3

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

recipe/passwordless/passwordless_email_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package passwordless
1818

1919
import (
20+
"bytes"
2021
"encoding/json"
2122
"io/ioutil"
2223
"net/http"
24+
"net/http/httptest"
2325
"testing"
2426

2527
"github.com/stretchr/testify/assert"
@@ -629,3 +631,116 @@ func TestSMTPServiceOverrideEmailTemplateForMagicLinkAndOtp(t *testing.T) {
629631
assert.Equal(t, customCalled, false)
630632
assert.Equal(t, sendRawEmailCalled, true)
631633
}
634+
635+
func TestThatMagicLinkUsesRightValueFromOriginFunction(t *testing.T) {
636+
BeforeEach()
637+
unittesting.StartUpST("localhost", "8080")
638+
defer AfterEach()
639+
640+
customCalled := false
641+
plessEmail := ""
642+
var code, urlWithCode *string
643+
var codeLife uint64
644+
645+
sendEmail := func(input emaildelivery.EmailType, userContext supertokens.UserContext) error {
646+
plessEmail = input.PasswordlessLogin.Email
647+
code = input.PasswordlessLogin.UserInputCode
648+
urlWithCode = input.PasswordlessLogin.UrlWithLinkCode
649+
codeLife = input.PasswordlessLogin.CodeLifetime
650+
customCalled = true
651+
return nil
652+
}
653+
654+
tplConfig := plessmodels.TypeInput{
655+
FlowType: "USER_INPUT_CODE_AND_MAGIC_LINK",
656+
EmailDelivery: &emaildelivery.TypeInput{
657+
Service: &emaildelivery.EmailDeliveryInterface{
658+
SendEmail: &sendEmail,
659+
},
660+
},
661+
ContactMethodEmail: plessmodels.ContactMethodEmailConfig{
662+
Enabled: true,
663+
},
664+
}
665+
666+
config := supertokens.TypeInput{
667+
Supertokens: &supertokens.ConnectionInfo{
668+
ConnectionURI: "http://localhost:8080",
669+
},
670+
AppInfo: supertokens.AppInfo{
671+
APIDomain: "api.supertokens.io",
672+
AppName: "SuperTokens",
673+
GetOrigin: func(request *http.Request, userContext supertokens.UserContext) (string, error) {
674+
// read request body
675+
decoder := json.NewDecoder(request.Body)
676+
var requestBody map[string]interface{}
677+
err := decoder.Decode(&requestBody)
678+
if err != nil {
679+
return "https://supertokens.com", nil
680+
}
681+
if requestBody["origin"] == nil {
682+
return "https://supertokens.com", nil
683+
}
684+
return requestBody["origin"].(string), nil
685+
},
686+
},
687+
RecipeList: []supertokens.Recipe{
688+
session.Init(nil),
689+
Init(tplConfig),
690+
},
691+
}
692+
693+
err := supertokens.Init(config)
694+
assert.NoError(t, err)
695+
696+
mux := http.NewServeMux()
697+
testServer := httptest.NewServer(supertokens.Middleware(mux))
698+
defer testServer.Close()
699+
700+
querier, err := supertokens.GetNewQuerierInstanceOrThrowError("")
701+
if err != nil {
702+
t.Error(err.Error())
703+
}
704+
cdiVersion, err := querier.GetQuerierAPIVersion()
705+
if err != nil {
706+
t.Error(err.Error())
707+
}
708+
if unittesting.MaxVersion("2.10", cdiVersion) == "2.10" {
709+
return
710+
}
711+
712+
body := map[string]string{
713+
"email": "[email protected]",
714+
"origin": "localhost:2000",
715+
}
716+
717+
postBody, err := json.Marshal(body)
718+
if err != nil {
719+
t.Error(err.Error())
720+
return
721+
}
722+
723+
resp, err := http.Post(testServer.URL+"/auth/signinup/code", "application/json", bytes.NewBuffer(postBody))
724+
assert.NoError(t, err)
725+
assert.Equal(t, http.StatusOK, resp.StatusCode)
726+
727+
bodyBytes, err := ioutil.ReadAll(resp.Body)
728+
assert.NoError(t, err)
729+
body = map[string]string{}
730+
731+
err = json.Unmarshal(bodyBytes, &body)
732+
assert.NoError(t, err)
733+
734+
// Default handler not called
735+
assert.False(t, PasswordlessLoginEmailSentForTest)
736+
assert.Empty(t, PasswordlessLoginEmailDataForTest.Email)
737+
assert.Nil(t, PasswordlessLoginEmailDataForTest.UserInputCode)
738+
assert.Nil(t, PasswordlessLoginEmailDataForTest.UrlWithLinkCode)
739+
740+
// Custom handler called
741+
assert.Equal(t, plessEmail, "[email protected]")
742+
assert.NotNil(t, code)
743+
assert.Equal(t, (*urlWithCode)[:21], "http://localhost:2000")
744+
assert.NotZero(t, codeLife)
745+
assert.True(t, customCalled)
746+
}

0 commit comments

Comments
 (0)