-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: admin user update should update is_anonymous field (#1623)
## What kind of change does this PR introduce? * Fixes #1578
- Loading branch information
1 parent
3cd00ee
commit f5c6fcd
Showing
2 changed files
with
107 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import ( | |
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gofrs/uuid" | ||
jwt "github.com/golang-jwt/jwt" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
|
@@ -222,3 +224,85 @@ func (ts *AnonymousTestSuite) TestRateLimitAnonymousSignups() { | |
ts.API.handler.ServeHTTP(w, req) | ||
assert.Equal(ts.T(), http.StatusBadRequest, w.Code) | ||
} | ||
|
||
func (ts *AnonymousTestSuite) TestAdminUpdateAnonymousUser() { | ||
claims := &AccessTokenClaims{ | ||
Role: "supabase_admin", | ||
} | ||
adminJwt, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(ts.Config.JWT.Secret)) | ||
require.NoError(ts.T(), err) | ||
|
||
u1, err := models.NewUser("", "", "", ts.Config.JWT.Aud, nil) | ||
require.NoError(ts.T(), err) | ||
u1.IsAnonymous = true | ||
require.NoError(ts.T(), ts.API.db.Create(u1)) | ||
|
||
u2, err := models.NewUser("", "", "", ts.Config.JWT.Aud, nil) | ||
require.NoError(ts.T(), err) | ||
u2.IsAnonymous = true | ||
require.NoError(ts.T(), ts.API.db.Create(u2)) | ||
|
||
cases := []struct { | ||
desc string | ||
userId uuid.UUID | ||
body map[string]interface{} | ||
expected map[string]interface{} | ||
expectedIdentities int | ||
}{ | ||
{ | ||
desc: "update anonymous user with email and email confirm true", | ||
userId: u1.ID, | ||
body: map[string]interface{}{ | ||
"email": "[email protected]", | ||
"email_confirm": true, | ||
}, | ||
expected: map[string]interface{}{ | ||
"email": "[email protected]", | ||
"is_anonymous": false, | ||
}, | ||
expectedIdentities: 1, | ||
}, | ||
{ | ||
desc: "update anonymous user with email and email confirm false", | ||
userId: u2.ID, | ||
body: map[string]interface{}{ | ||
"email": "[email protected]", | ||
"email_confirm": false, | ||
}, | ||
expected: map[string]interface{}{ | ||
"email": "[email protected]", | ||
"is_anonymous": true, | ||
}, | ||
expectedIdentities: 1, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
ts.Run(c.desc, func() { | ||
// Request body | ||
var buffer bytes.Buffer | ||
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.body)) | ||
|
||
req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/admin/users/%s", c.userId), &buffer) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", adminJwt)) | ||
|
||
w := httptest.NewRecorder() | ||
ts.API.handler.ServeHTTP(w, req) | ||
require.Equal(ts.T(), http.StatusOK, w.Code) | ||
|
||
var data models.User | ||
require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&data)) | ||
|
||
require.NotNil(ts.T(), data) | ||
require.Len(ts.T(), data.Identities, c.expectedIdentities) | ||
|
||
actual := map[string]interface{}{ | ||
"email": data.GetEmail(), | ||
"is_anonymous": data.IsAnonymous, | ||
} | ||
|
||
require.Equal(ts.T(), c.expected, actual) | ||
}) | ||
} | ||
} |