Skip to content

Commit

Permalink
Implement user password reset (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Sep 15, 2021
1 parent 9db01ab commit 567d8c6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
14 changes: 7 additions & 7 deletions fastly/fixtures/users/get_current_user.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ interactions:
form: {}
headers:
User-Agent:
- FastlyGo/2.1.0 (+github.com/fastly/go-fastly; go1.15.4)
- FastlyGo/3.10.0 (+github.com/fastly/go-fastly; go1.17)
url: https://api.fastly.com/current_user
method: GET
response:
body: '{"id":"1tSCjJnpQYBBCAsIfI6pX0","created_at":"2020-11-04T09:51:20Z","updated_at":"2021-01-13T13:48:29Z","name":"Mark
McDonnell","customer_id":"51MumwLiSJyFTWhtbByYgR","require_new_password":null,"role":"superuser","login":"mmcdonnell+experiments@fastly.com","deleted_at":null,"locked":null,"two_factor_auth_enabled":null,"limit_services":false}'
body: '{"id":"1tSCjJnpQYBBCAsIfI6pX0","created_at":"2020-11-04T09:51:20Z","updated_at":"2021-07-27T12:45:51Z","name":"Mark
McDonnell","customer_id":"51MumwLiSJyFTWhtbByYgR","require_new_password":null,"role":"superuser","login":"mmcdonnell+experiments@fastly.com","deleted_at":null,"locked":null,"two_factor_auth_enabled":null,"limit_services":false,"last_active_at":"2021-07-27T12:45:51Z"}'
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-cache
- no-store
Content-Type:
- application/json
Date:
- Thu, 14 Jan 2021 10:53:07 GMT
- Tue, 14 Sep 2021 15:35:48 GMT
Status:
- 200 OK
Strict-Transport-Security:
Expand All @@ -34,9 +34,9 @@ interactions:
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-slwdc9036-CONTROL-SLWDC, cache-man4135-MAN
- cache-control-slwdc9036-CONTROL-SLWDC, cache-man4124-MAN
X-Timer:
- S1610621587.401013,VS0,VE149
- S1631633748.452364,VS0,VE259
status: 200 OK
code: 200
duration: ""
45 changes: 45 additions & 0 deletions fastly/fixtures/users/reset_password.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
User-Agent:
- FastlyGo/3.10.0 (+github.com/fastly/go-fastly; go1.17)
url: https://api.fastly.com/user/go-fastly-test+user1@example.com/password/request_reset
method: POST
response:
body: '{"status":"ok"}'
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-store
Content-Type:
- application/json
Date:
- Tue, 14 Sep 2021 15:34:45 GMT
Fastly-Ratelimit-Remaining:
- "4997"
Fastly-Ratelimit-Reset:
- "1631635200"
Status:
- 200 OK
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish, 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-slwdc9037-CONTROL-SLWDC, cache-man4129-MAN
X-Timer:
- S1631633685.005810,VS0,VE260
status: 200 OK
code: 200
duration: ""
28 changes: 28 additions & 0 deletions fastly/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fastly

import (
"fmt"
"net/url"
"sort"
"time"
)
Expand Down Expand Up @@ -184,3 +185,30 @@ func (c *Client) DeleteUser(i *DeleteUserInput) error {
}
return nil
}

// ResetUserPasswordInput is used as input to the ResetUserPassword function.
type ResetUserPasswordInput struct {
Login string
}

// ResetUserPassword revokes a specific token by its ID.
func (c *Client) ResetUserPassword(i *ResetUserPasswordInput) error {
if i.Login == "" {
return ErrMissingLogin
}

path := fmt.Sprintf("/user/%s/password/request_reset", url.PathEscape(i.Login))
resp, err := c.Post(path, nil)
if err != nil {
return err
}

var r *statusResp
if err := decodeBodyMap(resp.Body, &r); err != nil {
return err
}
if !r.Ok() {
return ErrNotOK
}
return nil
}
19 changes: 19 additions & 0 deletions fastly/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ func TestClient_Users(t *testing.T) {
t.Errorf("bad role: %q", uu.Role)
}

// Reset Password
record(t, fixtureBase+"reset_password", func(c *Client) {
err = c.ResetUserPassword(&ResetUserPasswordInput{
Login: uu.Login,
})
})
if err != nil {
t.Fatal(err)
}

// Delete
record(t, fixtureBase+"delete", func(c *Client) {
err = c.DeleteUser(&DeleteUserInput{
Expand Down Expand Up @@ -172,3 +182,12 @@ func TestClient_DeleteUser_validation(t *testing.T) {
t.Errorf("bad error: %s", err)
}
}

func TestClient_ResetUser_validation(t *testing.T) {
err := testClient.ResetUserPassword(&ResetUserPasswordInput{
Login: "",
})
if err != ErrMissingLogin {
t.Errorf("bad error: %s", err)
}
}

0 comments on commit 567d8c6

Please sign in to comment.