Skip to content

Commit 1769f3d

Browse files
committed
Added more tests
1 parent 8a2d193 commit 1769f3d

File tree

6 files changed

+88
-15
lines changed

6 files changed

+88
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,6 @@ Usage only `gin` package and oauth2 client/server mechanic
272272

273273
# Testing
274274
```
275+
export APP_WD=go_path to app/rsources or app/artifacts - needed for load templates
275276
➜ make test
276277
```

app/entrypoint/entrypoint.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ import (
55
"os"
66
"sync"
77

8-
"github.com/pkg/errors"
98
"github.com/spf13/viper"
109
)
1110

1211
var (
13-
vi *viper.Viper
14-
mu sync.Mutex
15-
reloadCh = make(chan struct{})
16-
shutdownCtx context.Context
17-
cancelFn context.CancelFunc
18-
errAlreadyInitialized = errors.New("entrypoint already initialized")
19-
ep *EntryPoint
20-
wd string
12+
vi *viper.Viper
13+
mu sync.Mutex
14+
reloadCh = make(chan struct{})
15+
shutdownCtx context.Context
16+
cancelFn context.CancelFunc
17+
ep *EntryPoint
18+
wd string
2119
)
2220

2321
const prefix = "app.entrypoint"
@@ -42,7 +40,7 @@ func Initialize(workDir string, v *viper.Viper) (*EntryPoint, error) {
4240
mu.Lock()
4341
defer mu.Unlock()
4442
if ep != nil {
45-
return nil, errors.WithMessage(errAlreadyInitialized, prefix)
43+
return ep, nil
4644
}
4745
if len(workDir) > 0 {
4846
wd = workDir

app/entrypoint/provider.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package entrypoint
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
57

68
"github.com/google/wire"
79
)
@@ -14,8 +16,12 @@ func ContextProvider() (context.Context, func(), error) {
1416

1517
// ContextProviderTest
1618
func ContextProviderTest() (context.Context, func(), error) {
19+
// initializing
20+
wd, _ := filepath.Abs(os.Getenv("APP_WD"))
21+
_, err := Initialize(wd, nil)
22+
1723
c := context.Background()
18-
return c, func() {}, nil
24+
return c, func() {}, err
1925
}
2026

2127
var (

app/http/http.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/go-session/session"
1111

1212
"github.com/aristat/golang-example-app/app/logger"
13-
oauth_router "github.com/aristat/golang-example-app/app/routers/oauth-router"
1413
)
1514

1615
const prefix = "app.http"
@@ -25,7 +24,6 @@ type Config struct {
2524
type Http struct {
2625
ctx context.Context
2726
cfg Config
28-
oauth *oauth_router.Manager
2927
session *session.Manager
3028
log logger.Logger
3129
mux *chi.Mux
@@ -66,7 +64,6 @@ func New(ctx context.Context, mux *chi.Mux, log logger.Logger, cfg Config, manag
6664
return &Http{
6765
ctx: ctx,
6866
cfg: cfg,
69-
oauth: managers.oauth,
7067
session: managers.session,
7168
mux: mux,
7269
log: log.WithFields(logger.Fields{"service": prefix}),

app/routers/users-router/router.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ func (service *Router) PostLogin(w http.ResponseWriter, r *http.Request) {
6565

6666
currentUser, err := service.repo.Users.FindByEmail(email)
6767
if err != nil {
68+
w.WriteHeader(http.StatusNotFound)
6869
service.template.ExecuteTemplate(w, "users/login", H{"errors": []string{userNotFound.Error()}})
6970
return
7071
}
7172

7273
if common.CheckPasswordHash(password, currentUser.EncryptedPassword) == false {
74+
w.WriteHeader(http.StatusNotFound)
7375
service.template.ExecuteTemplate(w, "users/login", H{"errors": []string{userNotFound.Error()}})
7476
return
7577
}
@@ -84,7 +86,7 @@ func (service *Router) PostLogin(w http.ResponseWriter, r *http.Request) {
8486
}
8587

8688
w.Header().Set("Location", "/login")
87-
w.WriteHeader(http.StatusFound)
89+
w.WriteHeader(http.StatusNotFound)
8890
}
8991

9092
func (service *Router) Auth(w http.ResponseWriter, r *http.Request) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package users_router
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"net/url"
7+
"strings"
8+
"testing"
9+
10+
"golang.org/x/crypto/bcrypt"
11+
12+
mocket "github.com/selvatico/go-mocket"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestGetLogin(t *testing.T) {
17+
provider, _, _ := BuildTest()
18+
rec := httptest.NewRecorder()
19+
req, _ := http.NewRequest(http.MethodGet, "/login", nil)
20+
provider.Router.GetLogin(rec, req)
21+
22+
assert.Equal(t, http.StatusOK, rec.Code)
23+
}
24+
25+
func TestPostLogin(t *testing.T) {
26+
mocket.Catcher.Logging = true
27+
28+
tests := []struct {
29+
name string
30+
urlValues url.Values
31+
mock func()
32+
expectedCode int
33+
}{
34+
{
35+
name: "successful logged",
36+
urlValues: url.Values{"email": {"test_email"}, "password": {"test_password"}},
37+
mock: func() {
38+
ePassword, e := bcrypt.GenerateFromPassword([]byte("test_password"), 8)
39+
assert.Nil(t, e, "Password is correct")
40+
reply := []map[string]interface{}{{"id": 1, "email": "test_email", "encrypted_password": ePassword}}
41+
mocket.Catcher.Reset().NewMock().WithQuery(`WHERE (users.email = $1) LIMIT 1`).WithArgs("test_email").WithReply(reply)
42+
},
43+
expectedCode: http.StatusFound,
44+
},
45+
{
46+
name: "user does not exist",
47+
urlValues: url.Values{"email": {"test_email"}, "password": {"test_password"}},
48+
mock: func() {},
49+
expectedCode: http.StatusNotFound,
50+
},
51+
}
52+
53+
for _, test := range tests {
54+
t.Run(test.name, func(t *testing.T) {
55+
mocket.Catcher.Reset()
56+
test.mock()
57+
58+
provider, _, e := BuildTest()
59+
assert.Nil(t, e, "err should be nil")
60+
61+
rec := httptest.NewRecorder()
62+
req, _ := http.NewRequest(http.MethodPost, "/login", strings.NewReader(test.urlValues.Encode()))
63+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
64+
provider.Router.PostLogin(rec, req)
65+
66+
assert.Equal(t, test.expectedCode, rec.Code)
67+
})
68+
}
69+
}

0 commit comments

Comments
 (0)