|
17 | 17 | package passwordless |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bytes" |
20 | 21 | "encoding/json" |
21 | 22 | "io/ioutil" |
22 | 23 | "net/http" |
| 24 | + "net/http/httptest" |
23 | 25 | "testing" |
24 | 26 |
|
25 | 27 | "github.com/stretchr/testify/assert" |
@@ -629,3 +631,116 @@ func TestSMTPServiceOverrideEmailTemplateForMagicLinkAndOtp(t *testing.T) { |
629 | 631 | assert.Equal(t, customCalled, false) |
630 | 632 | assert.Equal(t, sendRawEmailCalled, true) |
631 | 633 | } |
| 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 | + |
| 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