satellite/admin: add user update endpoint (#3896)

This commit is contained in:
Stefan Benten 2020-05-20 11:20:04 +02:00 committed by GitHub
parent 8ec64f3daf
commit 1a4ab3df48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 0 deletions

View File

@ -30,6 +30,20 @@ A successful response:
} }
``` ```
## PUT /api/user/{user-email}
Updates the details of existing user found by its email.
A successful update request:
```json
{
"email": "alice+2@mail.test",
"shortName": "Al",
"passwordHash": "1234abcd"
}
```
## GET /api/user/{user-email} ## GET /api/user/{user-email}
This endpoint returns information about user and their projects. This endpoint returns information about user and their projects.

View File

@ -68,6 +68,7 @@ func NewServer(log *zap.Logger, listener net.Listener, db DB, config Config) *Se
// When adding new options, also update README.md // When adding new options, also update README.md
server.mux.HandleFunc("/api/user", server.addUser).Methods("POST") server.mux.HandleFunc("/api/user", server.addUser).Methods("POST")
server.mux.HandleFunc("/api/user/{useremail}", server.updateUser).Methods("PUT")
server.mux.HandleFunc("/api/user/{useremail}", server.userInfo).Methods("GET") server.mux.HandleFunc("/api/user/{useremail}", server.userInfo).Methods("GET")
server.mux.HandleFunc("/api/coupon", server.addCoupon).Methods("POST") server.mux.HandleFunc("/api/coupon", server.addCoupon).Methods("POST")
server.mux.HandleFunc("/api/coupon/{couponid}", server.couponInfo).Methods("GET") server.mux.HandleFunc("/api/coupon/{couponid}", server.couponInfo).Methods("GET")

View File

@ -174,3 +174,60 @@ func (server *Server) userInfo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared _, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
} }
func (server *Server) updateUser(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
userEmail, ok := vars["useremail"]
if !ok {
http.Error(w, "user-email missing", http.StatusBadRequest)
return
}
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, fmt.Sprintf("user with email %q not found", userEmail), http.StatusNotFound)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("failed to get user %q: %v", userEmail, err), http.StatusInternalServerError)
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("failed to read body: %v", err), http.StatusInternalServerError)
return
}
var input console.User
err = json.Unmarshal(body, &input)
if err != nil {
http.Error(w, fmt.Sprintf("failed to unmarshal request: %v", err), http.StatusBadRequest)
return
}
if input.FullName != "" {
user.FullName = input.FullName
}
if input.ShortName != "" {
user.ShortName = input.ShortName
}
if input.Email != "" {
user.Email = input.Email
}
if !input.PartnerID.IsZero() {
user.PartnerID = input.PartnerID
}
if len(input.PasswordHash) > 0 {
user.PasswordHash = input.PasswordHash
}
err = server.db.Console().Users().Update(ctx, user)
if err != nil {
http.Error(w, fmt.Sprintf("failed to update user: %v", err), http.StatusInternalServerError)
return
}
}

View File

@ -96,3 +96,42 @@ func TestAddUser(t *testing.T) {
require.Equal(t, email, user.Email) require.Equal(t, email, user.Email)
}) })
} }
func TestUpdateUser(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1,
StorageNodeCount: 0,
UplinkCount: 1,
Reconfigure: testplanet.Reconfigure{
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
config.Admin.Address = "127.0.0.1:0"
},
},
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
user, err := planet.Satellites[0].DB.Console().Users().GetByEmail(ctx, planet.Uplinks[0].Projects[0].Owner.Email)
require.NoError(t, err)
body := strings.NewReader(`{"email":"alice+2@mail.test", "shortName":"Newbie"}`)
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("http://"+address.String()+"/api/user/%s", user.Email), body)
require.NoError(t, err)
req.Header.Set("Authorization", "very-secret-token")
response, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode)
responseBody, err := ioutil.ReadAll(response.Body)
require.NoError(t, err)
require.NoError(t, response.Body.Close())
require.Len(t, responseBody, 0)
updatedUser, err := planet.Satellites[0].DB.Console().Users().Get(ctx, user.ID)
require.NoError(t, err)
require.Equal(t, "alice+2@mail.test", updatedUser.Email)
require.Equal(t, user.FullName, updatedUser.FullName)
require.NotEqual(t, "Newbie", user.ShortName)
require.Equal(t, "Newbie", updatedUser.ShortName)
require.Equal(t, user.ID, updatedUser.ID)
require.Equal(t, user.Status, updatedUser.Status)
})
}