-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from perebaj/feat_authorization_email
✨ feat: authorization email
- Loading branch information
Showing
10 changed files
with
138 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -34,13 +34,14 @@ func TestConvert(t *testing.T) { | |
content := `12022-01-15T19:20:30-03:00CURSO DE BEM-ESTAR 0000012750JOSE CARLOS | ||
12021-12-03T11:46:02-03:00DOMINANDO INVESTIMENTOS 0000050000MARIA CANDIDA | ||
` | ||
transac, err := convert(content) | ||
transac, err := convert(content, "[email protected]") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := []contractus.Transaction{ | ||
{ | ||
Email: "[email protected]", | ||
Type: 1, | ||
Date: time.Date(2022, 01, 15, 22, 20, 30, 0, time.UTC), | ||
ProductDescription: "CURSO DE BEM-ESTAR", | ||
|
@@ -50,6 +51,7 @@ func TestConvert(t *testing.T) { | |
Action: "venda produtor", | ||
}, | ||
{ | ||
Email: "[email protected]", | ||
Type: 1, | ||
Date: time.Date(2021, 12, 03, 14, 46, 02, 0, time.UTC), | ||
ProductDescription: "DOMINANDO INVESTIMENTOS", | ||
|
@@ -61,6 +63,7 @@ func TestConvert(t *testing.T) { | |
} | ||
|
||
if len(transac) == len(want) { | ||
assert(t, transac[0].Email, want[0].Email) | ||
assert(t, transac[0].Type, want[0].Type) | ||
assert(t, transac[0].Date.Format(time.RFC3339), want[0].Date.Format(time.RFC3339)) | ||
assert(t, transac[0].ProductDescription, want[0].ProductDescription) | ||
|
@@ -69,6 +72,7 @@ func TestConvert(t *testing.T) { | |
assert(t, transac[0].SellerType, want[0].SellerType) | ||
assert(t, transac[0].Action, want[0].Action) | ||
|
||
assert(t, transac[1].Email, want[1].Email) | ||
assert(t, transac[1].Type, want[1].Type) | ||
assert(t, transac[1].Date.Format(time.RFC3339), want[1].Date.Format(time.RFC3339)) | ||
assert(t, transac[1].ProductDescription, want[1].ProductDescription) | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"testing" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/jwtauth/v5" | ||
"github.com/perebaj/contractus" | ||
) | ||
|
||
|
@@ -19,11 +20,11 @@ func (m *mockTransactionStorage) SaveTransaction(_ context.Context, _ []contract | |
return nil | ||
} | ||
|
||
func (m *mockTransactionStorage) Balance(_ context.Context, _ string, _ string) (*contractus.BalanceResponse, error) { | ||
func (m *mockTransactionStorage) Balance(_ context.Context, _ string, _ string, _ string) (*contractus.BalanceResponse, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m *mockTransactionStorage) Transactions(_ context.Context) (contractus.TransactionResponse, error) { | ||
func (m *mockTransactionStorage) Transactions(_ context.Context, _ string) (contractus.TransactionResponse, error) { | ||
return contractus.TransactionResponse{}, nil | ||
} | ||
|
||
|
@@ -48,13 +49,20 @@ func TestTransactionHandlerUpload(t *testing.T) { | |
m := &mockTransactionStorage{} | ||
r := chi.NewRouter() | ||
|
||
RegisterTransactionsHandler(r, m) | ||
tokenAuth := jwtauth.New("HS256", []byte("secret"), nil) | ||
_, token, _ := tokenAuth.Encode(map[string]interface{}{"email": "[email protected]"}) | ||
|
||
r.Group(func(r chi.Router) { | ||
r.Use(jwtauth.Verifier(tokenAuth)) | ||
r.Use(jwtauth.Authenticator) | ||
RegisterTransactionsHandler(r, m) | ||
}) | ||
|
||
req := httptest.NewRequest(http.MethodPost, "/upload", &b) | ||
req.Header.Set("Content-Type", w.FormDataContentType()) | ||
req.AddCookie(&http.Cookie{ | ||
Name: "jwt", | ||
Value: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBlcmViYWpAZ21haWwuY29tIn0.7uJvLACFC_2470iO_G8_xLaa1ChFgxQxHBvS9nzOMDM", | ||
Value: token, | ||
}) | ||
resp := httptest.NewRecorder() | ||
|
||
|
@@ -66,36 +74,48 @@ func TestTransactionHandlerUpload(t *testing.T) { | |
} | ||
|
||
func TestTransactionHandlerBalanceProducer(t *testing.T) { | ||
tokenAuth := jwtauth.New("HS256", []byte("secret"), nil) | ||
_, token, _ := tokenAuth.Encode(map[string]interface{}{"email": "[email protected]"}) | ||
|
||
m := &mockTransactionStorage{} | ||
r := chi.NewRouter() | ||
|
||
RegisterTransactionsHandler(r, m) | ||
r.Group(func(r chi.Router) { | ||
r.Use(jwtauth.Verifier(tokenAuth)) | ||
r.Use(jwtauth.Authenticator) | ||
RegisterTransactionsHandler(r, m) | ||
}) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/balance/producer?name=JOSE%20CARLOS", nil) | ||
req.AddCookie(&http.Cookie{ | ||
Name: "jwt", | ||
Value: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBlcmViYWpAZ21haWwuY29tIn0.7uJvLACFC_2470iO_G8_xLaa1ChFgxQxHBvS9nzOMDM", | ||
Value: token, | ||
}) | ||
|
||
resp := httptest.NewRecorder() | ||
|
||
r.ServeHTTP(resp, req) | ||
|
||
if resp.Code != http.StatusOK { | ||
t.Fatalf("expected status code %d, got %d", http.StatusOK, resp.Code) | ||
t.Fatalf("expected status code %d, got %d, Response Body %s", http.StatusOK, resp.Code, resp.Body.String()) | ||
} | ||
} | ||
|
||
func TestTransactionHandlerBalanceAffiliate(t *testing.T) { | ||
m := &mockTransactionStorage{} | ||
r := chi.NewRouter() | ||
tokenAuth := jwtauth.New("HS256", []byte("secret"), nil) | ||
_, token, _ := tokenAuth.Encode(map[string]interface{}{"email": "[email protected]"}) | ||
|
||
RegisterTransactionsHandler(r, m) | ||
r.Group(func(r chi.Router) { | ||
r.Use(jwtauth.Verifier(tokenAuth)) | ||
r.Use(jwtauth.Authenticator) | ||
RegisterTransactionsHandler(r, m) | ||
}) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/balance/affiliate?name=JOSE%20CARLOS", nil) | ||
req.AddCookie(&http.Cookie{ | ||
Name: "jwt", | ||
Value: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBlcmViYWpAZ21haWwuY29tIn0.7uJvLACFC_2470iO_G8_xLaa1ChFgxQxHBvS9nzOMDM", | ||
Value: token, | ||
}) | ||
|
||
resp := httptest.NewRecorder() | ||
|
@@ -110,13 +130,19 @@ func TestTransactionHandlerBalanceAffiliate(t *testing.T) { | |
func TestTransactionHandlerTransactions(t *testing.T) { | ||
m := &mockTransactionStorage{} | ||
r := chi.NewRouter() | ||
tokenAuth := jwtauth.New("HS256", []byte("secret"), nil) | ||
_, token, _ := tokenAuth.Encode(map[string]interface{}{"email": "[email protected]"}) | ||
|
||
RegisterTransactionsHandler(r, m) | ||
r.Group(func(r chi.Router) { | ||
r.Use(jwtauth.Verifier(tokenAuth)) | ||
r.Use(jwtauth.Authenticator) | ||
RegisterTransactionsHandler(r, m) | ||
}) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/transactions", nil) | ||
req.AddCookie(&http.Cookie{ | ||
Name: "jwt", | ||
Value: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBlcmViYWpAZ21haWwuY29tIn0.7uJvLACFC_2470iO_G8_xLaa1ChFgxQxHBvS9nzOMDM", | ||
Value: token, | ||
}) | ||
|
||
resp := httptest.NewRecorder() | ||
|
Empty file.
This file contains 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,12 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE transactions ADD COLUMN email TEXT; | ||
|
||
UPDATE transactions SET email = '[email protected]' WHERE email IS NULL; | ||
|
||
ALTER TABLE transactions ALTER COLUMN email SET NOT NULL ; | ||
|
||
ALTER TABLE transactions ADD CONSTRAINT empty_email CHECK (email <> ''); | ||
|
||
COMMIT; | ||
|
Oops, something went wrong.