github.com/cs3org/reva/v2@v2.27.7/internal/http/services/ocmd/protocols_test.go (about)

     1  // Copyright 2018-2023 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 ocmd
    20  
    21  import (
    22  	"encoding/json"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/gdexlab/go-render/render"
    27  )
    28  
    29  func TestUnmarshalProtocol(t *testing.T) {
    30  	tests := []struct {
    31  		raw      string
    32  		expected Protocols
    33  		err      string
    34  	}{
    35  		{
    36  			raw:      "{}",
    37  			expected: []Protocol{},
    38  		},
    39  		{
    40  			raw:      `{"name":"foo","options":{ }}`,
    41  			expected: []Protocol{},
    42  		},
    43  		{
    44  			raw: `{"name":"foo","options":{"unsupported":"value"}}`,
    45  			err: `protocol options not supported: {"unsupported":"value"}`,
    46  		},
    47  		{
    48  			raw: `{"unsupported":{}}`,
    49  			err: "protocol unsupported not recognised",
    50  		},
    51  		{
    52  			raw: `{"name":"multi","options":{},"webdav":{"sharedSecret":"secret","permissions":["read","write"],"url":"http://example.org"}}`,
    53  			expected: []Protocol{
    54  				&WebDAV{
    55  					SharedSecret: "secret",
    56  					Permissions:  []string{"read", "write"},
    57  					URL:          "http://example.org",
    58  				},
    59  			},
    60  		},
    61  		{
    62  			raw: `{"name":"multi","options":{},"webapp":{"uriTemplate":"http://example.org/{test}"}}`,
    63  			expected: []Protocol{
    64  				&Webapp{
    65  					URITemplate: "http://example.org/{test}",
    66  				},
    67  			},
    68  		},
    69  		{
    70  			raw: `{"name":"multi","options":{},"datatx":{"sharedSecret":"secret","srcUri":"http://example.org","size":10}}`,
    71  			expected: []Protocol{
    72  				&Datatx{
    73  					SharedSecret: "secret",
    74  					SourceURI:    "http://example.org",
    75  					Size:         10,
    76  				},
    77  			},
    78  		},
    79  		{
    80  			raw: `{"name":"multi","options":{},"webdav":{"sharedSecret":"secret","permissions":["read","write"],"url":"http://example.org"},"webapp":{"uriTemplate":"http://example.org/{test}"},"datatx":{"sharedSecret":"secret","srcUri":"http://example.org","size":10}}`,
    81  			expected: []Protocol{
    82  				&WebDAV{
    83  					SharedSecret: "secret",
    84  					Permissions:  []string{"read", "write"},
    85  					URL:          "http://example.org",
    86  				},
    87  				&Webapp{
    88  					URITemplate: "http://example.org/{test}",
    89  				},
    90  				&Datatx{
    91  					SharedSecret: "secret",
    92  					SourceURI:    "http://example.org",
    93  					Size:         10,
    94  				},
    95  			},
    96  		},
    97  	}
    98  
    99  	for _, tt := range tests {
   100  		var got Protocols
   101  		err := json.Unmarshal([]byte(tt.raw), &got)
   102  		if err != nil && err.Error() != tt.err {
   103  			t.Fatalf("not expected error. got=%+v expected=%+v", err, tt.err)
   104  		}
   105  
   106  		if tt.err == "" {
   107  			if !protocolsEqual(got, tt.expected) {
   108  				t.Fatalf("result does not match with expected. got=%+v expected=%+v", render.AsCode(got), render.AsCode(tt.expected))
   109  			}
   110  		}
   111  	}
   112  }
   113  
   114  func protocolsToMap(p Protocols) map[string]Protocol {
   115  	m := make(map[string]Protocol)
   116  	for _, prot := range p {
   117  		switch prot.(type) {
   118  		case *WebDAV:
   119  			m["webdav"] = prot
   120  		case *Webapp:
   121  			m["webapp"] = prot
   122  		case *Datatx:
   123  			m["datatx"] = prot
   124  		}
   125  	}
   126  	return m
   127  }
   128  
   129  func protocolsEqual(p1, p2 Protocols) bool {
   130  	return reflect.DeepEqual(protocolsToMap(p1), protocolsToMap(p2))
   131  }
   132  
   133  func TestMarshalProtocol(t *testing.T) {
   134  	tests := []struct {
   135  		in       Protocols
   136  		expected map[string]any
   137  		err      string
   138  	}{
   139  		{
   140  			in:  []Protocol{},
   141  			err: "json: error calling MarshalJSON for type ocmd.Protocols: no protocol defined",
   142  		},
   143  		{
   144  			in: []Protocol{
   145  				&WebDAV{
   146  					SharedSecret: "secret",
   147  					Permissions:  []string{"read"},
   148  					URL:          "http://example.org",
   149  				},
   150  			},
   151  			expected: map[string]any{
   152  				"name":    "multi",
   153  				"options": map[string]any{},
   154  				"webdav": map[string]any{
   155  					"sharedSecret": "secret",
   156  					"permissions":  []any{"read"},
   157  					"url":          "http://example.org",
   158  				},
   159  			},
   160  		},
   161  		{
   162  			in: []Protocol{
   163  				&Webapp{
   164  					URITemplate: "http://example.org",
   165  					ViewMode:    "read",
   166  				},
   167  			},
   168  			expected: map[string]any{
   169  				"name":    "multi",
   170  				"options": map[string]any{},
   171  				"webapp": map[string]any{
   172  					"uriTemplate": "http://example.org",
   173  					"viewMode":    "read",
   174  				},
   175  			},
   176  		},
   177  		{
   178  			in: []Protocol{
   179  				&Datatx{
   180  					SharedSecret: "secret",
   181  					SourceURI:    "http://example.org/source",
   182  					Size:         10,
   183  				},
   184  			},
   185  			expected: map[string]any{
   186  				"name":    "multi",
   187  				"options": map[string]any{},
   188  				"datatx": map[string]any{
   189  					"sharedSecret": "secret",
   190  					"srcUri":       "http://example.org/source",
   191  					"size":         float64(10),
   192  				},
   193  			},
   194  		},
   195  		{
   196  			in: []Protocol{
   197  				&WebDAV{
   198  					SharedSecret: "secret",
   199  					Permissions:  []string{"read"},
   200  					URL:          "http://example.org",
   201  				},
   202  				&Webapp{
   203  					URITemplate: "http://example.org",
   204  					ViewMode:    "read",
   205  				},
   206  				&Datatx{
   207  					SharedSecret: "secret",
   208  					SourceURI:    "http://example.org/source",
   209  					Size:         10,
   210  				},
   211  			},
   212  			expected: map[string]any{
   213  				"name":    "multi",
   214  				"options": map[string]any{},
   215  				"webdav": map[string]any{
   216  					"sharedSecret": "secret",
   217  					"permissions":  []any{"read"},
   218  					"url":          "http://example.org",
   219  				},
   220  				"webapp": map[string]any{
   221  					"uriTemplate": "http://example.org",
   222  					"viewMode":    "read",
   223  				},
   224  				"datatx": map[string]any{
   225  					"sharedSecret": "secret",
   226  					"srcUri":       "http://example.org/source",
   227  					"size":         float64(10),
   228  				},
   229  			},
   230  		},
   231  	}
   232  
   233  	for _, tt := range tests {
   234  		d, err := json.Marshal(tt.in)
   235  		if err != nil && err.Error() != tt.err {
   236  			t.Fatalf("not expected error. got=%+v expected=%+v", err, tt.err)
   237  		}
   238  		if err == nil {
   239  			var got map[string]any
   240  			if err := json.Unmarshal(d, &got); err != nil {
   241  				t.Fatalf("not expected error %+v with input %+v", err, tt.in)
   242  			}
   243  
   244  			if !reflect.DeepEqual(tt.expected, got) {
   245  				t.Fatalf("result does not match with expected. got=%+v expected=%+v", render.AsCode(got), render.AsCode(tt.expected))
   246  			}
   247  		}
   248  	}
   249  }