-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
152 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ | |
.config/genenv.local.sh | ||
|
||
node_modules/ | ||
|
||
# gomock | ||
*_mock_test.go |
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
default = pkgs.mkShell { | ||
buildInputs = with pkgs; [ | ||
go | ||
mockgen | ||
gopls | ||
golangci-lint | ||
wire | ||
|
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,7 @@ | ||
package users | ||
|
||
type CreateCmd struct { | ||
UserName UserName | ||
Email Email | ||
Password Password | ||
} |
This file was deleted.
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
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,72 @@ | ||
package users | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/prismelabs/prismeanalytics/internal/secret" | ||
"github.com/prismelabs/prismeanalytics/internal/testutils" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestService(t *testing.T) { | ||
t.Run("CreateUser", func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
t.Run("UserAlreadyExists", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
store := NewMockStore(ctrl) | ||
|
||
service := ProvideService(store) | ||
|
||
username := testutils.Must(NewUserName)("foo") | ||
email := testutils.Must(NewEmail)("foo@example.com") | ||
|
||
store.EXPECT().InsertUser( | ||
ctx, | ||
gomock.Any(), // user id | ||
username, | ||
email, | ||
gomock.Any(), // password | ||
).Return(ErrUserAlreadyExists) | ||
|
||
userId, err := service.CreateUser(ctx, CreateCmd{ | ||
UserName: username, | ||
Email: email, | ||
Password: testutils.Must(NewPassword)(secret.New("p4ssW0rd!")), | ||
}) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, ErrUserAlreadyExists) | ||
require.Equal(t, UserId{}, userId) | ||
}) | ||
|
||
t.Run("EmailNotUsed", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
store := NewMockStore(ctrl) | ||
|
||
service := ProvideService(store) | ||
|
||
username := testutils.Must(NewUserName)("foo") | ||
email := testutils.Must(NewEmail)("foo@example.com") | ||
|
||
store.EXPECT().InsertUser( | ||
ctx, | ||
gomock.Any(), // user id | ||
username, | ||
email, | ||
gomock.Any(), // password | ||
).Return(nil) | ||
|
||
userId, err := service.CreateUser(ctx, CreateCmd{ | ||
UserName: username, | ||
Email: email, | ||
Password: testutils.Must(NewPassword)(secret.New("p4ssW0rd!")), | ||
}) | ||
require.NoError(t, err) | ||
require.NotEqual(t, UserId{}, userId) | ||
}) | ||
}) | ||
} |
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,11 @@ | ||
package users | ||
|
||
import "time" | ||
|
||
type User struct { | ||
Id UserId | ||
Email Email | ||
Password Password | ||
Name UserName | ||
CreatedAt time.Time | ||
} |
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,12 @@ | ||
package testutils | ||
|
||
func Must[I, R any](fn func(I) (R, error)) func(I) R { | ||
return func(value I) R { | ||
result, err := fn(value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return result | ||
} | ||
} |