From 5c2ae014a640c439e6690b176548ea308aeb488a Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Thu, 23 Jul 2020 10:39:16 +0545 Subject: [PATCH] unit tests for parseConfig of appprovider --- .golangci.yaml | 4 + go.mod | 1 + .../services/appprovider/appprovider_test.go | 89 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 internal/grpc/services/appprovider/appprovider_test.go diff --git a/.golangci.yaml b/.golangci.yaml index 6d59182f54..019e7d5a9a 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -12,6 +12,10 @@ issues: text: "SA1019:" linters: - staticcheck + # Exclude scopelint for tests files because of https://github.com/kyoh86/scopelint/issues/4 + - path: _test\.go + linters: + - scopelint linters: enable: - bodyclose diff --git a/go.mod b/go.mod index 685157c031..9f50217ada 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect github.com/rs/cors v1.7.0 github.com/rs/zerolog v1.19.0 + github.com/stretchr/testify v1.5.1 github.com/tus/tusd v1.1.1-0.20200416115059-9deabf9d80c2 go.opencensus.io v0.22.4 golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 diff --git a/internal/grpc/services/appprovider/appprovider_test.go b/internal/grpc/services/appprovider/appprovider_test.go new file mode 100644 index 0000000000..f36cf3d383 --- /dev/null +++ b/internal/grpc/services/appprovider/appprovider_test.go @@ -0,0 +1,89 @@ +// Copyright 2018-2020 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package appprovider + +import ( + "testing" + + "github.com/mitchellh/mapstructure" + "github.com/stretchr/testify/assert" +) + +func Test_parseConfig(t *testing.T) { + + tests := []struct { + name string + m map[string]interface{} + want *config + wantErr interface{} + }{ + { + name: "all configurations set", + m: map[string]interface{}{ + "Driver": "demo", + "Demo": map[string]interface{}{"a": "b", "c": "d"}, + "IopSecret": "very-secret", + "WopiURL": "https://my.wopi:9871", + "UIURL": "https://the.ui", + "StorageID": "123-455", + }, + want: &config{ + Driver: "demo", + Demo: map[string]interface{}{"a": "b", "c": "d"}, + IopSecret: "very-secret", + WopiURL: "https://my.wopi:9871", + UIURL: "", // this field seems not to get parsed see https://github.com/cs3org/reva/issues/991 + StorageID: "123-455", + }, + wantErr: nil, + }, + { + name: "wrong type of setting", + m: map[string]interface{}{"Driver": 123, "IopSecret": 456}, + want: nil, + wantErr: &mapstructure.Error{ + Errors: []string{ + "'driver' expected type 'string', got unconvertible type 'int'", + "'iopsecret' expected type 'string', got unconvertible type 'int'", + }, + }, + }, + { + name: "undefined settings type", + m: map[string]interface{}{"Not-Defined": 123}, + want: &config{ + Driver: "", + Demo: map[string]interface{}(nil), + IopSecret: "", + WopiURL: "", + UIURL: "", + StorageID: "", + }, + wantErr: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parseConfig(tt.m) + assert.Equal(t, tt.wantErr, err) + assert.Equal(t, tt.want, got) + }) + } +}