-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add create_user field to otp endpoint (#318)
* fix: add no_signup field to otp endpoint * docs: update readme * refactor: rename no_signup to create_user * test: add otp tests * docs: update readme * fix: unhandled otp error
- Loading branch information
1 parent
cc94302
commit 43d2e39
Showing
4 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/netlify/gotrue/conf" | ||
"github.com/netlify/gotrue/models" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type OtpTestSuite struct { | ||
suite.Suite | ||
API *API | ||
Config *conf.Configuration | ||
|
||
instanceID uuid.UUID | ||
} | ||
|
||
func TestOtp(t *testing.T) { | ||
api, config, instanceID, err := setupAPIForTestForInstance() | ||
require.NoError(t, err) | ||
|
||
ts := &OtpTestSuite{ | ||
API: api, | ||
Config: config, | ||
instanceID: instanceID, | ||
} | ||
defer api.db.Close() | ||
|
||
suite.Run(t, ts) | ||
} | ||
|
||
func (ts *OtpTestSuite) SetupTest() { | ||
models.TruncateAll(ts.API.db) | ||
} | ||
|
||
func (ts *OtpTestSuite) TestOtp() { | ||
cases := []struct { | ||
desc string | ||
params OtpParams | ||
expected struct { | ||
code int | ||
response map[string]interface{} | ||
} | ||
}{ | ||
{ | ||
"Test Success Magiclink Otp", | ||
OtpParams{ | ||
Email: "test@example.com", | ||
CreateUser: true, | ||
}, | ||
struct { | ||
code int | ||
response map[string]interface{} | ||
}{ | ||
http.StatusOK, | ||
make(map[string]interface{}), | ||
}, | ||
}, | ||
{ | ||
"Test Failure Pass Both Email & Phone", | ||
OtpParams{ | ||
Email: "test@example.com", | ||
Phone: "123456789", | ||
CreateUser: true, | ||
}, | ||
struct { | ||
code int | ||
response map[string]interface{} | ||
}{ | ||
http.StatusBadRequest, | ||
map[string]interface{}{ | ||
"code": float64(http.StatusBadRequest), | ||
"msg": "Only an email address or phone number should be provided", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
ts.Run(c.desc, func() { | ||
var buffer bytes.Buffer | ||
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.params)) | ||
|
||
req := httptest.NewRequest(http.MethodPost, "/otp", &buffer) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
w := httptest.NewRecorder() | ||
|
||
ts.API.handler.ServeHTTP(w, req) | ||
|
||
require.Equal(ts.T(), c.expected.code, w.Code) | ||
|
||
data := make(map[string]interface{}) | ||
require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&data)) | ||
|
||
// response should be empty | ||
assert.Equal(ts.T(), data, c.expected.response) | ||
}) | ||
} | ||
} | ||
|
||
func (ts *OtpTestSuite) TestNoSignupsForOtp() { | ||
var buffer bytes.Buffer | ||
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{ | ||
"email": "newuser@example.com", | ||
"create_user": false, | ||
})) | ||
|
||
req := httptest.NewRequest(http.MethodPost, "/otp", &buffer) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
w := httptest.NewRecorder() | ||
|
||
ts.API.handler.ServeHTTP(w, req) | ||
|
||
require.Equal(ts.T(), http.StatusBadRequest, w.Code) | ||
|
||
data := make(map[string]interface{}) | ||
require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&data)) | ||
|
||
// response should be empty | ||
assert.Equal(ts.T(), data, map[string]interface{}{ | ||
"code": float64(http.StatusBadRequest), | ||
"msg": "Signups not allowed for otp", | ||
}) | ||
} |