From 8f65e8e0c7fd0f566df61114dbb7b8158c4e1ff8 Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Tue, 9 Apr 2024 11:10:20 +0530 Subject: [PATCH] removes a bunch of unnecessary tests --- .../emailExistsAndVerificationCheck_test.go | 189 ++++++++----- .../loginmethods_test.go | 2 +- .../authorisationUrlFeature_test.go | 164 ------------ recipe/thirdpartyemailpassword/config_test.go | 142 ---------- .../emailExist_test.go | 222 ---------------- .../emailVerify_test.go | 250 ------------------ 6 files changed, 122 insertions(+), 847 deletions(-) rename recipe/{thirdpartyemailpassword => emailpassword}/loginmethods_test.go (97%) delete mode 100644 recipe/thirdpartyemailpassword/authorisationUrlFeature_test.go delete mode 100644 recipe/thirdpartyemailpassword/config_test.go delete mode 100644 recipe/thirdpartyemailpassword/emailExist_test.go delete mode 100644 recipe/thirdpartyemailpassword/emailVerify_test.go diff --git a/recipe/emailpassword/emailExistsAndVerificationCheck_test.go b/recipe/emailpassword/emailExistsAndVerificationCheck_test.go index aa206463..346f29e1 100644 --- a/recipe/emailpassword/emailExistsAndVerificationCheck_test.go +++ b/recipe/emailpassword/emailExistsAndVerificationCheck_test.go @@ -430,76 +430,66 @@ func TestEmailDoesExistsWithBadInput(t *testing.T) { res, err := http.DefaultClient.Do(req) assert.NoError(t, err) assert.Equal(t, 400, res.StatusCode) + result := *unittesting.HttpResponseToConsumableInformation(res.Body) + assert.Equal(t, "Please provide the email as a GET param", result["message"]) } -//email_verify tests -// func TestGenerateTokenAPIWithValidInputAndEmailNotVerified(t *testing.T) { -// customAntiCsrfVal := "VIA_TOKEN" -// configValue := supertokens.TypeInput{ -// Supertokens: &supertokens.ConnectionInfo{ -// ConnectionURI: "http://localhost:8080", -// }, -// AppInfo: supertokens.AppInfo{ -// APIDomain: "api.supertokens.io", -// AppName: "SuperTokens", -// WebsiteDomain: "supertokens.io", -// }, -// RecipeList: []supertokens.Recipe{ -// Init(nil), -// session.Init(&sessmodels.TypeInput{ -// AntiCsrf: &customAntiCsrfVal, -// }), -// }, -// } - -// BeforeEach() -// unittesting.StartUpST("localhost", "8080") -// defer AfterEach() -// err := supertokens.Init(configValue) -// if err != nil { -// t.Error(err.Error()) -// } -// mux := http.NewServeMux() -// testServer := httptest.NewServer(supertokens.Middleware(mux)) -// defer testServer.Close() - -// resp, err := unittesting.SignupRequest("test@gmail.com", "testPass123", testServer.URL) -// if err != nil { -// t.Error(err.Error()) -// } -// assert.NoError(t, err) -// assert.Equal(t, 200, resp.StatusCode) -// data, _ := io.ReadAll(resp.Body) -// resp.Body.Close() -// var response map[string]interface{} -// _ = json.Unmarshal(data, &response) -// assert.Equal(t, "OK", response["status"]) - -// userId := response["user"].(map[string]interface{})["id"] -// cookieData := unittesting.ExtractInfoFromResponse(resp) - -// verifyToken, err := CreateEmailVerificationToken(userId.(string)) -// if err != nil { -// t.Error(err.Error()) -// } -// VerifyEmailUsingToken(verifyToken.OK.Token) - -// resp1, err := unittesting.EmailVerifyTokenRequest(testServer.URL, userId.(string), cookieData["sAccessToken"], cookieData["antiCsrf"]) - -// if err != nil { -// t.Error(err.Error()) -// } - -// assert.NoError(t, err) -// assert.Equal(t, 200, resp1.StatusCode) -// data1, _ := io.ReadAll(resp1.Body) -// resp1.Body.Close() -// var response1 map[string]interface{} -// _ = json.Unmarshal(data1, &response1) - -// assert.Equal(t, "EMAIL_ALREADY_VERIFIED_ERROR", response1["status"]) - -// } +func TestGenerateTokenAPIWithValidInputAndEmailNotVerified(t *testing.T) { + customCSRFval := "VIA_TOKEN" + configValue := supertokens.TypeInput{ + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: "http://localhost:8080", + }, + AppInfo: supertokens.AppInfo{ + APIDomain: "api.supertokens.io", + AppName: "SuperTokens", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + emailverification.Init(evmodels.TypeInput{ + Mode: evmodels.ModeOptional, + }), + Init(nil), + session.Init(&sessmodels.TypeInput{ + AntiCsrf: &customCSRFval, + GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { + return sessmodels.CookieTransferMethod + }, + }), + }, + } + + BeforeEach() + unittesting.StartUpST("localhost", "8080") + defer AfterEach() + err := supertokens.Init(configValue) + if err != nil { + t.Error(err.Error()) + } + mux := http.NewServeMux() + testServer := httptest.NewServer(supertokens.Middleware(mux)) + defer testServer.Close() + + resp, err := unittesting.SignupRequest("random@gmail.com", "validPass123", testServer.URL) + if err != nil { + t.Error(err.Error()) + } + + assert.Equal(t, http.StatusOK, resp.StatusCode) + + cookieData := unittesting.ExtractInfoFromResponse(resp) + + result := *unittesting.HttpResponseToConsumableInformation(resp.Body) + assert.Equal(t, "OK", result["status"]) + user := result["user"].(map[string]interface{}) + + rep1, err := unittesting.EmailVerifyTokenRequest(testServer.URL, user["id"].(string), cookieData["sAccessToken"], cookieData["antiCsrf"]) + if err != nil { + t.Error(err.Error()) + } + result1 := *unittesting.HttpResponseToConsumableInformation(rep1.Body) + assert.Equal(t, "OK", result1["status"]) +} func TestGenerateTokenAPIWithValidInputNoSessionAndCheckOutput(t *testing.T) { customAntiCsrfVal := "VIA_TOKEN" @@ -558,6 +548,69 @@ func TestGenerateTokenAPIWithValidInputNoSessionAndCheckOutput(t *testing.T) { assert.Equal(t, "unauthorised", response["message"]) } +func TestGenerateTokenAPIwithValidInputEmailVerifiedAndTestError(t *testing.T) { + customCSRFval := "VIA_TOKEN" + configValue := supertokens.TypeInput{ + Supertokens: &supertokens.ConnectionInfo{ + ConnectionURI: "http://localhost:8080", + }, + AppInfo: supertokens.AppInfo{ + APIDomain: "api.supertokens.io", + AppName: "SuperTokens", + WebsiteDomain: "supertokens.io", + }, + RecipeList: []supertokens.Recipe{ + emailverification.Init(evmodels.TypeInput{ + Mode: evmodels.ModeOptional, + }), + Init(nil), + session.Init(&sessmodels.TypeInput{ + AntiCsrf: &customCSRFval, + GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { + return sessmodels.CookieTransferMethod + }, + }), + }, + } + + BeforeEach() + unittesting.StartUpST("localhost", "8080") + defer AfterEach() + err := supertokens.Init(configValue) + if err != nil { + t.Error(err.Error()) + } + mux := http.NewServeMux() + testServer := httptest.NewServer(supertokens.Middleware(mux)) + defer testServer.Close() + + resp, err := unittesting.SignupRequest("random@gmail.com", "validPass123", testServer.URL) + if err != nil { + t.Error(err.Error()) + } + + assert.Equal(t, http.StatusOK, resp.StatusCode) + + cookieData := unittesting.ExtractInfoFromResponse(resp) + + result := *unittesting.HttpResponseToConsumableInformation(resp.Body) + assert.Equal(t, "OK", result["status"]) + user := result["user"].(map[string]interface{}) + + verifyToken, err := emailverification.CreateEmailVerificationToken("public", user["id"].(string), nil) + if err != nil { + t.Error(err.Error()) + } + emailverification.VerifyEmailUsingToken("public", verifyToken.OK.Token) + + rep1, err := unittesting.EmailVerifyTokenRequest(testServer.URL, user["id"].(string), cookieData["sAccessToken"], cookieData["antiCsrf"]) + if err != nil { + t.Error(err.Error()) + } + result1 := *unittesting.HttpResponseToConsumableInformation(rep1.Body) + assert.Equal(t, "EMAIL_ALREADY_VERIFIED_ERROR", result1["status"]) +} + func TestGenerateTokenAPIWithExpiredAccessToken(t *testing.T) { customAntiCsrfVal := "VIA_TOKEN" configValue := supertokens.TypeInput{ diff --git a/recipe/thirdpartyemailpassword/loginmethods_test.go b/recipe/emailpassword/loginmethods_test.go similarity index 97% rename from recipe/thirdpartyemailpassword/loginmethods_test.go rename to recipe/emailpassword/loginmethods_test.go index ba09d6b6..846e5021 100644 --- a/recipe/thirdpartyemailpassword/loginmethods_test.go +++ b/recipe/emailpassword/loginmethods_test.go @@ -1,4 +1,4 @@ -package thirdpartyemailpassword +package emailpassword import ( "encoding/json" diff --git a/recipe/thirdpartyemailpassword/authorisationUrlFeature_test.go b/recipe/thirdpartyemailpassword/authorisationUrlFeature_test.go deleted file mode 100644 index 84487fd0..00000000 --- a/recipe/thirdpartyemailpassword/authorisationUrlFeature_test.go +++ /dev/null @@ -1,164 +0,0 @@ -/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. - * - * This software is licensed under the Apache License, Version 2.0 (the - * "License") as published by the Apache Software Foundation. - * - * You may not use this file except in compliance with the License. You may - * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -package thirdpartyemailpassword - -import ( - "encoding/json" - "io" - "net/http" - "net/http/httptest" - "net/url" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/supertokens/supertokens-golang/recipe/session" - "github.com/supertokens/supertokens-golang/recipe/session/sessmodels" - "github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels" - "github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword/tpepmodels" - "github.com/supertokens/supertokens-golang/supertokens" - "github.com/supertokens/supertokens-golang/test/unittesting" -) - -func TestMinimumConfigForThirdPartyModule(t *testing.T) { - customAntiCsrfVal := "VIA_TOKEN" - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - Init( - &tpepmodels.TypeInput{ - Providers: []tpmodels.ProviderInput{ - customProvider1, - }, - }, - ), - session.Init(&sessmodels.TypeInput{ - AntiCsrf: &customAntiCsrfVal, - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - - if err != nil { - t.Error(err.Error()) - } - - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - resp, err := http.Get(testServer.URL + "/auth/authorisationurl?thirdPartyId=custom") - if err != nil { - t.Error(err.Error()) - } - - dataInBytes, err := io.ReadAll(resp.Body) - if err != nil { - t.Error(err.Error()) - } - resp.Body.Close() - - var data map[string]interface{} - err = json.Unmarshal(dataInBytes, &data) - if err != nil { - t.Error(err.Error()) - } - - assert.Equal(t, "OK", data["status"]) - - fetchedUrl, err := url.Parse(data["urlWithQueryParams"].(string)) - if err != nil { - t.Error(err.Error()) - } - assert.Equal(t, "test.com", fetchedUrl.Host) - assert.Equal(t, "/oauth/auth", fetchedUrl.Path) -} - -func TestThirdPartyProviderDoesnotExsit(t *testing.T) { - customAntiCsrfVal := "VIA_TOKEN" - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - Init( - &tpepmodels.TypeInput{ - Providers: []tpmodels.ProviderInput{ - customProvider1, - }, - }, - ), - session.Init(&sessmodels.TypeInput{ - AntiCsrf: &customAntiCsrfVal, - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - - if err != nil { - t.Error(err.Error()) - } - - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - resp, err := http.Get(testServer.URL + "/auth/authorisationurl?thirdPartyId=google") - if err != nil { - t.Error(err.Error()) - } - - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) - - dataInBytes, err := io.ReadAll(resp.Body) - if err != nil { - t.Error(err.Error()) - } - resp.Body.Close() - - var data map[string]interface{} - err = json.Unmarshal(dataInBytes, &data) - if err != nil { - t.Error(err.Error()) - } - - assert.Equal(t, "the provider google could not be found in the configuration", data["message"].(string)) -} diff --git a/recipe/thirdpartyemailpassword/config_test.go b/recipe/thirdpartyemailpassword/config_test.go deleted file mode 100644 index 43d5e844..00000000 --- a/recipe/thirdpartyemailpassword/config_test.go +++ /dev/null @@ -1,142 +0,0 @@ -/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. - * - * This software is licensed under the Apache License, Version 2.0 (the - * "License") as published by the Apache Software Foundation. - * - * You may not use this file except in compliance with the License. You may - * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -package thirdpartyemailpassword - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels" - "github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword/tpepmodels" - "github.com/supertokens/supertokens-golang/supertokens" - "github.com/supertokens/supertokens-golang/test/unittesting" -) - -func TestDefaultConfigForThirdPartyEmailPasswordRecipe(t *testing.T) { - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - Init( - &tpepmodels.TypeInput{ - Providers: []tpmodels.ProviderInput{}, - }, - ), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - - if err != nil { - t.Error(err.Error()) - } - - thirdpartyemailpassword, err := GetRecipeInstanceOrThrowError() - if err != nil { - t.Error(err.Error()) - } - - assert.NotNil(t, thirdpartyemailpassword.thirdPartyRecipe) // thirdPartyRecipe must be created always - - emailpassword := thirdpartyemailpassword.emailPasswordRecipe - signUpFeature := emailpassword.Config.SignUpFeature - assert.Equal(t, 2, len(signUpFeature.FormFields)) - for _, formField := range signUpFeature.FormFields { - assert.False(t, formField.Optional) - assert.NotNil(t, formField.Validate) - } - - signInFeature := emailpassword.Config.SignInFeature - assert.Equal(t, 2, len(signInFeature.FormFields)) - for _, formField := range signInFeature.FormFields { - assert.False(t, formField.Optional) - assert.NotNil(t, formField.Validate) - } - - resetPasswordUsingTokenFeature := emailpassword.Config.ResetPasswordUsingTokenFeature - assert.Equal(t, 1, len(resetPasswordUsingTokenFeature.FormFieldsForGenerateTokenForm)) - assert.Equal(t, "email", resetPasswordUsingTokenFeature.FormFieldsForGenerateTokenForm[0].ID) - assert.Equal(t, 1, len(resetPasswordUsingTokenFeature.FormFieldsForPasswordResetForm)) - assert.Equal(t, "password", resetPasswordUsingTokenFeature.FormFieldsForPasswordResetForm[0].ID) -} - -func TestDefaultConfigForThirdPartyEmailPasswordRecipeWithProvider(t *testing.T) { - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - Init( - &tpepmodels.TypeInput{ - Providers: []tpmodels.ProviderInput{ - customProvider2, - }, - }, - ), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - - if err != nil { - t.Error(err.Error()) - } - - thirdpartyemailpassword, err := GetRecipeInstanceOrThrowError() - if err != nil { - t.Error(err.Error()) - } - - assert.NotNil(t, thirdpartyemailpassword.thirdPartyRecipe) - - emailpassword := thirdpartyemailpassword.emailPasswordRecipe - signUpFeature := emailpassword.Config.SignUpFeature - assert.Equal(t, 2, len(signUpFeature.FormFields)) - for _, formField := range signUpFeature.FormFields { - assert.False(t, formField.Optional) - assert.NotNil(t, formField.Validate) - } - - signInFeature := emailpassword.Config.SignInFeature - assert.Equal(t, 2, len(signInFeature.FormFields)) - for _, formField := range signInFeature.FormFields { - assert.False(t, formField.Optional) - assert.NotNil(t, formField.Validate) - } - - resetPasswordUsingTokenFeature := emailpassword.Config.ResetPasswordUsingTokenFeature - assert.Equal(t, 1, len(resetPasswordUsingTokenFeature.FormFieldsForGenerateTokenForm)) - assert.Equal(t, "email", resetPasswordUsingTokenFeature.FormFieldsForGenerateTokenForm[0].ID) - assert.Equal(t, 1, len(resetPasswordUsingTokenFeature.FormFieldsForPasswordResetForm)) - assert.Equal(t, "password", resetPasswordUsingTokenFeature.FormFieldsForPasswordResetForm[0].ID) -} diff --git a/recipe/thirdpartyemailpassword/emailExist_test.go b/recipe/thirdpartyemailpassword/emailExist_test.go deleted file mode 100644 index 2e42a3ef..00000000 --- a/recipe/thirdpartyemailpassword/emailExist_test.go +++ /dev/null @@ -1,222 +0,0 @@ -/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. - * - * This software is licensed under the Apache License, Version 2.0 (the - * "License") as published by the Apache Software Foundation. - * - * You may not use this file except in compliance with the License. You may - * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -package thirdpartyemailpassword - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/supertokens/supertokens-golang/recipe/session" - "github.com/supertokens/supertokens-golang/recipe/session/sessmodels" - "github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword/tpepmodels" - "github.com/supertokens/supertokens-golang/supertokens" - "github.com/supertokens/supertokens-golang/test/unittesting" -) - -func TestIfDisableAPIdefaultEmailExistDoesNotWork(t *testing.T) { - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - Init(&tpepmodels.TypeInput{ - Override: &tpepmodels.OverrideStruct{ - APIs: func(originalImplementation tpepmodels.APIInterface) tpepmodels.APIInterface { - *originalImplementation.EmailPasswordEmailExistsGET = nil - return originalImplementation - }, - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - if err != nil { - t.Error(err.Error()) - } - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/signup/email/exists", nil) - q := req.URL.Query() - q.Add("email", "random@gmail.com") - req.URL.RawQuery = q.Encode() - assert.NoError(t, err) - res, err := http.DefaultClient.Do(req) - assert.NoError(t, err) - assert.Equal(t, http.StatusNotFound, res.StatusCode) -} - -func TestGoodInputEmailExist(t *testing.T) { - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - Init(nil), - session.Init(&sessmodels.TypeInput{ - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - if err != nil { - t.Error(err.Error()) - } - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - resp, err := unittesting.SignupRequest("random@gmail.com", "validPass123", testServer.URL) - if err != nil { - t.Error(err.Error()) - } - - assert.Equal(t, http.StatusOK, resp.StatusCode) - - result := *unittesting.HttpResponseToConsumableInformation(resp.Body) - assert.Equal(t, "OK", result["status"]) - - req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/signup/email/exists", nil) - q := req.URL.Query() - q.Add("email", "random@gmail.com") - req.URL.RawQuery = q.Encode() - if err != nil { - t.Error(err.Error()) - } - res, err := http.DefaultClient.Do(req) - if err != nil { - t.Error(err.Error()) - } - result1 := *unittesting.HttpResponseToConsumableInformation(res.Body) - assert.Equal(t, http.StatusOK, res.StatusCode) - assert.Equal(t, "OK", result1["status"]) - assert.Equal(t, true, result1["exists"]) -} - -func TestGoodInputEmailDoesNotExist(t *testing.T) { - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - Init(nil), - session.Init(&sessmodels.TypeInput{ - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - if err != nil { - t.Error(err.Error()) - } - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/signup/email/exists", nil) - q := req.URL.Query() - q.Add("email", "random@gmail.com") - req.URL.RawQuery = q.Encode() - if err != nil { - t.Error(err.Error()) - } - res, err := http.DefaultClient.Do(req) - if err != nil { - t.Error(err.Error()) - } - result := *unittesting.HttpResponseToConsumableInformation(res.Body) - assert.Equal(t, http.StatusOK, res.StatusCode) - assert.Equal(t, "OK", result["status"]) - assert.Equal(t, false, result["exists"]) -} - -func TestBadInputDoNotPassEmail(t *testing.T) { - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - Init(nil), - session.Init(&sessmodels.TypeInput{ - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - if err != nil { - t.Error(err.Error()) - } - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/signup/email/exists", nil) - if err != nil { - t.Error(err.Error()) - } - res, err := http.DefaultClient.Do(req) - if err != nil { - t.Error(err.Error()) - } - result := *unittesting.HttpResponseToConsumableInformation(res.Body) - assert.Equal(t, http.StatusBadRequest, res.StatusCode) - assert.Equal(t, "Please provide the email as a GET param", result["message"]) -} diff --git a/recipe/thirdpartyemailpassword/emailVerify_test.go b/recipe/thirdpartyemailpassword/emailVerify_test.go deleted file mode 100644 index d2c5cc7d..00000000 --- a/recipe/thirdpartyemailpassword/emailVerify_test.go +++ /dev/null @@ -1,250 +0,0 @@ -/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. - * - * This software is licensed under the Apache License, Version 2.0 (the - * "License") as published by the Apache Software Foundation. - * - * You may not use this file except in compliance with the License. You may - * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -package thirdpartyemailpassword - -import ( - "bytes" - "encoding/json" - "net/http" - "net/http/httptest" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/supertokens/supertokens-golang/recipe/emailverification" - "github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels" - "github.com/supertokens/supertokens-golang/recipe/session" - "github.com/supertokens/supertokens-golang/recipe/session/sessmodels" - "github.com/supertokens/supertokens-golang/supertokens" - "github.com/supertokens/supertokens-golang/test/unittesting" -) - -func TestTheGenerateTokenAPIwithValidInputEmailNotVerified(t *testing.T) { - customCSRFval := "VIA_TOKEN" - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - emailverification.Init(evmodels.TypeInput{ - Mode: evmodels.ModeOptional, - }), - Init(nil), - session.Init(&sessmodels.TypeInput{ - AntiCsrf: &customCSRFval, - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - if err != nil { - t.Error(err.Error()) - } - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - resp, err := unittesting.SignupRequest("random@gmail.com", "validPass123", testServer.URL) - if err != nil { - t.Error(err.Error()) - } - - assert.Equal(t, http.StatusOK, resp.StatusCode) - - cookieData := unittesting.ExtractInfoFromResponse(resp) - - result := *unittesting.HttpResponseToConsumableInformation(resp.Body) - assert.Equal(t, "OK", result["status"]) - user := result["user"].(map[string]interface{}) - - rep1, err := unittesting.EmailVerifyTokenRequest(testServer.URL, user["id"].(string), cookieData["sAccessToken"], cookieData["antiCsrf"]) - if err != nil { - t.Error(err.Error()) - } - result1 := *unittesting.HttpResponseToConsumableInformation(rep1.Body) - assert.Equal(t, "OK", result1["status"]) -} - -func TestGenerateTokenAPIwithValidInputEmailVerifiedAndTestError(t *testing.T) { - customCSRFval := "VIA_TOKEN" - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - emailverification.Init(evmodels.TypeInput{ - Mode: evmodels.ModeOptional, - }), - Init(nil), - session.Init(&sessmodels.TypeInput{ - AntiCsrf: &customCSRFval, - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - if err != nil { - t.Error(err.Error()) - } - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - resp, err := unittesting.SignupRequest("random@gmail.com", "validPass123", testServer.URL) - if err != nil { - t.Error(err.Error()) - } - - assert.Equal(t, http.StatusOK, resp.StatusCode) - - cookieData := unittesting.ExtractInfoFromResponse(resp) - - result := *unittesting.HttpResponseToConsumableInformation(resp.Body) - assert.Equal(t, "OK", result["status"]) - user := result["user"].(map[string]interface{}) - - verifyToken, err := emailverification.CreateEmailVerificationToken("public", user["id"].(string), nil) - if err != nil { - t.Error(err.Error()) - } - emailverification.VerifyEmailUsingToken("public", verifyToken.OK.Token) - - rep1, err := unittesting.EmailVerifyTokenRequest(testServer.URL, user["id"].(string), cookieData["sAccessToken"], cookieData["antiCsrf"]) - if err != nil { - t.Error(err.Error()) - } - result1 := *unittesting.HttpResponseToConsumableInformation(rep1.Body) - assert.Equal(t, "EMAIL_ALREADY_VERIFIED_ERROR", result1["status"]) -} - -func TestGenerateTokenAPIWithValidInputNoSessionAndCheckOutput(t *testing.T) { - customCSRFval := "VIA_TOKEN" - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - emailverification.Init(evmodels.TypeInput{ - Mode: evmodels.ModeOptional, - }), - Init(nil), - session.Init(&sessmodels.TypeInput{ - AntiCsrf: &customCSRFval, - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - if err != nil { - t.Error(err.Error()) - } - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - - resp, err := http.Post(testServer.URL+"/auth/user/email/verify/token", "application/json", nil) - if err != nil { - t.Error(err.Error()) - } - assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) - result := *unittesting.HttpResponseToConsumableInformation(resp.Body) - assert.Equal(t, "unauthorised", result["message"]) -} - -func TestEmailVerifyAPIwithInvalidTokenCheckError(t *testing.T) { - customCSRFval := "VIA_TOKEN" - configValue := supertokens.TypeInput{ - Supertokens: &supertokens.ConnectionInfo{ - ConnectionURI: "http://localhost:8080", - }, - AppInfo: supertokens.AppInfo{ - APIDomain: "api.supertokens.io", - AppName: "SuperTokens", - WebsiteDomain: "supertokens.io", - }, - RecipeList: []supertokens.Recipe{ - emailverification.Init(evmodels.TypeInput{ - Mode: evmodels.ModeOptional, - }), - Init(nil), - session.Init(&sessmodels.TypeInput{ - AntiCsrf: &customCSRFval, - GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { - return sessmodels.CookieTransferMethod - }, - }), - }, - } - - BeforeEach() - unittesting.StartUpST("localhost", "8080") - defer AfterEach() - err := supertokens.Init(configValue) - if err != nil { - t.Error(err.Error()) - } - mux := http.NewServeMux() - testServer := httptest.NewServer(supertokens.Middleware(mux)) - defer testServer.Close() - formFields := map[string]string{ - "method": "token", - "token": "randomToken", - } - - postBody, err := json.Marshal(formFields) - if err != nil { - t.Error(err.Error()) - } - - resp, err := http.Post(testServer.URL+"/auth/user/email/verify", "application/json", bytes.NewBuffer(postBody)) - if err != nil { - t.Error(err.Error()) - } - result := *unittesting.HttpResponseToConsumableInformation(resp.Body) - assert.Equal(t, "EMAIL_VERIFICATION_INVALID_TOKEN_ERROR", result["status"]) -}