1package auth
2
3import (
4	"testing"
5
6	"github.com/fabiolb/fabio/config"
7)
8
9func TestLoadAuthSchemes(t *testing.T) {
10
11	t.Run("should fail when auth scheme fails to load", func(t *testing.T) {
12		_, err := LoadAuthSchemes(map[string]config.AuthScheme{
13			"myauth": {
14				Name: "myauth",
15				Type: "basic",
16				Basic: config.BasicAuth{
17					File: "/some/non/existent/file",
18				},
19			},
20		})
21
22		const errorText = "open /some/non/existent/file: no such file or directory"
23
24		if err.Error() != errorText {
25			t.Fatalf("got %s, want %s", err.Error(), errorText)
26		}
27	})
28
29	t.Run("should return an error when auth type is unknown", func(t *testing.T) {
30		_, err := LoadAuthSchemes(map[string]config.AuthScheme{
31			"myauth": {
32				Name: "myauth",
33				Type: "foo",
34			},
35		})
36
37		const errorText = "unknown auth type 'foo'"
38
39		if err.Error() != errorText {
40			t.Fatalf("got %s, want %s", err.Error(), errorText)
41		}
42	})
43
44	t.Run("should load multiple auth schemes", func(t *testing.T) {
45		myauth, err := createBasicAuthFile("foo:bar")
46		if err != nil {
47			t.Fatalf("could not create file on disk %s", err)
48		}
49
50		myotherauth, err := createBasicAuthFile("bar:foo")
51		if err != nil {
52			t.Fatalf("could not create file on disk %s", err)
53		}
54
55		result, err := LoadAuthSchemes(map[string]config.AuthScheme{
56			"myauth": {
57				Name: "myauth",
58				Type: "basic",
59				Basic: config.BasicAuth{
60					File: myauth,
61				},
62			},
63			"myotherauth": {
64				Name: "myotherauth",
65				Type: "basic",
66				Basic: config.BasicAuth{
67					File: myotherauth,
68				},
69			},
70		})
71
72		if len(result) != 2 {
73			t.Fatalf("expected 2 auth schemes, got %d", len(result))
74		}
75	})
76}
77