From fd9eaa3e8370e9f9f62fb256aa6cd36bf6c1b86f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Mon, 21 Dec 2020 22:05:47 -0800 Subject: [PATCH] Make user fields editable via API --- api/payload.go | 70 +++++++++++++++++++++++++++++++++++----------- api/user.go | 10 ++++++- client/client.go | 6 +++- client/core.go | 33 ++++++++++++++++------ go.mod | 1 + go.sum | 13 +++++++++ tests/user_test.go | 33 ++++++++++++++++++++++ 7 files changed, 139 insertions(+), 27 deletions(-) diff --git a/api/payload.go b/api/payload.go index bd1e50a7..baf66e3d 100644 --- a/api/payload.go +++ b/api/payload.go @@ -165,27 +165,41 @@ func decodeFeedModificationRequest(r io.ReadCloser) (*feedModificationRequest, e return &feed, nil } -func decodeUserCreationRequest(r io.ReadCloser) (*model.User, error) { +type userCreationRequest struct { + Username string `json:"username"` + Password string `json:"password"` + IsAdmin bool `json:"is_admin"` + GoogleID string `json:"google_id"` + OpenIDConnectID string `json:"openid_connect_id"` +} + +func decodeUserCreationRequest(r io.ReadCloser) (*userCreationRequest, error) { defer r.Close() - var user model.User + var request userCreationRequest decoder := json.NewDecoder(r) - if err := decoder.Decode(&user); err != nil { - return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err) + if err := decoder.Decode(&request); err != nil { + return nil, fmt.Errorf("Unable to decode user creation JSON object: %v", err) } - return &user, nil + return &request, nil } type userModificationRequest struct { - Username *string `json:"username"` - Password *string `json:"password"` - IsAdmin *bool `json:"is_admin"` - Theme *string `json:"theme"` - Language *string `json:"language"` - Timezone *string `json:"timezone"` - EntryDirection *string `json:"entry_sorting_direction"` - EntriesPerPage *int `json:"entries_per_page"` + Username *string `json:"username"` + Password *string `json:"password"` + IsAdmin *bool `json:"is_admin"` + Theme *string `json:"theme"` + Language *string `json:"language"` + Timezone *string `json:"timezone"` + EntryDirection *string `json:"entry_sorting_direction"` + Stylesheet *string `json:"stylesheet"` + GoogleID *string `json:"google_id"` + OpenIDConnectID *string `json:"openid_connect_id"` + EntriesPerPage *int `json:"entries_per_page"` + KeyboardShortcuts *bool `json:"keyboard_shortcuts"` + ShowReadingTime *bool `json:"show_reading_time"` + EntrySwipe *bool `json:"entry_swipe"` } func (u *userModificationRequest) Update(user *model.User) { @@ -217,21 +231,45 @@ func (u *userModificationRequest) Update(user *model.User) { user.EntryDirection = *u.EntryDirection } + if u.Stylesheet != nil { + user.Stylesheet = *u.Stylesheet + } + + if u.GoogleID != nil { + user.GoogleID = *u.GoogleID + } + + if u.OpenIDConnectID != nil { + user.OpenIDConnectID = *u.OpenIDConnectID + } + if u.EntriesPerPage != nil { user.EntriesPerPage = *u.EntriesPerPage } + + if u.KeyboardShortcuts != nil { + user.KeyboardShortcuts = *u.KeyboardShortcuts + } + + if u.ShowReadingTime != nil { + user.ShowReadingTime = *u.ShowReadingTime + } + + if u.EntrySwipe != nil { + user.EntrySwipe = *u.EntrySwipe + } } func decodeUserModificationRequest(r io.ReadCloser) (*userModificationRequest, error) { defer r.Close() - var user userModificationRequest + var request userModificationRequest decoder := json.NewDecoder(r) - if err := decoder.Decode(&user); err != nil { + if err := decoder.Decode(&request); err != nil { return nil, fmt.Errorf("Unable to decode user modification JSON object: %v", err) } - return &user, nil + return &request, nil } func decodeEntryStatusRequest(r io.ReadCloser) ([]int64, string, error) { diff --git a/api/user.go b/api/user.go index f81d4b3f..98c90b48 100644 --- a/api/user.go +++ b/api/user.go @@ -10,6 +10,7 @@ import ( "miniflux.app/http/request" "miniflux.app/http/response/json" + "miniflux.app/model" ) func (h *handler) currentUser(w http.ResponseWriter, r *http.Request) { @@ -28,12 +29,19 @@ func (h *handler) createUser(w http.ResponseWriter, r *http.Request) { return } - user, err := decodeUserCreationRequest(r.Body) + userCreationRequest, err := decodeUserCreationRequest(r.Body) if err != nil { json.BadRequest(w, r, err) return } + user := model.NewUser() + user.Username = userCreationRequest.Username + user.Password = userCreationRequest.Password + user.IsAdmin = userCreationRequest.IsAdmin + user.GoogleID = userCreationRequest.GoogleID + user.OpenIDConnectID = userCreationRequest.OpenIDConnectID + if err := user.ValidateUserCreation(); err != nil { json.BadRequest(w, r, err) return diff --git a/client/client.go b/client/client.go index 60797f5c..1f732376 100644 --- a/client/client.go +++ b/client/client.go @@ -96,7 +96,11 @@ func (c *Client) UserByUsername(username string) (*User, error) { // CreateUser creates a new user in the system. func (c *Client) CreateUser(username, password string, isAdmin bool) (*User, error) { - body, err := c.request.Post("/v1/users", &User{Username: username, Password: password, IsAdmin: isAdmin}) + body, err := c.request.Post("/v1/users", &UserCreationRequest{ + Username: username, + Password: password, + IsAdmin: isAdmin, + }) if err != nil { return nil, err } diff --git a/client/core.go b/client/core.go index 9a7818f2..80104097 100644 --- a/client/core.go +++ b/client/core.go @@ -40,16 +40,31 @@ func (u User) String() string { return fmt.Sprintf("#%d - %s (admin=%v)", u.ID, u.Username, u.IsAdmin) } -// UserModification is used to update a user. +// UserCreationRequest represents the request to create a user. +type UserCreationRequest struct { + Username string `json:"username"` + Password string `json:"password"` + IsAdmin bool `json:"is_admin"` + GoogleID string `json:"google_id"` + OpenIDConnectID string `json:"openid_connect_id"` +} + +// UserModification represents the request to update a user. type UserModification struct { - Username *string `json:"username"` - Password *string `json:"password"` - IsAdmin *bool `json:"is_admin"` - Theme *string `json:"theme"` - Language *string `json:"language"` - Timezone *string `json:"timezone"` - EntryDirection *string `json:"entry_sorting_direction"` - EntriesPerPage *int `json:"entries_per_page"` + Username *string `json:"username"` + Password *string `json:"password"` + IsAdmin *bool `json:"is_admin"` + Theme *string `json:"theme"` + Language *string `json:"language"` + Timezone *string `json:"timezone"` + EntryDirection *string `json:"entry_sorting_direction"` + Stylesheet *string `json:"stylesheet"` + GoogleID *string `json:"google_id"` + OpenIDConnectID *string `json:"openid_connect_id"` + EntriesPerPage *int `json:"entries_per_page"` + KeyboardShortcuts *bool `json:"keyboard_shortcuts"` + ShowReadingTime *bool `json:"show_reading_time"` + EntrySwipe *bool `json:"entry_swipe"` } // Users represents a list of users. diff --git a/go.mod b/go.mod index c6c167ea..23bfb1aa 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/prometheus/client_golang v1.9.0 github.com/rylans/getlang v0.0.0-20200505200108-4c3188ff8a2d github.com/stretchr/testify v1.6.1 // indirect + github.com/tdewolff/minify/v2 v2.9.10 // indirect golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 golang.org/x/net v0.0.0-20201029221708-28c70e62bb1d golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d diff --git a/go.sum b/go.sum index c66aecd4..5121c96a 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,7 @@ github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -69,6 +70,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= @@ -85,6 +87,7 @@ github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVB github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= github.com/fsnotify/fsnotify v1.4.3-0.20170329110642-4da3e2cfbabc/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/garyburd/redigo v1.1.1-0.20170914051019-70e1b1943d4f/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-delve/delve v1.5.0/go.mod h1:c6b3a1Gry6x8a4LGCe/CWzrocrfaHvkUxCj3k4bvSUQ= @@ -216,6 +219,7 @@ github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0U github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/magiconair/properties v1.7.4-0.20170902060319-8d7837e64d3c/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs= github.com/mattn/go-colorable v0.0.0-20170327083344-ded68f7a9561/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.0.10-0.20170816031813-ad5389df28cd/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -371,6 +375,13 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tdewolff/minify v1.1.0 h1:nxHQi1ML+g3ZbZHffiZ6eC7vMqNvSRfX3KB5Y5y/kfw= +github.com/tdewolff/minify v2.3.6+incompatible h1:2hw5/9ZvxhWLvBUnHE06gElGYz+Jv9R4Eys0XUzItYo= +github.com/tdewolff/minify/v2 v2.9.10 h1:p+ifTTl+JMFFLDYNAm7nxQ9XuCG10HTW00wlPAZ7aoE= +github.com/tdewolff/minify/v2 v2.9.10/go.mod h1:U1Nc+/YBSB0FPEarqcgkYH3Ep4DNyyIbOyl5P4eWMuo= +github.com/tdewolff/parse/v2 v2.5.5 h1:b7ICJa4I/54JQGEGgTte8DiyJPKcC5g8V773QMzkeUM= +github.com/tdewolff/parse/v2 v2.5.5/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho= +github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= @@ -462,6 +473,7 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -470,6 +482,7 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e h1:AyodaIpKjppX+cBfTASF2E1US3H2JFBj920Ot3rtDjs= diff --git a/tests/user_test.go b/tests/user_test.go index 26f4c855..bc0b396a 100644 --- a/tests/user_test.go +++ b/tests/user_test.go @@ -271,6 +271,39 @@ func TestUpdateUserTheme(t *testing.T) { } } +func TestUpdateUserFields(t *testing.T) { + username := getRandomUsername() + client := miniflux.New(testBaseURL, testAdminUsername, testAdminPassword) + user, err := client.CreateUser(username, testStandardPassword, false) + if err != nil { + t.Fatal(err) + } + + stylesheet := "body { color: red }" + swipe := false + entriesPerPage := 5 + user, err = client.UpdateUser(user.ID, &miniflux.UserModification{ + Stylesheet: &stylesheet, + EntrySwipe: &swipe, + EntriesPerPage: &entriesPerPage, + }) + if err != nil { + t.Fatal(err) + } + + if user.Stylesheet != stylesheet { + t.Fatalf(`Unable to update user stylesheet: got %q instead of %q`, user.Stylesheet, stylesheet) + } + + if user.EntrySwipe != swipe { + t.Fatalf(`Unable to update user EntrySwipe: got %v instead of %v`, user.EntrySwipe, swipe) + } + + if user.EntriesPerPage != entriesPerPage { + t.Fatalf(`Unable to update user EntriesPerPage: got %q instead of %q`, user.EntriesPerPage, entriesPerPage) + } +} + func TestUpdateUserThemeWithInvalidValue(t *testing.T) { username := getRandomUsername() client := miniflux.New(testBaseURL, testAdminUsername, testAdminPassword)