github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/templates/templates_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 templates 20 21 import ( 22 "testing" 23 24 userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" 25 ) 26 27 type testUnit struct { 28 expected string 29 template string 30 user *userpb.User 31 } 32 33 var tests = []*testUnit{ 34 { 35 expected: "alabasta", 36 user: &userpb.User{ 37 Username: "alabasta", 38 }, 39 template: "{{.Username}}", 40 }, 41 { 42 expected: "a/alabasta", 43 user: &userpb.User{ 44 Username: "alabasta", 45 }, 46 template: "{{substr 0 1 .Username}}/{{.Username}}", 47 }, 48 { 49 expected: "idp@opaque", 50 user: &userpb.User{ 51 Id: &userpb.UserId{ 52 Idp: "idp", 53 OpaqueId: "opaque", 54 }, 55 }, 56 template: "{{.Id.Idp}}@{{.Id.OpaqueId}}", 57 }, 58 { // test path clean 59 expected: "/alabasta", 60 user: &userpb.User{ 61 Username: "alabasta", 62 }, 63 template: "///{{.Username}}", 64 }, 65 { 66 expected: "michael", 67 user: &userpb.User{ 68 Username: "MICHAEL", 69 }, 70 template: "{{lower .Username}}", 71 }, 72 { 73 expected: "somewhere.com/michael@somewhere.com", 74 user: &userpb.User{ 75 Username: "michael@somewhere.com", 76 }, 77 template: "{{.Email.Domain}}/{{.Username}}", 78 }, 79 { 80 expected: "somewhere.com/michael", 81 user: &userpb.User{ 82 Username: "michael@somewhere.com", 83 }, 84 template: "{{.Email.Domain}}/{{.Email.Local}}", 85 }, 86 { 87 expected: "_unknown/michael", 88 user: &userpb.User{ 89 Username: "michael", 90 }, 91 template: "{{.Email.Domain}}/{{.Username}}", 92 }, 93 } 94 95 func TestLayout(t *testing.T) { 96 for _, u := range tests { 97 got := WithUser(u.user, u.template) 98 if u.expected != got { 99 t.Fatal("expected: " + u.expected + " got: " + got) 100 } 101 } 102 } 103 104 func TestLayoutPanic(t *testing.T) { 105 assertPanic(t, testBadLayout) 106 } 107 108 func TestUserPanic(t *testing.T) { 109 assertPanic(t, testBadUser) 110 } 111 112 // should panic 113 func testBadLayout() { 114 layout := "{{ bad layout syntax" 115 user := &userpb.User{} 116 WithUser(user, layout) 117 } 118 119 // should panic 120 func testBadUser() { 121 layout := "{{ .DoesNotExist }}" 122 user := &userpb.User{} 123 WithUser(user, layout) 124 } 125 126 func assertPanic(t *testing.T, f func()) { 127 defer func() { 128 if r := recover(); r == nil { 129 t.Errorf("the code did not panic") 130 } 131 }() 132 f() 133 }