|
| 1 | +package emailpassword |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/supertokens/supertokens-golang/recipe/session" |
| 9 | + "github.com/supertokens/supertokens-golang/recipe/session/sessmodels" |
| 10 | + "github.com/supertokens/supertokens-golang/supertokens" |
| 11 | + "github.com/supertokens/supertokens-golang/test/unittesting" |
| 12 | + "io/ioutil" |
| 13 | + "net/http" |
| 14 | + "net/http/httptest" |
| 15 | + "testing" |
| 16 | +) |
| 17 | + |
| 18 | +func TestShouldMakeSignInUpReturn500WhenUsingProtectedProp(t *testing.T) { |
| 19 | + configValue := supertokens.TypeInput{ |
| 20 | + Supertokens: &supertokens.ConnectionInfo{ |
| 21 | + ConnectionURI: "http://localhost:8080", |
| 22 | + }, |
| 23 | + AppInfo: supertokens.AppInfo{ |
| 24 | + AppName: "SuperTokens", |
| 25 | + WebsiteDomain: "supertokens.io", |
| 26 | + APIDomain: "api.supertokens.io", |
| 27 | + }, |
| 28 | + RecipeList: []supertokens.Recipe{ |
| 29 | + session.Init(&sessmodels.TypeInput{ |
| 30 | + Override: &sessmodels.OverrideStruct{ |
| 31 | + Functions: func(originalImplementation sessmodels.RecipeInterface) sessmodels.RecipeInterface { |
| 32 | + originalCreateNewSession := *originalImplementation.CreateNewSession |
| 33 | + newCreateNewSession := func(userID string, accessTokenPayload map[string]interface{}, sessionDataInDatabase map[string]interface{}, disableAntiCsrf *bool, userContext supertokens.UserContext) (sessmodels.SessionContainer, error) { |
| 34 | + accessTokenPayload["sub"] = "asdf" |
| 35 | + |
| 36 | + return originalCreateNewSession(userID, accessTokenPayload, sessionDataInDatabase, disableAntiCsrf, userContext) |
| 37 | + } |
| 38 | + |
| 39 | + *originalImplementation.CreateNewSession = newCreateNewSession |
| 40 | + |
| 41 | + return originalImplementation |
| 42 | + }, |
| 43 | + }, |
| 44 | + }), |
| 45 | + Init(nil), |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + BeforeEach() |
| 50 | + unittesting.StartUpST("localhost", "8080") |
| 51 | + defer AfterEach() |
| 52 | + err := supertokens.Init(configValue) |
| 53 | + if err != nil { |
| 54 | + t.Error(err.Error()) |
| 55 | + } |
| 56 | + |
| 57 | + testServer := GetTestServer(t) |
| 58 | + defer func() { |
| 59 | + testServer.Close() |
| 60 | + }() |
| 61 | + |
| 62 | + passwordVal := "validPass123" |
| 63 | + |
| 64 | + emailVal := "random@email.com" |
| 65 | + |
| 66 | + formFields := map[string][]map[string]string{ |
| 67 | + "formFields": { |
| 68 | + { |
| 69 | + "id": "email", |
| 70 | + "value": emailVal, |
| 71 | + }, |
| 72 | + { |
| 73 | + "id": "password", |
| 74 | + "value": passwordVal, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + postBody, err := json.Marshal(formFields) |
| 80 | + if err != nil { |
| 81 | + t.Error(err.Error()) |
| 82 | + } |
| 83 | + |
| 84 | + resp, err := http.Post(testServer.URL+"/auth/signup", "application/json", bytes.NewBuffer(postBody)) |
| 85 | + assert.Equal(t, 500, resp.StatusCode) |
| 86 | + cookies := unittesting.ExtractInfoFromResponse(resp) |
| 87 | + assert.True(t, cookies["accessTokenFromAny"] == "") |
| 88 | + assert.True(t, cookies["refreshTokenFromAny"] == "") |
| 89 | + assert.True(t, cookies["frontToken"] == "") |
| 90 | +} |
| 91 | + |
| 92 | +func checkResponse(t *testing.T, res *http.Response, exposed bool) { |
| 93 | + info := unittesting.ExtractInfoFromResponse(res) |
| 94 | + |
| 95 | + if exposed { |
| 96 | + assert.Equal(t, info["sAccessToken"], info["accessTokenFromHeader"]) |
| 97 | + } else { |
| 98 | + assert.Equal(t, info["accessTokenFromHeader"], "") |
| 99 | + assert.NotEqual(t, info["sAccessToken"], "") |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func GetTestServer(t *testing.T) *httptest.Server { |
| 104 | + mux := http.NewServeMux() |
| 105 | + checkDBTrue := true |
| 106 | + |
| 107 | + mux.HandleFunc("/create", func(rw http.ResponseWriter, r *http.Request) { |
| 108 | + dataInBytes, err := ioutil.ReadAll(r.Body) |
| 109 | + if err != nil { |
| 110 | + t.Error(err.Error()) |
| 111 | + } |
| 112 | + var result map[string]interface{} |
| 113 | + err = json.Unmarshal(dataInBytes, &result) |
| 114 | + |
| 115 | + var payload map[string]interface{} |
| 116 | + |
| 117 | + if result["payload"] != nil { |
| 118 | + payload = result["payload"].(map[string]interface{}) |
| 119 | + } |
| 120 | + |
| 121 | + _, err2 := session.CreateNewSession(r, rw, "uniqueId", payload, map[string]interface{}{}) |
| 122 | + |
| 123 | + if err2 != nil { |
| 124 | + http.Error(rw, fmt.Sprint(err2), 400) |
| 125 | + } |
| 126 | + }) |
| 127 | + |
| 128 | + mux.HandleFunc("/verify", verifySession2(true, &checkDBTrue, func(rw http.ResponseWriter, r *http.Request) { |
| 129 | + session := session.GetSessionFromRequestContext(r.Context()) |
| 130 | + rw.Header().Set("Content-Type", "application/json") |
| 131 | + rw.WriteHeader(http.StatusOK) |
| 132 | + json.NewEncoder(rw).Encode(map[string]interface{}{ |
| 133 | + "message": true, |
| 134 | + "sessionHandle": session.GetHandle(), |
| 135 | + "sessionExists": session != nil, |
| 136 | + }) |
| 137 | + })) |
| 138 | + |
| 139 | + mux.HandleFunc("/merge-into-payload", verifySession2(true, nil, func(rw http.ResponseWriter, r *http.Request) { |
| 140 | + session := session.GetSessionFromRequestContext(r.Context()) |
| 141 | + |
| 142 | + dataInBytes, err := ioutil.ReadAll(r.Body) |
| 143 | + if err != nil { |
| 144 | + t.Error(err.Error()) |
| 145 | + } |
| 146 | + var result map[string]interface{} |
| 147 | + err = json.Unmarshal(dataInBytes, &result) |
| 148 | + |
| 149 | + err = session.MergeIntoAccessTokenPayload(result["payload"].(map[string]interface{})) |
| 150 | + assert.NoError(t, err) |
| 151 | + |
| 152 | + rw.Header().Set("Content-Type", "application/json") |
| 153 | + rw.WriteHeader(http.StatusOK) |
| 154 | + json.NewEncoder(rw).Encode(map[string]interface{}{ |
| 155 | + "message": true, |
| 156 | + "sessionHandle": session.GetHandle(), |
| 157 | + "sessionExists": session != nil, |
| 158 | + "newPayload": session.GetAccessTokenPayload(), |
| 159 | + }) |
| 160 | + })) |
| 161 | + |
| 162 | + testServer := httptest.NewServer(supertokens.Middleware(mux)) |
| 163 | + return testServer |
| 164 | +} |
| 165 | + |
| 166 | +func verifySession2(sessionRequired bool, checkDatabase *bool, otherHandler http.HandlerFunc) http.HandlerFunc { |
| 167 | + return session.VerifySession(&sessmodels.VerifySessionOptions{ |
| 168 | + SessionRequired: &sessionRequired, |
| 169 | + CheckDatabase: checkDatabase, |
| 170 | + }, otherHandler) |
| 171 | +} |
0 commit comments