Skip to content

Commit

Permalink
✅ Remove Gingko/Gomega for Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Oct 31, 2023
1 parent d565126 commit 42422b1
Show file tree
Hide file tree
Showing 35 changed files with 3,481 additions and 3,748 deletions.
7 changes: 0 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,12 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand Down Expand Up @@ -77,15 +73,12 @@ require (
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

require (
github.com/goccy/go-json v0.10.2
github.com/gorilla/feeds v1.1.1
github.com/mattn/go-mastodon v0.0.6
github.com/onsi/ginkgo/v2 v2.12.1
github.com/onsi/gomega v1.28.0
golang.org/x/image v0.5.0 // indirect
)
13 changes: 0 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SU
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
Expand All @@ -115,8 +113,6 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
Expand Down Expand Up @@ -183,8 +179,6 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
Expand Down Expand Up @@ -251,10 +245,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/onsi/ginkgo/v2 v2.12.1 h1:uHNEO1RP2SpuZApSkel9nEh1/Mu+hmQe7Q+Pepg5OYA=
github.com/onsi/ginkgo/v2 v2.12.1/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.28.0 h1:i2rg/p9n/UqIDAMFUJ6qIUUMcsqOuUHgbpbu235Vr1c=
github.com/onsi/gomega v1.28.0/go.mod h1:A1H2JE76sI14WIP57LMKj7FVfCHx3g3BcZVjJG8bjX8=
github.com/paulmach/go.geojson v1.5.0 h1:7mhpMK89SQdHFcEGomT7/LuJhwhEgfmpWYVlVmLEdQw=
github.com/paulmach/go.geojson v1.5.0/go.mod h1:DgdUy2rRVDDVgKqrjMe2vZAHMfhDTrjVKt3LmHIXGbU=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
Expand Down Expand Up @@ -392,7 +382,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -563,8 +552,6 @@ golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
122 changes: 67 additions & 55 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -12,80 +11,93 @@ import (

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/systemli/ticker/internal/api/response"
"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/logger"
"github.com/systemli/ticker/internal/storage"
)

var l = logger.NewLogrus("debug", "text")
type APITestSuite struct {
cfg config.Config
store *storage.MockStorage
logger *logrus.Logger
suite.Suite
}

func init() {
logrus.SetOutput(io.Discard)
func (s *APITestSuite) SetupTest() {
gin.SetMode(gin.TestMode)
}
logrus.SetOutput(io.Discard)

func TestHealthz(t *testing.T) {
c := config.LoadConfig("")
s := &storage.MockStorage{}
r := API(c, s, l)
s.cfg = config.LoadConfig("")
s.store = &storage.MockStorage{}

logger := logger.NewLogrus("debug", "text")
logger.SetOutput(io.Discard)
s.logger = logger
}

func (s *APITestSuite) TestHealthz() {
r := API(s.cfg, s.store, s.logger)
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)

fmt.Println(w.Body.String())
assert.Equal(t, http.StatusOK, w.Code)
s.Equal(http.StatusOK, w.Code)
s.store.AssertExpectations(s.T())
}

func TestLoginNotSuccessful(t *testing.T) {
c := config.LoadConfig("")
s := &storage.MockStorage{}
user := storage.User{}
user.UpdatePassword("password")
s.On("FindUserByEmail", mock.Anything).Return(user, nil)
r := API(c, s, l)
func (s *APITestSuite) TestLogin() {
s.Run("when password is wrong", func() {
user, err := storage.NewUser("[email protected]", "password")
s.NoError(err)
s.store.On("FindUserByEmail", mock.Anything).Return(user, nil)
r := API(s.cfg, s.store, s.logger)

body := `{"username":"[email protected]","password":"WRONG"}`
req := httptest.NewRequest(http.MethodPost, "/v1/admin/login", strings.NewReader(body))
req.Header.Add("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
body := `{"username":"[email protected]","password":"WRONG"}`
req := httptest.NewRequest(http.MethodPost, "/v1/admin/login", strings.NewReader(body))
req.Header.Add("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)

var res response.Response
err := json.Unmarshal(w.Body.Bytes(), &res)
assert.Nil(t, err)
assert.Equal(t, http.StatusUnauthorized, w.Code)
assert.Nil(t, res.Data)
assert.Equal(t, res.Error.Code, response.CodeBadCredentials)
assert.Equal(t, res.Error.Message, response.Unauthorized)
}
var res response.Response
err = json.Unmarshal(w.Body.Bytes(), &res)
s.NoError(err)
s.Equal(http.StatusUnauthorized, w.Code)
s.Nil(res.Data)
s.Equal(res.Error.Code, response.CodeBadCredentials)
s.Equal(res.Error.Message, response.Unauthorized)
s.store.AssertExpectations(s.T())
})

func TestLoginSuccessful(t *testing.T) {
c := config.LoadConfig("")
s := &storage.MockStorage{}
user := storage.User{}
user.UpdatePassword("password")
s.On("FindUserByEmail", mock.Anything).Return(user, nil)
r := API(c, s, l)
s.Run("when login is successful", func() {
user, err := storage.NewUser("[email protected]", "password")
s.NoError(err)
s.store.On("FindUserByEmail", mock.Anything).Return(user, nil)
r := API(s.cfg, s.store, s.logger)

body := `{"username":"[email protected]","password":"password"}`
req := httptest.NewRequest(http.MethodPost, "/v1/admin/login", strings.NewReader(body))
req.Header.Add("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
body := `{"username":"[email protected]","password":"password"}`
req := httptest.NewRequest(http.MethodPost, "/v1/admin/login", strings.NewReader(body))
req.Header.Add("Content-Type", "application/json")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)

var res struct {
Code int `json:"code"`
Expire time.Time `json:"expire"`
Token string `json:"token"`
}
err = json.Unmarshal(w.Body.Bytes(), &res)
s.NoError(err)
s.Equal(http.StatusOK, w.Code)
s.Equal(http.StatusOK, res.Code)
s.NotNil(res.Expire)
s.NotNil(res.Token)
s.store.AssertExpectations(s.T())
})
}

var res struct {
Code int `json:"code"`
Expire time.Time `json:"expire"`
Token string `json:"token"`
}
err := json.Unmarshal(w.Body.Bytes(), &res)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, http.StatusOK, res.Code)
assert.NotNil(t, res.Expire)
assert.NotNil(t, res.Token)
func TestAPITestSuite(t *testing.T) {
suite.Run(t, new(APITestSuite))
}
23 changes: 15 additions & 8 deletions internal/api/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,35 @@ import (
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/storage"
)

func init() {
type FeaturesTestSuite struct {
suite.Suite
}

func (s *FeaturesTestSuite) SetupTest() {
gin.SetMode(gin.TestMode)
}

func TestGetFeatures(t *testing.T) {
func (s *FeaturesTestSuite) TestGetFeatures() {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
s := &storage.MockStorage{}
store := &storage.MockStorage{}

h := handler{
storage: s,
storage: store,
config: config.LoadConfig(""),
}

h.GetFeatures(c)

expected := `{"data":{"features":{"telegramEnabled":false}},"status":"success","error":{}}`
assert.Equal(t, expected, w.Body.String())
assert.Equal(t, http.StatusOK, w.Code)
s.Equal(http.StatusOK, w.Code)
s.Equal(`{"data":{"features":{"telegramEnabled":false}},"status":"success","error":{}}`, w.Body.String())
}

func TestFeaturesTestSuite(t *testing.T) {
suite.Run(t, new(FeaturesTestSuite))
}
Loading

0 comments on commit 42422b1

Please sign in to comment.