github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/appprovider/appprovider_test.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package appprovider
    20  
    21  import (
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/mitchellh/mapstructure"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func Test_parseConfig(t *testing.T) {
    30  
    31  	tests := []struct {
    32  		name    string
    33  		m       map[string]interface{}
    34  		want    *config
    35  		wantErr interface{}
    36  	}{
    37  		{
    38  			name: "all configurations set for demo driver",
    39  			m: map[string]interface{}{
    40  				"Driver":  "demo",
    41  				"Drivers": map[string]map[string]interface{}{"demo": {"a": "b", "c": "d"}},
    42  			},
    43  			want: &config{
    44  				Driver:      "demo",
    45  				Drivers:     map[string]map[string]interface{}{"demo": {"a": "b", "c": "d"}},
    46  				RefreshTime: 20 * time.Second,
    47  			},
    48  			wantErr: nil,
    49  		},
    50  		{
    51  			name: "all configurations set for wopi driver",
    52  			m: map[string]interface{}{
    53  				"Driver":      "wopi",
    54  				"Drivers":     map[string]map[string]interface{}{"wopi": {"iop_secret": "very-secret", "wopi_url": "https://my.wopi:9871"}},
    55  				"RefreshTime": 10 * time.Second,
    56  			},
    57  			want: &config{
    58  				Driver:      "wopi",
    59  				Drivers:     map[string]map[string]interface{}{"wopi": {"iop_secret": "very-secret", "wopi_url": "https://my.wopi:9871"}},
    60  				RefreshTime: 10 * time.Second,
    61  			},
    62  			wantErr: nil,
    63  		},
    64  		{
    65  			name: "wrong type of setting",
    66  			m:    map[string]interface{}{"Driver": 123, "NonExistentField": 456},
    67  			want: nil,
    68  			wantErr: &mapstructure.Error{
    69  				Errors: []string{
    70  					"'driver' expected type 'string', got unconvertible type 'int', value: '123'",
    71  				},
    72  			},
    73  		},
    74  		{
    75  			name: "undefined settings type",
    76  			m:    map[string]interface{}{"Not-Defined": 123},
    77  			want: &config{
    78  				Driver:      "demo",
    79  				Drivers:     map[string]map[string]interface{}(nil),
    80  				RefreshTime: 20 * time.Second,
    81  			},
    82  			wantErr: nil,
    83  		},
    84  	}
    85  
    86  	for _, tt := range tests {
    87  		t.Run(tt.name, func(t *testing.T) {
    88  			got, err := parseConfig(tt.m)
    89  			assert.Equal(t, tt.wantErr, err)
    90  			assert.Equal(t, tt.want, got)
    91  		})
    92  	}
    93  }