Skip to content

Commit

Permalink
Refactor other packages (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostcar committed May 15, 2022
1 parent e23ba6c commit ab83689
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 175 deletions.
1 change: 1 addition & 0 deletions cmd/autoupdate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func run() error {

// Start http server.
listenAddr := ":" + env["AUTOUPDATE_PORT"]
fmt.Printf("Listen on %s\n", listenAddr)
return http.Run(ctx, listenAddr, authService, service)
}

Expand Down
7 changes: 3 additions & 4 deletions internal/autoupdate/autoupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/OpenSlides/openslides-autoupdate-service/internal/autoupdate"
"github.com/OpenSlides/openslides-autoupdate-service/internal/keysbuilder"
"github.com/OpenSlides/openslides-autoupdate-service/internal/test"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)

Expand All @@ -20,7 +19,7 @@ func TestSingleDataEmptyValues(t *testing.T) {
ds := dsmock.NewMockDatastore(dsmock.YAMLData(`---
user/1/organization_management_level: superadmin
`))
s := autoupdate.New(ds, test.RestrictAllowed)
s := autoupdate.New(ds, RestrictAllowed)

kb, err := keysbuilder.FromKeys("user/1/username")
if err != nil {
Expand All @@ -44,7 +43,7 @@ func TestHistoryInformation(t *testing.T) {
ds := dsmock.NewMockDatastore(dsmock.YAMLData(`---
user/1/organization_management_level: superadmin
`))
s := autoupdate.New(ds, test.RestrictAllowed)
s := autoupdate.New(ds, RestrictAllowed)

buf := new(bytes.Buffer)
err := s.HistoryInformation(ctx, 1, "collection/1", buf)
Expand All @@ -70,7 +69,7 @@ func TestHistoryInformationWrongFQID(t *testing.T) {
ds := dsmock.NewMockDatastore(dsmock.YAMLData(`---
user/1/organization_management_level: superadmin
`))
s := autoupdate.New(ds, test.RestrictAllowed)
s := autoupdate.New(ds, RestrictAllowed)

buf := new(bytes.Buffer)
err := s.HistoryInformation(ctx, 1, "collection", buf)
Expand Down
11 changes: 5 additions & 6 deletions internal/autoupdate/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/OpenSlides/openslides-autoupdate-service/internal/autoupdate"
"github.com/OpenSlides/openslides-autoupdate-service/internal/keysbuilder"
"github.com/OpenSlides/openslides-autoupdate-service/internal/test"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)
Expand Down Expand Up @@ -87,7 +86,7 @@ func TestConnectionEmptyData(t *testing.T) {
})
go ds.ListenOnUpdates(shutdownCtx, func(err error) { log.Println(err) })

s := autoupdate.New(ds, test.RestrictAllowed)
s := autoupdate.New(ds, RestrictAllowed)
kb, _ := keysbuilder.FromKeys(doesExistKey.String(), doesNotExistKey.String())

t.Run("First response", func(t *testing.T) {
Expand Down Expand Up @@ -228,7 +227,7 @@ func TestConntectionFilterOnlyOneKey(t *testing.T) {
})
go ds.ListenOnUpdates(shutdownCtx, func(err error) { log.Println(err) })

s := autoupdate.New(ds, test.RestrictAllowed)
s := autoupdate.New(ds, RestrictAllowed)
kb, _ := keysbuilder.FromKeys(userNameKey.String())
next := s.Connect(1, kb)
if _, err := next(context.Background()); err != nil {
Expand Down Expand Up @@ -259,7 +258,7 @@ func TestNextNoReturnWhenDataIsRestricted(t *testing.T) {
userNameKey: []byte(`"Hello World"`),
})

s := autoupdate.New(ds, test.RestrictNotAllowed)
s := autoupdate.New(ds, RestrictNotAllowed)
kb, _ := keysbuilder.FromKeys(userNameKey.String())

next := s.Connect(1, kb)
Expand Down Expand Up @@ -331,7 +330,7 @@ func TestKeyNotRequestedAnymore(t *testing.T) {
`))
go datastore.ListenOnUpdates(shutdownCtx, nil)

s := autoupdate.New(datastore, test.RestrictAllowed)
s := autoupdate.New(datastore, RestrictAllowed)
kb, err := keysbuilder.FromJSON(strings.NewReader(`{
"collection":"organization",
"ids":[
Expand Down Expand Up @@ -398,7 +397,7 @@ func TestKeyRequestedAgain(t *testing.T) {
`))
go datastore.ListenOnUpdates(shutdownCtx, nil)

s := autoupdate.New(datastore, test.RestrictAllowed)
s := autoupdate.New(datastore, RestrictAllowed)
kb, err := keysbuilder.FromJSON(strings.NewReader(`{
"collection":"organization",
"ids":[
Expand Down
3 changes: 1 addition & 2 deletions internal/autoupdate/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/OpenSlides/openslides-autoupdate-service/internal/autoupdate"
"github.com/OpenSlides/openslides-autoupdate-service/internal/keysbuilder"
"github.com/OpenSlides/openslides-autoupdate-service/internal/test"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)

Expand Down Expand Up @@ -78,7 +77,7 @@ gb/1:

func TestFeatures(t *testing.T) {
datastore := dsmock.NewMockDatastore(dsmock.YAMLData(dataSet))
service := autoupdate.New(datastore, test.RestrictAllowed)
service := autoupdate.New(datastore, RestrictAllowed)

for _, tt := range []struct {
name string
Expand Down
36 changes: 34 additions & 2 deletions internal/autoupdate/mock_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package autoupdate_test

import (
"context"
"fmt"
"time"

"github.com/OpenSlides/openslides-autoupdate-service/internal/autoupdate"
"github.com/OpenSlides/openslides-autoupdate-service/internal/keysbuilder"
"github.com/OpenSlides/openslides-autoupdate-service/internal/test"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
"github.com/OpenSlides/openslides-autoupdate-service/pkg/dsmock"
)
Expand All @@ -14,7 +15,7 @@ func getConnection() (autoupdate.DataProvider, *dsmock.MockDatastore) {
datastore := dsmock.NewMockDatastore(map[datastore.Key][]byte{
userNameKey: []byte(`"Hello World"`),
})
s := autoupdate.New(datastore, test.RestrictAllowed)
s := autoupdate.New(datastore, RestrictAllowed)
kb, _ := keysbuilder.FromKeys(userNameKey.String())
next := s.Connect(1, kb)

Expand All @@ -41,3 +42,34 @@ func blockingTime(wait time.Duration, f func()) bool {
return true
}
}

// RestrictAllowed is a restricter that allows everything
func RestrictAllowed(getter datastore.Getter, uid int) datastore.Getter {
return mockRestricter{getter, true}
}

// RestrictNotAllowed is a restricter that removes everythin
func RestrictNotAllowed(getter datastore.Getter, uid int) datastore.Getter {
return mockRestricter{getter, false}
}

type mockRestricter struct {
getter datastore.Getter
allow bool
}

func (r mockRestricter) Get(ctx context.Context, keys ...datastore.Key) (map[datastore.Key][]byte, error) {
data, err := r.getter.Get(ctx, keys...)
if err != nil {
return nil, fmt.Errorf("getting data: %w", err)
}

if r.allow {
return data, nil
}

for k := range data {
data[k] = nil
}
return data, nil
}
6 changes: 5 additions & 1 deletion internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ func Run(ctx context.Context, addr string, auth Authenticater, autoupdate *autou
go func() {
<-ctx.Done()
if err := srv.Shutdown(context.Background()); err != nil {
// TODO EXTERNAL ERROR
wait <- fmt.Errorf("HTTP server shutdown: %w", err)
return
}
wait <- nil
}()

fmt.Printf("Listen on %s\n", addr)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// TODO EXTERNAL ERROR
return fmt.Errorf("HTTP Server failed: %v", err)
}

Expand Down Expand Up @@ -85,6 +86,7 @@ func Autoupdate(mux *http.ServeMux, auth Authenticater, connecter Connecter, cou

body, err := io.ReadAll(r.Body)
if err != nil {
// TODO EXTERNAL ERROR
handleError(w, fmt.Errorf("reading body: %w", err), true)
return
}
Expand Down Expand Up @@ -121,6 +123,7 @@ func Autoupdate(mux *http.ServeMux, auth Authenticater, connecter Connecter, cou
}

if err := json.NewEncoder(w).Encode(converted); err != nil {
// TODO EXTERNAL ERROR
handleError(w, fmt.Errorf("encoding end sending next message: %w", err), true)
return
}
Expand Down Expand Up @@ -192,6 +195,7 @@ func sendMessages(ctx context.Context, w io.Writer, uid int, kb autoupdate.KeysB
}

if err := encoder.Encode(converted); err != nil {
// TODO EXTERNAL ERROR
return fmt.Errorf("encoding and sending next message: %w", err)
}

Expand Down
12 changes: 2 additions & 10 deletions internal/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,9 @@ import (
"github.com/OpenSlides/openslides-autoupdate-service/pkg/datastore"
)

func MustKey(in string) datastore.Key {
k, err := datastore.KeyFromString(in)
if err != nil {
panic(err)
}
return k
}

var (
myKey1 = MustKey("collection/1/field")
myKey2 = MustKey("collection/2/field")
myKey1 = datastore.Key{Collection: "collection", ID: 1, Field: "field"}
myKey2 = datastore.Key{Collection: "collection", ID: 2, Field: "field"}
)

type connecterMock struct {
Expand Down
1 change: 1 addition & 0 deletions internal/keysbuilder/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (b *body) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &field); err != nil {
return err
}

if len(field.IDs) == 0 {
return InvalidError{msg: "no ids"}
}
Expand Down
23 changes: 0 additions & 23 deletions internal/keysbuilder/simple.go

This file was deleted.

0 comments on commit ab83689

Please sign in to comment.