...

Source file src/github.com/openshift/api/osin/v1/stringsource_test.go

Documentation: github.com/openshift/api/osin/v1

     1  package v1
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  
     9  	"k8s.io/apimachinery/pkg/runtime"
    10  	"k8s.io/apimachinery/pkg/runtime/serializer"
    11  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    12  
    13  	configv1 "github.com/openshift/api/config/v1"
    14  )
    15  
    16  var testScheme = runtime.NewScheme()
    17  
    18  func init() {
    19  	utilruntime.Must(Install(testScheme))
    20  }
    21  
    22  func TestStringSourceUnmarshaling(t *testing.T) {
    23  	codec := serializer.NewCodecFactory(testScheme).LegacyCodec(GroupVersion)
    24  
    25  	testcases := map[string]struct {
    26  		JSON           string
    27  		ExpectedObject configv1.StringSource
    28  		ExpectedError  string
    29  	}{
    30  		"bool": {
    31  			JSON:           `true`,
    32  			ExpectedObject: configv1.StringSource{},
    33  			ExpectedError:  "cannot unmarshal",
    34  		},
    35  		"number": {
    36  			JSON:           `1`,
    37  			ExpectedObject: configv1.StringSource{},
    38  			ExpectedError:  "cannot unmarshal",
    39  		},
    40  
    41  		"empty string": {
    42  			JSON:           `""`,
    43  			ExpectedObject: configv1.StringSource{},
    44  			ExpectedError:  "",
    45  		},
    46  		"string": {
    47  			JSON:           `"foo"`,
    48  			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Value: "foo"}},
    49  			ExpectedError:  "",
    50  		},
    51  
    52  		"empty struct": {
    53  			JSON:           `{}`,
    54  			ExpectedObject: configv1.StringSource{},
    55  			ExpectedError:  "",
    56  		},
    57  		"struct value": {
    58  			JSON:           `{"value":"foo"}`,
    59  			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Value: "foo"}},
    60  			ExpectedError:  "",
    61  		},
    62  		"struct env": {
    63  			JSON:           `{"env":"foo"}`,
    64  			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Env: "foo"}},
    65  			ExpectedError:  "",
    66  		},
    67  		"struct file": {
    68  			JSON:           `{"file":"foo"}`,
    69  			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{File: "foo"}},
    70  			ExpectedError:  "",
    71  		},
    72  		"struct file+keyFile": {
    73  			JSON:           `{"file":"foo","keyFile":"bar"}`,
    74  			ExpectedObject: configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{File: "foo", KeyFile: "bar"}},
    75  			ExpectedError:  "",
    76  		},
    77  	}
    78  
    79  	for k, tc := range testcases {
    80  		t.Run(k, func(t *testing.T) {
    81  			// Wrap in a dummy object we can deserialize
    82  			input := fmt.Sprintf(`{"kind":"GitHubIdentityProvider","apiVersion":"osin.config.openshift.io/v1","clientSecret":%s}`, tc.JSON)
    83  			githubProvider := &GitHubIdentityProvider{}
    84  			err := runtime.DecodeInto(codec, []byte(input), githubProvider)
    85  			if len(tc.ExpectedError) > 0 && (err == nil || !strings.Contains(err.Error(), tc.ExpectedError)) {
    86  				t.Errorf("%s: expected error containing %q, got %q", k, tc.ExpectedError, err.Error())
    87  			}
    88  			if len(tc.ExpectedError) == 0 && err != nil {
    89  				t.Errorf("%s: got unexpected error: %v", k, err)
    90  			}
    91  			if err != nil {
    92  				return
    93  			}
    94  			if !reflect.DeepEqual(tc.ExpectedObject, githubProvider.ClientSecret) {
    95  				t.Errorf("%s: expected\n%#v\ngot\n%#v", k, tc.ExpectedObject, githubProvider.ClientSecret)
    96  			}
    97  		})
    98  	}
    99  }
   100  
   101  func TestStringSourceMarshaling(t *testing.T) {
   102  	codec := serializer.NewCodecFactory(testScheme).LegacyCodec(GroupVersion)
   103  
   104  	testcases := map[string]struct {
   105  		Object       configv1.StringSource
   106  		ExpectedJSON string
   107  	}{
   108  		"empty string": {
   109  			Object:       configv1.StringSource{},
   110  			ExpectedJSON: `""`,
   111  		},
   112  		"string": {
   113  			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Value: "foo"}},
   114  			ExpectedJSON: `"foo"`,
   115  		},
   116  		"struct value+keyFile": {
   117  			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Value: "foo", KeyFile: "bar"}},
   118  			ExpectedJSON: `{"value":"foo","env":"","file":"","keyFile":"bar"}`,
   119  		},
   120  		"struct env": {
   121  			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{Env: "foo"}},
   122  			ExpectedJSON: `{"value":"","env":"foo","file":"","keyFile":""}`,
   123  		},
   124  		"struct file": {
   125  			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{File: "foo"}},
   126  			ExpectedJSON: `{"value":"","env":"","file":"foo","keyFile":""}`,
   127  		},
   128  		"struct file+keyFile": {
   129  			Object:       configv1.StringSource{StringSourceSpec: configv1.StringSourceSpec{File: "foo", KeyFile: "bar"}},
   130  			ExpectedJSON: `{"value":"","env":"","file":"foo","keyFile":"bar"}`,
   131  		},
   132  	}
   133  
   134  	for k, tc := range testcases {
   135  		provider := &GitHubIdentityProvider{ClientSecret: tc.Object}
   136  
   137  		json, err := runtime.Encode(codec, provider)
   138  		if err != nil {
   139  			t.Errorf("%s: unexpected error: %v", k, err)
   140  		}
   141  
   142  		// Wrap in a dummy JSON from the surrounding object
   143  		input := fmt.Sprintf(`{"kind":"GitHubIdentityProvider","apiVersion":"osin.config.openshift.io/v1","clientID":"","clientSecret":%s,"organizations":null,"teams":null,"hostname":"","ca":""}`, tc.ExpectedJSON)
   144  		if strings.TrimSpace(string(json)) != input {
   145  			t.Log(len(input), len(json))
   146  			t.Errorf("%s: expected\n%s\ngot\n%s", k, input, string(json))
   147  		}
   148  	}
   149  }
   150  

View as plain text