Skip to content

Commit

Permalink
adds more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Nov 25, 2023
1 parent 5b6dd83 commit b6d37a3
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions recipe/passwordless/passwordless_email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package passwordless

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -629,3 +631,116 @@ func TestSMTPServiceOverrideEmailTemplateForMagicLinkAndOtp(t *testing.T) {
assert.Equal(t, customCalled, false)
assert.Equal(t, sendRawEmailCalled, true)
}

func TestThatMagicLinkUsesRightValueFromOriginFunction(t *testing.T) {
BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()

customCalled := false
plessEmail := ""
var code, urlWithCode *string
var codeLife uint64

sendEmail := func(input emaildelivery.EmailType, userContext supertokens.UserContext) error {
plessEmail = input.PasswordlessLogin.Email
code = input.PasswordlessLogin.UserInputCode
urlWithCode = input.PasswordlessLogin.UrlWithLinkCode
codeLife = input.PasswordlessLogin.CodeLifetime
customCalled = true
return nil
}

tplConfig := plessmodels.TypeInput{
FlowType: "USER_INPUT_CODE_AND_MAGIC_LINK",
EmailDelivery: &emaildelivery.TypeInput{
Service: &emaildelivery.EmailDeliveryInterface{
SendEmail: &sendEmail,
},
},
ContactMethodEmail: plessmodels.ContactMethodEmailConfig{
Enabled: true,
},
}

config := supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: "http://localhost:8080",
},
AppInfo: supertokens.AppInfo{
APIDomain: "api.supertokens.io",
AppName: "SuperTokens",
GetOrigin: func(request *http.Request, userContext supertokens.UserContext) (string, error) {
// read request body
decoder := json.NewDecoder(request.Body)
var requestBody map[string]interface{}
err := decoder.Decode(&requestBody)
if err != nil {
return "https://supertokens.com", nil
}
if requestBody["origin"] == nil {
return "https://supertokens.com", nil
}
return requestBody["origin"].(string), nil
},
},
RecipeList: []supertokens.Recipe{
session.Init(nil),
Init(tplConfig),
},
}

err := supertokens.Init(config)
assert.NoError(t, err)

mux := http.NewServeMux()
testServer := httptest.NewServer(supertokens.Middleware(mux))
defer testServer.Close()

querier, err := supertokens.GetNewQuerierInstanceOrThrowError("")
if err != nil {
t.Error(err.Error())
}
cdiVersion, err := querier.GetQuerierAPIVersion()
if err != nil {
t.Error(err.Error())
}
if unittesting.MaxVersion("2.10", cdiVersion) == "2.10" {
return
}

body := map[string]string{
"email": "[email protected]",
"origin": "localhost:2000",
}

postBody, err := json.Marshal(body)
if err != nil {
t.Error(err.Error())
return
}

resp, err := http.Post(testServer.URL+"/auth/signinup/code", "application/json", bytes.NewBuffer(postBody))
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

bodyBytes, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
body = map[string]string{}

err = json.Unmarshal(bodyBytes, &body)
assert.NoError(t, err)

// Default handler not called
assert.False(t, PasswordlessLoginEmailSentForTest)
assert.Empty(t, PasswordlessLoginEmailDataForTest.Email)
assert.Nil(t, PasswordlessLoginEmailDataForTest.UserInputCode)
assert.Nil(t, PasswordlessLoginEmailDataForTest.UrlWithLinkCode)

// Custom handler called
assert.Equal(t, plessEmail, "[email protected]")
assert.NotNil(t, code)
assert.Equal(t, (*urlWithCode)[:21], "http://localhost:2000")
assert.NotZero(t, codeLife)
assert.True(t, customCalled)
}

0 comments on commit b6d37a3

Please sign in to comment.