-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bump `github.com/quarksgroup.com` to `v1.0.0`. * move http handlers to their own package `api/handlers`. * generate interface mocks at `mocks`. * add unit tests for package `github.com/rugwirobaker/helmes`, `github.com/rugwirobaker/helmes/api/handlers` * add a new type `github.com/rugwirobaker/helmes/Health` * rename `handlers.SMSHandler` to `handlers.SendService` * rename `helmes.Service` to `helmes.SendService`
- Loading branch information
1 parent
4fa9efd
commit 830ec3a
Showing
12 changed files
with
333 additions
and
32 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/rugwirobaker/helmes" | ||
"github.com/rugwirobaker/helmes/api/handlers" | ||
"github.com/rugwirobaker/helmes/mock" | ||
) | ||
|
||
var ( | ||
dummyMessage = &helmes.SMS{ | ||
Payload: "Hello", | ||
Recipient: "User_Phone", | ||
} | ||
dummyReport = &helmes.Report{ | ||
ID: "message id", | ||
Cost: 1, | ||
} | ||
) | ||
|
||
func TestSendHander(t *testing.T) { | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
sender := mock.NewMockSendService(controller) | ||
sender.EXPECT().Send(gomock.Any(), gomock.Any()).Return(dummyReport, nil) | ||
|
||
in := new(bytes.Buffer) | ||
|
||
_ = json.NewEncoder(in).Encode(dummyMessage) | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest("POST", "/", in) | ||
|
||
handlers.SendHandler(sender).ServeHTTP(w, r) | ||
if got, want := w.Code, http.StatusOK; want != got { | ||
t.Errorf("Want response code %d, got %d", want, got) | ||
} | ||
|
||
got, want := &helmes.Report{}, dummyReport | ||
json.NewDecoder(w.Body).Decode(got) | ||
if diff := cmp.Diff(got, want); len(diff) != 0 { | ||
t.Errorf(diff) | ||
} | ||
|
||
} | ||
|
||
func TestHealthHandler(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest("GET", "/healthz", nil) | ||
|
||
handlers.HealthHandler().ServeHTTP(w, r) | ||
|
||
if got, want := w.Code, 200; want != got { | ||
t.Errorf("Want response code %d, got %d", want, got) | ||
} | ||
} | ||
|
||
func TestVersionHandler(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest("GET", "/version", nil) | ||
|
||
handlers.VersionHandler().ServeHTTP(w, r) | ||
|
||
if got, want := w.Code, 200; want != got { | ||
t.Errorf("Want response code %d, got %d", want, got) | ||
} | ||
|
||
got, want := &helmes.Build{}, helmes.Data() | ||
json.NewDecoder(w.Body).Decode(got) | ||
if diff := cmp.Diff(got, want); len(diff) != 0 { | ||
t.Errorf(diff) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package helmes | ||
|
||
// Health report | ||
type Health struct { | ||
GitRev string `json:"git_rev"` | ||
Uptime float64 `json:"uptime"` | ||
Goroutines int `json:"goroutines"` | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package helmes_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/quarksgroup/sms-client/sms" | ||
"github.com/rugwirobaker/helmes" | ||
"github.com/rugwirobaker/helmes/mock/mocksmc" | ||
) | ||
|
||
var noContext = context.Background() | ||
|
||
func TestSend(t *testing.T) { | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
mockReport := &sms.Report{ | ||
ID: "fake_id", | ||
Cost: 1, | ||
} | ||
mockToken := &sms.Token{} | ||
mockSMS := &helmes.SMS{} | ||
|
||
mockSendService := mocksmc.NewMockSendService(controller) | ||
mockSendService.EXPECT().Send(gomock.Any(), gomock.Any()).Return(mockReport, nil, nil) | ||
|
||
mockAuthService := mocksmc.NewMockAuthService(controller) | ||
mockAuthService.EXPECT().Refresh(gomock.Any(), gomock.Any(), false).Return(mockToken, nil, nil) | ||
mockAuthService.EXPECT().Login(gomock.Any(), "id", "secret").Return(mockToken, nil, nil) | ||
|
||
client := new(sms.Client) | ||
client.Message = mockSendService | ||
client.Auth = mockAuthService | ||
|
||
service, _ := helmes.New(client, "id", "secret", "sender", "callback") | ||
|
||
want := &helmes.Report{ | ||
ID: "fake_id", | ||
Cost: 1, | ||
} | ||
|
||
got, err := service.Send(noContext, mockSMS) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if diff := cmp.Diff(got, want); diff != "" { | ||
t.Errorf(diff) | ||
} | ||
|
||
} |
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,3 @@ | ||
package mock | ||
|
||
//go:generate mockgen -package=mock -destination=mock_gen.go github.com/rugwirobaker/helmes SendService |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 @@ | ||
package mocksmc | ||
|
||
//go:generate mockgen -package=mocksmc -destination=mock_gen.go github.com/quarksgroup/sms-client/sms SendService,AuthService |
Oops, something went wrong.