Skip to content
Snippets Groups Projects
Select Git revision
  • 969a0d4bbc1cdfc763b1b57672dd32eca1cc6abf
  • master default protected
  • greenkeeper/webpack-4.10.1
  • greenkeeper/webpack-4.10.0
  • greenkeeper/webpack-4.9.2
  • greenkeeper/promise-polyfill-8.0.0
  • greenkeeper/webpack-4.9.1
  • greenkeeper/webpack-4.9.0
  • greenkeeper/webpack-manifest-plugin-2.0.3
  • greenkeeper/update-to-node-10
  • gh-pages
  • greenkeeper/webpack-4.8.3
  • greenkeeper/webpack-4.8.2
  • greenkeeper/webpack-4.7.0
  • greenkeeper/webpack-manifest-plugin-2.0.2
  • greenkeeper/webpack-manifest-plugin-2.0.1
  • greenkeeper/style-loader-0.21.0
  • greenkeeper/webpack-4.6.0
  • greenkeeper/sass-loader-7.0.1
  • greenkeeper/sass-loader-7.0.0
  • greenkeeper/webpack-manifest-plugin-2.0.0
  • 2.7.3
  • 2.7.2
  • 2.7.1
  • 2.7.0
  • 2.6.6
  • 2.6.5
  • 2.6.4
  • 2.6.3
  • 2.6.2
  • 2.6.1
  • 2.6.0
  • 2.5.5
  • 2.5.4
  • 2.5.3
  • 2.5.2
  • 2.5.1
  • 2.5.0
  • 2.4.0
  • 2.3.0
  • 2.2.6
41 results

customization.md

Blame
  • config_test.go 4.86 KiB
    /*
    Copyright 2015 All rights reserved.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
        http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */
    
    package main
    
    import (
    	"io/ioutil"
    	"os"
    	"testing"
    
    	"github.com/codegangsta/cli"
    )
    
    func TestNewDefaultConfig(t *testing.T) {
    	if config := newDefaultConfig(); config == nil {
    		t.Errorf("we should have recieved a config")
    	}
    }
    
    func TestReadConfiguration(t *testing.T) {
    	testCases := []struct {
    		Content string
    		Ok      bool
    	}{
    		{
    			Content: `
    discovery_url: https://keyclock.domain.com/
    client-id: <client_id>
    secret: <secret>
    `,
    		},
    		{
    			Content: `
    discovery_url: https://keyclock.domain.com
    client-id: <client_id>
    secret: <secret>
    upstream-url: http://127.0.0.1:8080
    redirection_url: http://127.0.0.1:3000
    `,
    			Ok: true,
    		},
    	}
    
    	for i, test := range testCases {
    		// step: write the fake config file
    		file := writeFakeConfigFile(t, test.Content)
    		defer func(f *os.File) {
    			os.Remove(f.Name())
    		}(file)
    
    		config := new(Config)
    		err := readConfigFile(file.Name(), config)
    		if test.Ok && err != nil {
    			t.Errorf("test case %d should not have failed, config: %v, error: %s", i, config, err)
    		}
    	}
    }
    
    func TestIsConfig(t *testing.T) {
    	tests := []struct {
    		Config *Config
    		Ok     bool
    	}{
    		{
    			Config: &Config{},
    		},
    		{
    			Config: &Config{
    				DiscoveryURL: "http://127.0.0.1:8080",
    			},
    		},
    		{
    			Config: &Config{
    				DiscoveryURL: "http://127.0.0.1:8080",
    				ClientID:     "client",
    				ClientSecret: "client",
    			},
    		},
    		{
    			Config: &Config{
    				Listen:         ":8080",
    				DiscoveryURL:   "http://127.0.0.1:8080",
    				ClientID:       "client",
    				ClientSecret:   "client",
    				RedirectionURL: "http://120.0.0.1",
    			},
    		},
    		{
    			Config: &Config{
    				Listen:         ":8080",
    				DiscoveryURL:   "http://127.0.0.1:8080",
    				ClientID:       "client",
    				ClientSecret:   "client",
    				RedirectionURL: "http://120.0.0.1",
    				Upstream:       "http://120.0.0.1",
    			},
    			Ok: true,
    		},
    		{
    			Config: &Config{
    				Listen:         ":8080",
    				DiscoveryURL:   "http://127.0.0.1:8080",
    				ClientID:       "client",
    				ClientSecret:   "client",
    				RedirectionURL: "http://120.0.0.1",
    				Upstream:       "http://120.0.0.1",
    				MatchClaims: map[string]string{
    					"test": "&&&[",
    				},
    			},
    		},
    		{
    			Config: &Config{
    				Listen:                ":8080",
    				SkipTokenVerification: true,
    				Upstream:              "http://120.0.0.1",
    			},
    			Ok: true,
    		},
    		{
    			Config: &Config{
    				DiscoveryURL:   "http://127.0.0.1:8080",
    				ClientID:       "client",
    				ClientSecret:   "client",
    				RedirectionURL: "http://120.0.0.1",
    				Upstream:       "http://120.0.0.1",
    			},
    		},
    		{
    			Config: &Config{
    				Listen:         ":8080",
    				DiscoveryURL:   "http://127.0.0.1:8080",
    				ClientID:       "client",
    				ClientSecret:   "client",
    				RedirectionURL: "http://120.0.0.1",
    			},
    		},
    		{
    			Config: &Config{
    				Listen:         ":8080",
    				DiscoveryURL:   "http://127.0.0.1:8080",
    				ClientID:       "client",
    				ClientSecret:   "client",
    				RedirectionURL: "http://120.0.0.1",
    				Upstream:       "this should fail",
    			},
    		},
    		{
    			Config: &Config{
    				Listen:         ":8080",
    				DiscoveryURL:   "http://127.0.0.1:8080",
    				ClientID:       "client",
    				ClientSecret:   "client",
    				RedirectionURL: "http://120.0.0.1",
    				Upstream:       "this should fail",
    				SecureCookie:   true,
    			},
    		},
    		{
    			Config: &Config{
    				Listen:         ":8080",
    				DiscoveryURL:   "http://127.0.0.1:8080",
    				ClientID:       "client",
    				ClientSecret:   "client",
    				RedirectionURL: "https://120.0.0.1",
    				Upstream:       "this should fail",
    				SecureCookie:   true,
    			},
    			Ok: true,
    		},
    	}
    
    	for i, c := range tests {
    		if err := c.Config.isValid(); err != nil && c.Ok {
    			t.Errorf("test case %d, the config should not have errored, error: %s", i, err)
    		}
    	}
    }
    
    func TestReadOptions(t *testing.T) {
    	c := cli.NewApp()
    	c.Flags = getOptions()
    	c.Action = func(cx *cli.Context) {
    		readOptions(cx, &Config{})
    	}
    	c.Run([]string{""})
    }
    
    func TestGetOptions(t *testing.T) {
    	if flags := getOptions(); flags == nil {
    		t.Errorf("we should have received some flags options")
    	}
    }
    
    func writeFakeConfigFile(t *testing.T, content string) *os.File {
    	f, err := ioutil.TempFile("", "node_label_file")
    	if err != nil {
    		t.Fatalf("unexpected error creating node_label_file: %v", err)
    	}
    	f.Close()
    
    	if err := ioutil.WriteFile(f.Name(), []byte(content), 0700); err != nil {
    		t.Fatalf("unexpected error writing node label file: %v", err)
    	}
    
    	return f
    }