Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"testing"
"time"

"github.com/stretchr/testify/suite"

"github.com/brave/go-sync/auth"
"github.com/brave/go-sync/auth/authtest"
"github.com/stretchr/testify/suite"
)

type AuthTestSuite struct {
Expand All @@ -20,14 +21,14 @@ func (suite *AuthTestSuite) TestAuthenticate() {
// invalid token format
id, err := auth.Authenticate(base64.URLEncoding.EncodeToString([]byte("||")))
suite.Require().Error(err, "invalid token format should fail")
suite.Require().Equal("", id, "empty clientID should be returned")
suite.Require().Empty(id, "empty clientID should be returned")

// invalid signature
_, tokenHex, _, err := authtest.GenerateToken(time.Now().UnixMilli())
suite.Require().NoError(err, "generate token should succeed")
id, err = auth.Authenticate(base64.URLEncoding.EncodeToString([]byte("12" + tokenHex)))
suite.Require().Error(err, "invalid signature should fail")
suite.Require().Equal("", id)
suite.Require().Empty(id)

// valid token
tkn, _, expectedID, err := authtest.GenerateToken(time.Now().UnixMilli())
Expand All @@ -41,13 +42,13 @@ func (suite *AuthTestSuite) TestAuthenticate() {
suite.Require().NoError(err, "generate token should succeed")
id, err = auth.Authenticate(tkn)
suite.Require().Error(err, "outdated token should failed")
suite.Require().Equal("", id)
suite.Require().Empty(id)

tkn, _, _, err = authtest.GenerateToken(time.Now().UnixMilli() + auth.TokenMaxDuration + 100)
suite.Require().NoError(err, "generate token should succeed")
id, err = auth.Authenticate(tkn)
suite.Require().Error(err, "outdated token should failed")
suite.Require().Equal("", id)
suite.Require().Empty(id)
}

func (suite *AuthTestSuite) TestAuthorize() {
Expand Down
5 changes: 3 additions & 2 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"testing"

"github.com/brave/go-sync/cache"
"github.com/stretchr/testify/suite"

"github.com/brave/go-sync/cache"
)

type CacheTestSuite struct {
Expand All @@ -21,7 +22,7 @@ func (suite *CacheTestSuite) TestSetTypeMtime() {
suite.cache.SetTypeMtime(context.Background(), "id", 123, 12345678)
val, err := suite.cache.Get(context.Background(), "id#123", false)
suite.Require().NoError(err)
suite.Require().Equal(val, "12345678")
suite.Require().Equal("12345678", val)
}

func (suite *CacheTestSuite) TestIsTypeMtimeUpdated() {
Expand Down
3 changes: 2 additions & 1 deletion command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/rs/zerolog/log"

"github.com/brave/go-sync/cache"
"github.com/brave/go-sync/datastore"
"github.com/brave/go-sync/schema/protobuf/sync_pb"
"github.com/rs/zerolog/log"
)

var (
Expand Down
141 changes: 71 additions & 70 deletions command/command_test.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions command/server_defined_unique_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/google/uuid"

"github.com/brave/go-sync/datastore"
"github.com/brave/go-sync/schema/protobuf/sync_pb"
"github.com/satori/go.uuid"
)

const (
Expand All @@ -28,7 +29,7 @@ func createServerDefinedUniqueEntity(name string, serverDefinedTag string, clien
deleted := false
folder := true
version := int64(1)
idString := uuid.NewV4().String()
idString := uuid.New().String()

pbEntity := &sync_pb.SyncEntity{
Ctime: &now, Mtime: &now, Deleted: &deleted, Folder: &folder,
Expand Down
19 changes: 10 additions & 9 deletions command/server_defined_unique_entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/stretchr/testify/suite"

"github.com/brave/go-sync/command"
"github.com/brave/go-sync/datastore"
"github.com/brave/go-sync/datastore/datastoretest"
"github.com/stretchr/testify/suite"
)

type ServerDefinedUniqueEntityTestSuite struct {
Expand Down Expand Up @@ -107,17 +108,17 @@ func (suite *ServerDefinedUniqueEntityTestSuite) TestInsertServerDefinedUniqueEn

// Check that Ctime and Mtime have been set, reset to zero value for subsequent
// tests
for i := 0; i < len(tagItems); i++ {
suite.Assert().NotNil(tagItems[i].Ctime)
suite.Assert().NotNil(tagItems[i].Mtime)
for i := range tagItems {
suite.NotNil(tagItems[i].Ctime)
suite.NotNil(tagItems[i].Mtime)

tagItems[i].Ctime = nil
tagItems[i].Mtime = nil
}

sort.Sort(datastore.TagItemByClientIDID(tagItems))
sort.Sort(datastore.TagItemByClientIDID(expectedTagItems))
suite.Assert().Equal(tagItems, expectedTagItems)
suite.Equal(expectedTagItems, tagItems)

syncItems, err := datastoretest.ScanSyncEntities(suite.dynamo)
suite.Require().NoError(err, "ScanSyncEntities should succeed")
Expand All @@ -133,7 +134,7 @@ func (suite *ServerDefinedUniqueEntityTestSuite) TestInsertServerDefinedUniqueEn
break
}
}
suite.Assert().NotEqual(bookmarksRootID, "", "Cannot find ID of bookmarks root folder")
suite.NotEmpty(bookmarksRootID, "Cannot find ID of bookmarks root folder")

// For each item returned by ScanSyncEntities, make sure it is in the map and
// its value is matched, then remove it from the map.
Expand All @@ -148,11 +149,11 @@ func (suite *ServerDefinedUniqueEntityTestSuite) TestInsertServerDefinedUniqueEn
Folder: item.Folder,
}

suite.Assert().NotNil(item.ServerDefinedUniqueTag)
suite.Assert().Equal(syncAttrs, *expectedSyncAttrsMap[*item.ServerDefinedUniqueTag])
suite.NotNil(item.ServerDefinedUniqueTag)
suite.Equal(syncAttrs, *expectedSyncAttrsMap[*item.ServerDefinedUniqueTag])
delete(expectedSyncAttrsMap, *item.ServerDefinedUniqueTag)
}
suite.Assert().Equal(0, len(expectedSyncAttrsMap))
suite.Empty(expectedSyncAttrsMap)

suite.Require().NoError(
command.InsertServerDefinedUniqueEntities(suite.dynamo, "client2"),
Expand Down
10 changes: 5 additions & 5 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package controller
import (
"compress/gzip"
"io"
"io/ioutil"
"net/http"

"github.com/brave-intl/bat-go/libs/closers"
"github.com/brave-intl/bat-go/libs/middleware"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/proto"

"github.com/brave/go-sync/cache"
"github.com/brave/go-sync/command"
syncContext "github.com/brave/go-sync/context"
"github.com/brave/go-sync/datastore"
syncMiddleware "github.com/brave/go-sync/middleware"
"github.com/brave/go-sync/schema/protobuf/sync_pb"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/proto"
)

const (
Expand Down Expand Up @@ -55,7 +55,7 @@ func Command(cache *cache.Cache, db datastore.Datastore) http.HandlerFunc {
reader = gr
}

msg, err := ioutil.ReadAll(io.LimitReader(reader, payloadLimit10MB))
msg, err := io.ReadAll(io.LimitReader(reader, payloadLimit10MB))
if err != nil {
log.Error().Err(err).Msg("Read request body failed")
http.Error(w, "Read request body error", http.StatusInternalServerError)
Expand Down
5 changes: 3 additions & 2 deletions controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/stretchr/testify/suite"
"google.golang.org/protobuf/proto"

"github.com/brave/go-sync/auth/authtest"
"github.com/brave/go-sync/cache"
syncContext "github.com/brave/go-sync/context"
"github.com/brave/go-sync/controller"
"github.com/brave/go-sync/datastore"
"github.com/brave/go-sync/datastore/datastoretest"
"github.com/brave/go-sync/schema/protobuf/sync_pb"
"github.com/stretchr/testify/suite"
"google.golang.org/protobuf/proto"
)

type ControllerTestSuite struct {
Expand Down
5 changes: 3 additions & 2 deletions datastore/datastoretest/dynamo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"time"
Expand All @@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"

"github.com/brave/go-sync/datastore"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func DeleteTable(dynamo *datastore.Dynamo) error {
func CreateTable(dynamo *datastore.Dynamo) error {
_, b, _, _ := runtime.Caller(0)
root := filepath.Join(filepath.Dir(b), "../../")
raw, err := ioutil.ReadFile(filepath.Join(root, "schema/dynamodb/table.json"))
raw, err := os.ReadFile(filepath.Join(root, "schema/dynamodb/table.json"))
if err != nil {
return fmt.Errorf("error reading table.json: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion datastore/datastoretest/mock_datastore.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package datastoretest

import (
"github.com/brave/go-sync/datastore"
"github.com/stretchr/testify/mock"

"github.com/brave/go-sync/datastore"
)

// MockDatastore is used to mock datastorein tests
Expand Down
2 changes: 1 addition & 1 deletion datastore/item_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (dynamo *Dynamo) initRealCountsAndUpdateHistoryCounts(counts *ClientItemCou
timeSinceLastChange := now - counts.LastPeriodChangeTime
if timeSinceLastChange >= periodDurationSecs {
changeCount := int(timeSinceLastChange / periodDurationSecs)
for i := 0; i < changeCount; i++ {
for range changeCount {
// The records from "period 1"/the earliest period
// will be purged from the count, since they will be deleted via DDB TTL
counts.HistoryItemCountPeriod1 = counts.HistoryItemCountPeriod2
Expand Down
9 changes: 5 additions & 4 deletions datastore/item_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"sort"
"testing"

"github.com/stretchr/testify/suite"

"github.com/brave/go-sync/datastore"
"github.com/brave/go-sync/datastore/datastoretest"
"github.com/stretchr/testify/suite"
)

type ItemCountTestSuite struct {
Expand Down Expand Up @@ -46,13 +47,13 @@ func (suite *ItemCountTestSuite) TestGetClientItemCount() {
for _, item := range items {
count, err := suite.dynamo.GetClientItemCount(item.ClientID)
suite.Require().NoError(err, "GetClientItemCount should succeed")
suite.Assert().Equal(count.ItemCount, item.ItemCount, "ItemCount should match")
suite.Equal(count.ItemCount, item.ItemCount, "ItemCount should match")
}

// Non-exist client item count should succeed with count = 0.
count, err := suite.dynamo.GetClientItemCount("client3")
suite.Require().NoError(err, "Get non-exist ClientItemCount should succeed")
suite.Assert().Equal(count.ItemCount, 0)
suite.Equal(0, count.ItemCount)
}

func (suite *ItemCountTestSuite) TestUpdateClientItemCount() {
Expand Down Expand Up @@ -81,7 +82,7 @@ func (suite *ItemCountTestSuite) TestUpdateClientItemCount() {
clientCountItems[i].Version = 0
clientCountItems[i].LastPeriodChangeTime = 0
}
suite.Assert().Equal(expectedItems, clientCountItems)
suite.Equal(expectedItems, clientCountItems)
}

func TestItemCountTestSuite(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions datastore/sync_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/brave/go-sync/schema/protobuf/sync_pb"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
uuid "github.com/satori/go.uuid"
"google.golang.org/protobuf/proto"

"github.com/brave/go-sync/schema/protobuf/sync_pb"
)

const (
Expand Down Expand Up @@ -789,7 +790,7 @@ func CreateDBSyncEntity(entity *sync_pb.SyncEntity, cacheGUID *string, clientID
var originatorCacheGUID, originatorClientItemID *string
if cacheGUID != nil {
if *entity.Version == 0 {
id = uuid.NewV4().String()
id = uuid.New().String()
}
originatorCacheGUID = cacheGUID
originatorClientItemID = entity.IdString
Expand Down
Loading