generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 20
Feat(SPV-1546): delete user endpoint #955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dzolt-4chain
wants to merge
10
commits into
main
Choose a base branch
from
feat/SPV-1546/delete_user_endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
219ba98
feat(SPV-1546): add delete user endpoint
dzolt-4chain 9b42bd8
test(SPV-1546): delete user endpoint tests
dzolt-4chain e470ede
feat(SPV-1546): add api interface to service
dzolt-4chain b58b098
test(SPV-1546): comment skip tests
dzolt-4chain 33e433b
test(SPV-1546): add test case to user delete
dzolt-4chain 2b71763
feat(SPV-1546): happy linter
dzolt-4chain 9e23c4f
Merge branch 'main' into feat/SPV-1546/delete_user_endpoint
dzolt-4chain 2c65310
feat(SPV-1546): address CR
dzolt-4chain ff4e083
test(SPV-1546): manual tests helper for delete user
dorzepowski aa41a2c
feat(SPV-1546): address cR
dzolt-4chain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| package users | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
| "github.com/bitcoin-sv/spv-wallet/server/reqctx" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // DeleteCurrentUser attempts to delete current user | ||
| func (s *APIUsers) DeleteCurrentUser(c *gin.Context) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe because only admin can create a user, maybe only admin should rather have power to remove the user |
||
| userContext := reqctx.GetUserContext(c) | ||
| userID, err := userContext.ShouldGetUserID() | ||
| if err != nil { | ||
| spverrors.ErrorResponse(c, err, reqctx.Logger(c)) | ||
| return | ||
| } | ||
|
|
||
| err = s.usersService.Remove(c.Request.Context(), userID) | ||
| if err != nil { | ||
| spverrors.ErrorResponse(c, err, reqctx.Logger(c)) | ||
| return | ||
| } | ||
|
|
||
| c.Status(http.StatusOK) | ||
| } | ||
This file contains hidden or 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,133 @@ | ||
| package users_test | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/bitcoin-sv/spv-wallet/actions/testabilities" | ||
| "github.com/bitcoin-sv/spv-wallet/actions/testabilities/apierror" | ||
| testengine "github.com/bitcoin-sv/spv-wallet/engine/testabilities" | ||
| "github.com/bitcoin-sv/spv-wallet/engine/tester/fixtures" | ||
| ) | ||
|
|
||
| func TestCreateAndDeleteUser(t *testing.T) { | ||
| // given: | ||
| givenForAllTests := testabilities.Given(t) | ||
| cleanup := givenForAllTests.StartedSPVWalletWithConfiguration( | ||
| testengine.WithV2(), | ||
| ) | ||
| defer cleanup() | ||
|
|
||
| // and: | ||
| userCandidate := fixtures.User{ | ||
| PrivKey: "xprv9s21ZrQH143K3QFk3G7fGtpKfi6ws96DVzeXpvvZLUafPBwnpfbX7A343GU9jMrbvkoJR3UrdCKjwPhXrPAmzDfQ8ipo3zLryFvj2ABH1hn", | ||
| Paymails: []fixtures.Paymail{ | ||
| "test_user2@" + fixtures.PaymailDomain, | ||
| }, | ||
| } | ||
| publicKey := userCandidate.PublicKey().ToDERHex() | ||
|
|
||
| var testState struct { | ||
| userID string | ||
| } | ||
|
|
||
| t.Run("Create a user as admin", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| SetBody(map[string]any{ | ||
| "publicKey": publicKey, | ||
| "paymail": map[string]any{ | ||
| "address": userCandidate.DefaultPaymail(), | ||
| }, | ||
| }). | ||
| Post("/api/v2/admin/users") | ||
|
|
||
| // then: | ||
| then.Response(res).HasStatus(201) | ||
dzolt-4chain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // update: | ||
| getter := then.Response(res).JSONValue() | ||
| testState.userID = getter.GetString("id") | ||
| }) | ||
|
|
||
| t.Run("Get new user by id as admin", func(t *testing.T) { | ||
dzolt-4chain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| SetPathParam("id", testState.userID). | ||
| Get("/api/v2/admin/users/{id}") | ||
|
|
||
| // then: | ||
| then.Response(res).IsOK() | ||
| }) | ||
|
|
||
| t.Run("Delete user", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| client := given.HttpClient().ForGivenUser(userCandidate) | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| Delete("/api/v2/users/current") | ||
|
|
||
| // then: | ||
| then.Response(res).IsOK() | ||
| }) | ||
|
|
||
| t.Run("Try to get new user by id as admin after deletion", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| SetPathParam("id", testState.userID). | ||
| Get("/api/v2/admin/users/{id}") | ||
|
|
||
| // then: | ||
| then.Response(res).HasStatus(http.StatusNotFound).WithJSONf(apierror.ExpectedJSON("error-user-not-found", "user not found")) | ||
| }) | ||
|
|
||
| t.Run("Try to make a request as deleted user", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| client := given.HttpClient().ForGivenUser(userCandidate) | ||
|
|
||
| // when: | ||
| res, _ := client.R().Get("/api/v2/users/current") | ||
|
|
||
| // then: | ||
| then.Response(res).HasStatus(http.StatusUnauthorized).WithJSONf(apierror.ExpectedJSON("error-unauthorized", "unauthorized")) | ||
| }) | ||
| } | ||
|
|
||
| func TestDeleteUserWithUTXO(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.New(t) | ||
| cleanup := given.StartedSPVWalletWithConfiguration( | ||
| testengine.WithV2(), | ||
| ) | ||
| defer cleanup() | ||
|
|
||
| // and: | ||
| userCandidate := fixtures.Sender | ||
|
|
||
| // and: | ||
| given.Faucet(fixtures.Sender).TopUp(1000) | ||
|
|
||
| // given: | ||
| client := given.HttpClient().ForGivenUser(userCandidate) | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| Delete("/api/v2/users/current") | ||
|
|
||
| then.Response(res).HasStatus(http.StatusBadRequest).WithJSONf(apierror.ExpectedJSON("error-user-has-existing-utxos", "cannot delete user with existing UTXOs")) | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,22 +1,30 @@ | ||
| package users | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/bitcoin-sv/spv-wallet/engine" | ||
| "github.com/bitcoin-sv/spv-wallet/models/bsv" | ||
| "github.com/rs/zerolog" | ||
| ) | ||
|
|
||
| type usersService interface { | ||
| Remove(ctx context.Context, userID string) error | ||
| GetBalance(ctx context.Context, userID string) (bsv.Satoshis, error) | ||
| } | ||
|
|
||
| // APIUsers represents server with API endpoints | ||
| type APIUsers struct { | ||
| engine engine.ClientInterface | ||
| logger *zerolog.Logger | ||
| usersService usersService | ||
| logger *zerolog.Logger | ||
| } | ||
|
|
||
| // NewAPIUsers creates a new server with API endpoints | ||
| func NewAPIUsers(engine engine.ClientInterface, log *zerolog.Logger) APIUsers { | ||
| logger := log.With().Str("api", "users").Logger() | ||
|
|
||
| return APIUsers{ | ||
| engine: engine, | ||
| logger: &logger, | ||
| usersService: engine.UsersService(), | ||
dzolt-4chain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger: &logger, | ||
| } | ||
| } | ||
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.