Skip to content

Commit

Permalink
set up http tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed Aug 17, 2024
1 parent 26d2694 commit bdc2d5d
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 0 deletions.
7 changes: 7 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
package malak

// Swagger generation
//
//go:generate swag init -g swagger.go
//
//
// Mocks generation
//go:generate mockgen -source=internal/pkg/socialauth/social.go -destination=internal/pkg/socialauth/mocks/social.go -package=socialauth_mocks
//go:generate mockgen -source=user.go -destination=mocks/user.go -package=malak_mocks
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/sdk/metric v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
go.uber.org/mock v0.4.0
golang.org/x/oauth2 v0.20.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeX
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
Expand Down
72 changes: 72 additions & 0 deletions internal/pkg/socialauth/mocks/social.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions mocks/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions server/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package server

import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/ayinke-llc/malak/config"
socialauth_mocks "github.com/ayinke-llc/malak/internal/pkg/socialauth/mocks"
malak_mocks "github.com/ayinke-llc/malak/mocks"
"github.com/sirupsen/logrus"
"go.uber.org/mock/gomock"
)

func getConfig() config.Config {
return config.Config{
Otel: struct {
Endpoint string "yaml:\"endpoint\" mapstructure:\"endpoint\""
UseTLS bool "yaml:\"use_tls\" mapstructure:\"use_tls\""
Headers string "yaml:\"headers\" mapstructure:\"headers\""
IsEnabled bool "yaml:\"is_enabled\" mapstructure:\"is_enabled\""
}{
IsEnabled: false,
},
}
}

func TestAuthHandler_Login(t *testing.T) {

tt := []struct {
name string
mockFn func(googleMock *socialauth_mocks.MockSocialAuthProvider, userRepo *malak_mocks.MockUserRepository)
expectedStatusCode int
}{
{
name: "no code to exchange provided",
mockFn: func(googleMock *socialauth_mocks.MockSocialAuthProvider, userRepo *malak_mocks.MockUserRepository) {
// googleMock.EXPECT().Validate()
},
},
}

for _, v := range tt {

t.Run(v.name, func(t *testing.T) {

logrus.SetOutput(io.Discard)

logger := logrus.WithField("test", true)

controller := gomock.NewController(t)
defer controller.Finish()

googleCfg := socialauth_mocks.NewMockSocialAuthProvider(controller)
userRepo := malak_mocks.NewMockUserRepository(controller)

a := &authHandler{
logger: logger,
cfg: getConfig(),
googleCfg: googleCfg,
userRepo: userRepo,
}

rr := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", strings.NewReader(""))

a.Login(rr, req)
})
}
}

0 comments on commit bdc2d5d

Please sign in to comment.