// WithIdentity fills the context with token // Token is filled using input Identity object func WithIdentity(ctx context.Context, ident account.Identity) context.Context { token := jwt.New(jwt.SigningMethodRS256) token.Claims.(jwt.MapClaims)["sub"] = ident.ID.String() token.Claims.(jwt.MapClaims)["uuid"] = ident.ID.String() token.Claims.(jwt.MapClaims)["fullName"] = ident.User.FullName token.Claims.(jwt.MapClaims)["imageURL"] = ident.User.ImageURL return goajwt.WithJWT(ctx, token) }
func TestCurrentAuthorizedMissingUUID(t *testing.T) { t.Parallel() resource.Require(t, resource.UnitTest) jwtToken := token.New(token.SigningMethodRS256) ctx := jwt.WithJWT(context.Background(), jwtToken) controller := newUserController(nil, nil) test.ShowUserBadRequest(t, ctx, nil, controller) }
func TestCurrentAuthorizedMissingIdentity(t *testing.T) { t.Parallel() resource.Require(t, resource.UnitTest) jwtToken := token.New(token.SigningMethodRS256) jwtToken.Claims.(token.MapClaims)["sub"] = uuid.NewV4().String() ctx := jwt.WithJWT(context.Background(), jwtToken) controller := newUserController(nil, nil) test.ShowUserUnauthorized(t, ctx, nil, controller) }
func TestLocateMissingUUIDInTokenInContext(t *testing.T) { tk := jwt.New(jwt.SigningMethodRS256) ctx := goajwt.WithJWT(context.Background(), tk) manager := createManager(t) _, err := manager.Locate(ctx) if err == nil { t.Error("Should have returned error on missing token in contex", err) } }
func TestLocateTokenInContex(t *testing.T) { id := uuid.NewV4() tk := jwt.New(jwt.SigningMethodRS256) tk.Claims.(jwt.MapClaims)["sub"] = id.String() ctx := goajwt.WithJWT(context.Background(), tk) manager := createManager(t) foundId, err := manager.Locate(ctx) if err != nil { t.Error("Failed not locate token in given context", err) } assert.Equal(t, id, foundId, "ID in created context not equal") }
func TestCurrentAuthorizedOK(t *testing.T) { t.Parallel() resource.Require(t, resource.UnitTest) jwtToken := token.New(token.SigningMethodRS256) jwtToken.Claims.(token.MapClaims)["sub"] = uuid.NewV4().String() ctx := jwt.WithJWT(context.Background(), jwtToken) usr := account.User{FullName: "Test User", ImageURL: "someURL", Email: "*****@*****.**", ID: uuid.NewV4()} ident := account.Identity{ID: uuid.NewV4(), Username: "******", Provider: account.KeycloakIDP, User: usr, UserID: account.NullUUID{UUID: usr.ID, Valid: true}} controller := newUserController(&ident, &usr) _, identity := test.ShowUserOK(t, ctx, nil, controller) assert.NotNil(t, identity) assert.Equal(t, usr.FullName, *identity.Data.Attributes.FullName) assert.Equal(t, ident.Username, *identity.Data.Attributes.Username) assert.Equal(t, usr.ImageURL, *identity.Data.Attributes.ImageURL) assert.Equal(t, usr.Email, *identity.Data.Attributes.Email) assert.Equal(t, ident.Provider, *identity.Data.Attributes.Provider) }