Skip to content

Commit

Permalink
added functionality for clients to create and switch between users
Browse files Browse the repository at this point in the history
  • Loading branch information
brian030128 committed May 21, 2024
1 parent 17f6bee commit b0c340d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
8 changes: 2 additions & 6 deletions internal/test/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ package test
import (
"context"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
monify "monify/protobuf"
"testing"
)

func TestCreateGroup(t *testing.T) {
client := GetTestClient(t)
_, accessToken := client.CreateTestUser()
md := metadata.Pairs("authorization", "Bearer "+accessToken)
ctx := metadata.NewOutgoingContext(context.Background(), md)

group, err := client.CreateGroup(ctx, &monify.CreateGroupRequest{Name: "test"})
_ = client.CreateTestUser()
group, err := client.CreateGroup(context.TODO(), &monify.CreateGroupRequest{Name: "test"})
assert.NoError(t, err)
assert.NotEmpty(t, group.GroupId)
}
22 changes: 19 additions & 3 deletions internal/test/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/joho/godotenv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/test/bufconn"
"log"
"monify/internal"
Expand Down Expand Up @@ -88,6 +89,10 @@ func createClient(lis *bufconn.Listener) Client {
return lis.Dial()
}),
grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req any, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
if currentUser != "" {
md := metadata.Pairs("authorization", "Bearer "+users[currentUser])
ctx = metadata.NewOutgoingContext(context.Background(), md)
}
return invoker(ctx, method, req, reply, cc, opts...)
}),
)
Expand All @@ -104,13 +109,24 @@ func createClient(lis *bufconn.Listener) Client {
}
}

// CreateTestUser creates a new user and returns the userId and accessToken
func (c Client) CreateTestUser() (string, string) {
// CreateTestUser creates a new user and returns the userId
// and sets current user as the newly created user
func (c Client) CreateTestUser() string {
email := uuid.New().String() + "@gmail.com"
_, err := c.EmailRegister(context.TODO(), &monify.EmailRegisterRequest{Email: email, Password: "12345678"})
if err != nil {
log.Fatalf("error creating user: %v", err)
}
res, err := c.EmailLogin(context.TODO(), &monify.EmailLoginRequest{Email: email, Password: "12345678"})
return res.UserId, res.AccessToken
*c.currentUser = res.UserId
(*c.users)[res.UserId] = res.AccessToken
return res.UserId
}

// SetTestUser sets the current user to the user with the given userId
func (c Client) SetTestUser(userId string) {
if _, ok := (*c.users)[userId]; !ok {
log.Fatalf("user %s not found", userId)
}
*c.currentUser = userId
}

0 comments on commit b0c340d

Please sign in to comment.