github.com/cs3org/reva/v2@v2.27.7/internal/http/services/owncloud/ocdav/ocdav_whitebox_test.go (about)

     1  // Copyright 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  package ocdav
    19  
    20  import (
    21  	"errors"
    22  	"testing"
    23  
    24  	sprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    25  	"github.com/cs3org/reva/v2/pkg/storagespace"
    26  	"github.com/test-go/testify/require"
    27  )
    28  
    29  func TestWrapResourceID(t *testing.T) {
    30  	expected := "storageid" + "$" + "spaceid" + "!" + "opaqueid"
    31  	wrapped := storagespace.FormatResourceID(&sprovider.ResourceId{StorageId: "storageid", SpaceId: "spaceid", OpaqueId: "opaqueid"})
    32  
    33  	if wrapped != expected {
    34  		t.Errorf("wrapped id doesn't have the expected format: got %s expected %s", wrapped, expected)
    35  	}
    36  }
    37  
    38  func TestNameNotEmpty(t *testing.T) {
    39  	expErr := errors.New("must not be empty")
    40  	tests := map[string]error{
    41  		"":      expErr,
    42  		" ":     expErr,
    43  		"\n":    expErr,
    44  		"name":  nil,
    45  		"empty": nil,
    46  	}
    47  
    48  	for name, expected := range tests {
    49  		rule := notEmpty()
    50  		require.Equal(t, expected, rule(name), name)
    51  	}
    52  }
    53  
    54  func TestNameDoesNotContain(t *testing.T) {
    55  	tests := []struct {
    56  		excludedChars []string
    57  		tests         map[string]error
    58  	}{
    59  		{
    60  			[]string{"a"},
    61  			map[string]error{
    62  				"foo": nil,
    63  				"bar": errors.New("must not contain a"),
    64  			},
    65  		},
    66  		{
    67  			[]string{"a", "b"},
    68  			map[string]error{
    69  				"foo": nil,
    70  				"bar": errors.New("must not contain a"),
    71  				"car": errors.New("must not contain a"),
    72  				"bor": errors.New("must not contain b"),
    73  			},
    74  		},
    75  	}
    76  
    77  	for _, tt := range tests {
    78  		rule := doesNotContain(tt.excludedChars)
    79  		for name, expected := range tt.tests {
    80  			require.Equal(t, expected, rule(name), name)
    81  		}
    82  	}
    83  }
    84  
    85  func TestNameMaxLength(t *testing.T) {
    86  	name := "123456789"
    87  	tests := []struct {
    88  		MaxLength int
    89  		Error     error
    90  	}{
    91  		{12, nil},
    92  		{8, errors.New("must be shorter than 8")},
    93  		{4, errors.New("must be shorter than 4")},
    94  	}
    95  	for _, tt := range tests {
    96  		rule := isShorterThan(tt.MaxLength)
    97  		require.Equal(t, tt.Error, rule(name), tt.MaxLength)
    98  	}
    99  }