Skip to content

Commit

Permalink
Increase test coverage (#3)
Browse files Browse the repository at this point in the history
* Exclude some files from coverage report

* Exclude auth callback handler (it's already covered on other layer)

* Add a bit of tests and git rid of unused code

* Use `make test` in test job

* Fix temp dir for coverage profile
  • Loading branch information
tomakado authored Nov 17, 2022
1 parent 273e50b commit 70c7384
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 21 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ jobs:
go-version: 1.19

- name: test
run: |
go test -v -timeout=100s -covermode=count -coverprofile=$GITHUB_WORKSPACE/profile.cov_tmp ./...
cat $GITHUB_WORKSPACE/profile.cov_tmp | grep -v "mocks" | grep -v "_mock" | grep -v "mock_" | grep -v ".mock." > $GITHUB_WORKSPACE/profile.cov
run: make TESTS_WD=$GITHUB_WORKSPACE test

- name: install goveralls
run: GO111MODULE=off go get -u github.com/mattn/goveralls
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ go.work
/.dbschema/
.env
/bin/

/tests/*.cov
/tests/*.cov_tmp
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,24 @@ lint: .install-linter
lint-fast: .install-linter
$(GOLANGCI_LINT) run ./... --fast --config=./.golangci.yml

TESTS_WD = $(PROJECT_DIR)/tests

# === Test ===
.PHONY: test
test:
go test -v --race --timeout=5m --cover ./...
mkdir -p $(TESTS_WD)
go test -v --timeout=5m --covermode=count --coverprofile=$(TESTS_WD)/profile.cov_tmp ./...
cat $(TESTS_WD)/profile.cov_tmp | grep -v "mocks" | grep -v "_mock" | grep -v "mock_" \
| grep -v ".mock." | grep -v "server.go" | grep -v "handle_token_page.go" \
| grep -v "handle_gh_oauth_callback.go" | grep -v "handle_get_gh_auth_link.go" > $(TESTS_WD)/profile.cov

.PHONY: test-coverage
test-coverage: test
go tool cover --func=$(TESTS_WD)/profile.cov

.PHONY: test-coverage-html
test-coverage-html: test
go tool cover --html=$(TESTS_WD)/profile.cov

# === Build ===
.PHONY: build
Expand Down
17 changes: 0 additions & 17 deletions internal/auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/defer-panic/url-shortener-api/internal/config"
"github.com/defer-panic/url-shortener-api/internal/model"
"github.com/golang-jwt/jwt/v4"
"github.com/labstack/echo/v4"
)

func MakeJWT(user model.User) (string, error) {
Expand All @@ -22,19 +21,3 @@ func MakeJWT(user model.User) (string, error) {
return token.SignedString([]byte(config.Get().Auth.JWTSecretKey))
}

func VerifyJWT(src string, c echo.Context) (any, error) {
token, err := jwt.ParseWithClaims(
src,
&model.UserClaims{},
func(_ *jwt.Token) (any, error) { return []byte(config.Get().Auth.JWTSecretKey), nil },
)
if err != nil {
return nil, err
}

if !token.Valid {
return nil, model.ErrInvalidToken
}

return token, nil
}
11 changes: 11 additions & 0 deletions internal/auth/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ import (
"github.com/stretchr/testify/require"
)

func TestGetGitHubAuthLink(t *testing.T) {
t.Run("returns github auth link with client id", func(t *testing.T) {
svc := auth.NewService(nil, nil, "client-id", "")
assert.Equal(
t,
"https://github.com/login/oauth/authorize?scopes=user,read:org&client_id=client-id",
svc.GitHubAuthLink(),
)
})
}

func TestService_GitHubAuthCallback(t *testing.T) {
t.Run("returns user model and JWT", func(t *testing.T) {
var (
Expand Down
78 changes: 78 additions & 0 deletions internal/server/handle_stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package server_test

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/defer-panic/url-shortener-api/internal/model"
"github.com/defer-panic/url-shortener-api/internal/server"
"github.com/defer-panic/url-shortener-api/internal/shorten"
"github.com/defer-panic/url-shortener-api/internal/storage/shortening"
"github.com/labstack/echo/v4"
. "github.com/samber/mo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHandleStats(t *testing.T) {
t.Run("returns shortening with given identifier", func(t *testing.T) {
var (
provider = shorten.NewService(shortening.NewInMemory())
handler = server.HandleStats(provider)
recorder = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/stats/abc", nil)
e = echo.New()
c = e.NewContext(request, recorder)
)

addUserToCtx(c)

c.SetPath("/stats/:identifier")
c.SetParamNames("identifier")
c.SetParamValues("abc")

_, err := provider.Shorten(
context.Background(),
model.ShortenInput{
Identifier: Some("abc"),
RawURL: "https://google.com",
CreatedBy: "user",
},
)
require.NoError(t, err)

require.NoError(t, handler(c))

var s model.Shortening
require.NoError(t, json.NewDecoder(recorder.Body).Decode(&s))

assert.Equal(t, "abc", s.Identifier)
assert.Equal(t, "https://google.com", s.OriginalURL)
assert.Equal(t, "user", s.CreatedBy)
assert.Equal(t, int64(0), s.Visits)
})

t.Run("returns 404 if shortening with given identifier does not exist", func(t *testing.T) {
var (
provider = shorten.NewService(shortening.NewInMemory())
handler = server.HandleStats(provider)
recorder = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/stats/abc", nil)
e = echo.New()
c = e.NewContext(request, recorder)
)

addUserToCtx(c)

c.SetPath("/stats/:identifier")
c.SetParamNames("identifier")
c.SetParamValues("abc")

var httpErr *echo.HTTPError
require.ErrorAs(t, handler(c), &httpErr)
assert.Equal(t, http.StatusNotFound, httpErr.Code)
})
}

0 comments on commit 70c7384

Please sign in to comment.