generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 20
feat(SPV-1538): migrate contact endpoints to v2 API #931
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
pawellewandowski98
wants to merge
30
commits into
main
Choose a base branch
from
feat/SPV-1538-migrate-contacts
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 all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
51e690f
feat(SPV-1538): implement upsert contact
pawellewandowski98 ceb7d4b
feat: add mapping for contact
pawellewandowski98 bc64687
feat(SPV-1538): add new contact endpoints
pawellewandowski98 e58bfab
feat: add admin contact endpoints
pawellewandowski98 e8ded44
feat: add accept and reject admin endpoints
pawellewandowski98 2e2850e
feat: update errors
pawellewandowski98 604d4c2
fix: fix api methods and regenerate
pawellewandowski98 aaf313f
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/SPV…
pawellewandowski98 ed05818
chore: generate after merge
pawellewandowski98 8b056d2
test: add tests for admin endpoints
pawellewandowski98 0c72c52
tests: add eew tests for contacts
pawellewandowski98 ce2f845
tests: add contact test for the rest ofthe endpoints
pawellewandowski98 d6bb02a
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/SPV…
pawellewandowski98 c1a04a4
fix: yaml linter
pawellewandowski98 d6a44ae
fix: linter errors
pawellewandowski98 f1d5f08
fix: linter errors
pawellewandowski98 c077248
fix: linter error
pawellewandowski98 22428a8
tests: refactor the way of creating endpoint url
pawellewandowski98 c1186bc
test: rename ContactFixture
pawellewandowski98 07d1058
tests: create new tests when needed
pawellewandowski98 67deaa0
chore: use gin context
pawellewandowski98 e085f6f
test: refactor the way of checking errors
pawellewandowski98 7da9cd8
chore: remove search endpoints
pawellewandowski98 ae36b57
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/SPV…
pawellewandowski98 aada719
fix: fix creating api v2 after merge
pawellewandowski98 8e18e4d
fix: linter errors
pawellewandowski98 12b5e39
feat: use contacts service interface instead of engine
pawellewandowski98 df89efc
tests: fix tests
pawellewandowski98 c74a6c7
tests: fix tests
pawellewandowski98 7cc7c62
Merge branch 'main' into feat/SPV-1538-migrate-contacts
pawellewandowski98 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package contacts | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/bitcoin-sv/spv-wallet/actions/v2/internal/mapping" | ||
| "github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // AdminAcceptInvitation accepts an invitation from a contact. | ||
| func (s *APIAdminContacts) AdminAcceptInvitation(c *gin.Context, id uint) { | ||
| contact, err := s.contactsService.AcceptContactByID(c, id) | ||
| if err != nil { | ||
| spverrors.ErrorResponse(c, err, s.logger) | ||
| return | ||
| } | ||
|
|
||
| res := mapping.MapToContactContract(contact) | ||
|
|
||
| c.JSON(http.StatusOK, res) | ||
| } |
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,131 @@ | ||
| package contacts_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "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" | ||
| "github.com/bitcoin-sv/spv-wallet/engine/v2/contacts/contactsmodels" | ||
| ) | ||
|
|
||
| func TestAcceptContact(t *testing.T) { | ||
| // given: | ||
| givenForAllTests := testabilities.Given(t) | ||
| cleanup := givenForAllTests.StartedSPVWalletWithConfiguration( | ||
| testengine.WithV2(), | ||
| ) | ||
| defer cleanup() | ||
|
|
||
| t.Run("Accept contact", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| contact := given.User(fixtures.Sender).HasContactTo(fixtures.RecipientInternal) | ||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| Post(fmt.Sprintf("/api/v2/admin/invitations/%d", contact.ID)) | ||
|
|
||
| // then: | ||
| then.Response(res). | ||
| HasStatus(200). | ||
| WithJSONMatching(`{ | ||
| "id": "{{ matchNumber }}", | ||
| "createdAt": "{{ matchTimestamp }}", | ||
| "updatedAt": "{{ matchTimestamp }}", | ||
| "fullName": "{{ .fullName }}", | ||
| "paymail": "{{ .paymail }}", | ||
| "pubKey": "{{ matchHexWithLength 66 }}", | ||
| "status": "{{ .status }}" | ||
| }`, map[string]any{ | ||
| "fullName": fixtures.RecipientInternal.DefaultPaymail().PublicName(), | ||
| "paymail": fixtures.RecipientInternal.DefaultPaymail().String(), | ||
| "status": contactsmodels.ContactNotConfirmed, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("Accept already accepted contact", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| contact := given.User(fixtures.Sender).HasContactTo(fixtures.RecipientInternal) | ||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| Post(fmt.Sprintf("/api/v2/admin/invitations/%d", contact.ID)) | ||
|
|
||
| // then: | ||
| then.Response(res). | ||
| HasStatus(200). | ||
| WithJSONMatching(`{ | ||
| "id": "{{ matchNumber }}", | ||
| "createdAt": "{{ matchTimestamp }}", | ||
| "updatedAt": "{{ matchTimestamp }}", | ||
| "fullName": "{{ .fullName }}", | ||
| "paymail": "{{ .paymail }}", | ||
| "pubKey": "{{ matchHexWithLength 66 }}", | ||
| "status": "{{ .status }}" | ||
| }`, map[string]any{ | ||
| "fullName": fixtures.RecipientInternal.DefaultPaymail().PublicName(), | ||
| "paymail": fixtures.RecipientInternal.DefaultPaymail().String(), | ||
| "status": contactsmodels.ContactNotConfirmed, | ||
| }) | ||
|
|
||
| // when: | ||
| res, _ = client.R(). | ||
| Post(fmt.Sprintf("/api/v2/admin/invitations/%d", contact.ID)) | ||
|
|
||
| // then: | ||
| then.Response(res). | ||
| HasStatus(200). | ||
| WithJSONMatching(`{ | ||
| "id": "{{ matchNumber }}", | ||
| "createdAt": "{{ matchTimestamp }}", | ||
| "updatedAt": "{{ matchTimestamp }}", | ||
| "fullName": "{{ .fullName }}", | ||
| "paymail": "{{ .paymail }}", | ||
| "pubKey": "{{ matchHexWithLength 66 }}", | ||
| "status": "{{ .status }}" | ||
| }`, map[string]any{ | ||
| "fullName": fixtures.RecipientInternal.DefaultPaymail().PublicName(), | ||
| "paymail": fixtures.RecipientInternal.DefaultPaymail().String(), | ||
| "status": contactsmodels.ContactNotConfirmed, | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("Accept contact with user xpub", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| contact := given.User(fixtures.Sender).HasContactTo(fixtures.RecipientInternal) | ||
|
|
||
| client := given.HttpClient().ForUser() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| Post(fmt.Sprintf("/api/v2/admin/invitations/%d", contact.ID)) | ||
|
|
||
| // then: | ||
| then.Response(res). | ||
| HasStatus(401). | ||
| WithJSONf(apierror.ExpectedJSON("error-unauthorized-xpub-not-an-admin-key", "xpub provided is not an admin key")) | ||
| }) | ||
|
|
||
| t.Run("No contact to accept", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| Post("/api/v2/admin/invitations/99999") | ||
|
|
||
| // then: | ||
| then.Response(res). | ||
| HasStatus(500). | ||
| WithJSONf(apierror.ExpectedJSON("error-contact-updating-status-failed", "updating contact status failed")) | ||
|
|
||
| }) | ||
| } |
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,26 @@ | ||
| package contacts | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/bitcoin-sv/spv-wallet/api" | ||
| "github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // AdminConfirmContact confirms a contact between two users. | ||
| func (s *APIAdminContacts) AdminConfirmContact(c *gin.Context) { | ||
| var reqParams *api.RequestsAdminConfirmContact | ||
| if err := c.Bind(&reqParams); err != nil { | ||
| spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.WithTrace(err), s.logger) | ||
| return | ||
| } | ||
|
|
||
| if err := s.contactsService.AdminConfirmContacts(c, reqParams.PaymailA, reqParams.PaymailB); err != nil { | ||
| spverrors.ErrorResponse(c, err, s.logger) | ||
| 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,130 @@ | ||
| package contacts_test | ||
|
|
||
| import ( | ||
| "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 TestConfirmContact(t *testing.T) { | ||
| // given: | ||
| givenForAllTests := testabilities.Given(t) | ||
| cleanup := givenForAllTests.StartedSPVWalletWithConfiguration( | ||
| testengine.WithV2(), | ||
| ) | ||
| defer cleanup() | ||
|
|
||
| t.Run("No side has contact", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
|
|
||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| SetBody(map[string]any{ | ||
| "paymailA": fixtures.Sender.DefaultPaymail().String(), | ||
| "paymailB": fixtures.RecipientInternal.DefaultPaymail().String(), | ||
| }). | ||
| Post("/api/v2/admin/contacts/confirmations") | ||
|
|
||
| // then: | ||
| then.Response(res).HasStatus(500). | ||
| WithJSONf(apierror.ExpectedJSON("error-contact-getting-contact-failed", "getting contact failed")) | ||
| }) | ||
|
|
||
| t.Run("Only one side has contact", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| given.User(fixtures.Sender).HasContactTo(fixtures.RecipientInternal) | ||
|
|
||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| SetBody(map[string]any{ | ||
| "paymailA": fixtures.Sender.DefaultPaymail().String(), | ||
| "paymailB": fixtures.RecipientInternal.DefaultPaymail().String(), | ||
| }). | ||
| Post("/api/v2/admin/contacts/confirmations") | ||
|
|
||
| // then: | ||
| then.Response(res).HasStatus(500). | ||
| WithJSONf(apierror.ExpectedJSON("error-contact-getting-contact-failed", "getting contact failed")) | ||
| }) | ||
|
|
||
| t.Run("Confirm contact", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| given.User(fixtures.Sender).HasContactTo(fixtures.RecipientInternal) | ||
| given.User(fixtures.RecipientInternal).HasContactTo(fixtures.Sender) | ||
|
|
||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| SetBody(map[string]any{ | ||
| "paymailA": fixtures.Sender.DefaultPaymail().String(), | ||
| "paymailB": fixtures.RecipientInternal.DefaultPaymail().String(), | ||
| }). | ||
| Post("/api/v2/admin/contacts/confirmations") | ||
|
|
||
| // then: | ||
| then.Response(res).IsOK() | ||
| }) | ||
|
|
||
| t.Run("Confirm already confirmed contact", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
| given.User(fixtures.Sender).HasContactTo(fixtures.RecipientInternal) | ||
| given.User(fixtures.RecipientInternal).HasContactTo(fixtures.Sender) | ||
|
|
||
| client := given.HttpClient().ForAdmin() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| SetBody(map[string]any{ | ||
| "paymailA": fixtures.Sender.DefaultPaymail().String(), | ||
| "paymailB": fixtures.RecipientInternal.DefaultPaymail().String(), | ||
| }). | ||
| Post("/api/v2/admin/contacts/confirmations") | ||
|
|
||
| // then: | ||
| then.Response(res).IsOK() | ||
|
|
||
| // when: | ||
| res, _ = client.R(). | ||
| SetBody(map[string]any{ | ||
| "paymailA": fixtures.Sender.DefaultPaymail().String(), | ||
| "paymailB": fixtures.RecipientInternal.DefaultPaymail().String(), | ||
| }). | ||
| Post("/api/v2/admin/contacts/confirmations") | ||
|
|
||
| // then: | ||
| then.Response(res).IsOK() | ||
| }) | ||
|
|
||
| t.Run("Confirm contact with user xpub", func(t *testing.T) { | ||
| // given: | ||
| given, then := testabilities.NewOf(givenForAllTests, t) | ||
|
|
||
| client := given.HttpClient().ForUser() | ||
|
|
||
| // when: | ||
| res, _ := client.R(). | ||
| SetBody(map[string]any{ | ||
| "paymailA": fixtures.RecipientExternal.DefaultPaymail().String(), | ||
| "paymailB": fixtures.Sender.DefaultPaymail().String(), | ||
| }). | ||
| Post("/api/v2/admin/contacts/confirmations") | ||
|
|
||
| // then: | ||
| then.Response(res). | ||
| HasStatus(401). | ||
| WithJSONf(apierror.ExpectedJSON("error-unauthorized-xpub-not-an-admin-key", "xpub provided is not an admin key")) | ||
| }) | ||
|
|
||
| } | ||
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,35 @@ | ||
| package contacts | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/bitcoin-sv/spv-wallet/actions/v2/internal/mapping" | ||
| "github.com/bitcoin-sv/spv-wallet/api" | ||
| "github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
| "github.com/bitcoin-sv/spv-wallet/engine/v2/contacts/contactsmodels" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // AdminCreateContact creates a new contact for a user. | ||
| func (s *APIAdminContacts) AdminCreateContact(c *gin.Context, paymail string) { | ||
| var req api.RequestsAdminCreateContact | ||
| if err := c.Bind(&req); err != nil { | ||
| spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.WithTrace(err), s.logger) | ||
| return | ||
| } | ||
|
|
||
| newContact := contactsmodels.NewContact{ | ||
| FullName: req.FullName, | ||
| NewContactPaymail: paymail, | ||
| RequesterPaymail: req.CreatorPaymail, | ||
| Status: contactsmodels.ContactNotConfirmed, | ||
| } | ||
|
|
||
| contact, err := s.contactsService.AdminCreateContact(c, newContact) | ||
| if err != nil { | ||
| spverrors.ErrorResponse(c, err, s.logger) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, mapping.MapToContactContract(contact)) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary
// and:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done