File: consumer_key_test.go

package info (click to toggle)
golang-github-ovh-go-ovh 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: makefile: 5
file content (158 lines) | stat: -rw-r--r-- 4,826 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ovh

import (
	"fmt"
	"net/http"
	"reflect"
	"testing"
	"time"
)

// Common helpers are in ovh_test.go

func TestNewCkRequest(t *testing.T) {
	const expectedRequest = `{"accessRules":[{"method":"GET","path":"/me"},{"method":"GET","path":"/xdsl/*"}]}`

	// Init test
	var InputRequest *http.Request
	var InputRequestBody string
	ts, client := initMockServer(&InputRequest, 200, `{
		"validationUrl":"https://validation.url",
		"ConsumerKey":"`+MockConsumerKey+`",
		"state":"pendingValidation"
	}`, &InputRequestBody, time.Duration(0))
	client.ConsumerKey = ""
	defer ts.Close()

	// Test
	ckRequest := client.NewCkRequest()
	ckRequest.AddRule("GET", "/me")
	ckRequest.AddRule("GET", "/xdsl/*")

	got, err := ckRequest.Do()

	// Validate
	if err != nil {
		t.Fatalf("CkRequest.Do() should not return an error. Got: %q", err)
	}
	if client.ConsumerKey != MockConsumerKey {
		t.Fatalf("CkRequest.Do() should set client.ConsumerKey to %s. Got %s", MockConsumerKey, client.ConsumerKey)
	}
	if got.ConsumerKey != MockConsumerKey {
		t.Fatalf("CkRequest.Do() should set CkValidationState.ConsumerKey to %s. Got %s", MockConsumerKey, got.ConsumerKey)
	}
	if got.ValidationURL == "" {
		t.Fatalf("CkRequest.Do() should set CkValidationState.ValidationURL")
	}
	if InputRequestBody != expectedRequest {
		t.Fatalf("CkRequest.Do() should issue '%s' request. Got %s", expectedRequest, InputRequestBody)
	}
	ensureHeaderPresent(t, InputRequest, "Accept", "application/json")
	ensureHeaderPresent(t, InputRequest, "X-Ovh-Application", MockApplicationKey)
}

func TestInvalidCkRequest(t *testing.T) {
	// Init test
	var InputRequest *http.Request
	var InputRequestBody string
	ts, client := initMockServer(&InputRequest, http.StatusForbidden, `{"message":"Invalid application key"}`, &InputRequestBody, time.Duration(0))
	client.ConsumerKey = ""
	defer ts.Close()

	// Test
	ckRequest := client.NewCkRequest()
	ckRequest.AddRule("GET", "/me")
	ckRequest.AddRule("GET", "/xdsl/*")

	_, err := ckRequest.Do()
	apiError, ok := err.(*APIError)

	// Validate
	if err == nil {
		t.Fatal("Expected an error, got none")
	}
	if !ok {
		t.Fatal("Expected error of type APIError")
	}
	if apiError.Code != http.StatusForbidden {
		t.Fatalf("Expected HTTP error 403. Got %d", apiError.Code)
	}
	if apiError.Message != "Invalid application key" {
		t.Fatalf("Expected API error message 'Invalid application key'. Got '%s'", apiError.Message)
	}
}

func TestAddRules(t *testing.T) {
	// Init test
	var InputRequest *http.Request
	var InputRequestBody string
	ts, client := initMockServer(&InputRequest, http.StatusForbidden, `{"message":"Invalid application key"}`, &InputRequestBody, time.Duration(0))
	client.ConsumerKey = ""
	defer ts.Close()

	// Test: allow all
	ckRequest := client.NewCkRequest()
	ckRequest.AddRecursiveRules(ReadWrite, "/")
	ExpectedRules := []AccessRule{
		{Method: "GET", Path: "/*"},
		{Method: "POST", Path: "/*"},
		{Method: "PUT", Path: "/*"},
		{Method: "DELETE", Path: "/*"},
	}
	if !reflect.DeepEqual(ckRequest.AccessRules, ExpectedRules) {
		t.Fatalf("Inserting recursive RW rules for / should generate %v. Got %v", ExpectedRules, ckRequest.AccessRules)
	}

	// Test: allow exactly /sms, RO
	ckRequest = client.NewCkRequest()
	ckRequest.AddRules(ReadOnly, "/sms")
	ExpectedRules = []AccessRule{
		{Method: "GET", Path: "/sms"},
	}
	if !reflect.DeepEqual(ckRequest.AccessRules, ExpectedRules) {
		t.Fatalf("Inserting RO rule for /sms should generate %v. Got %v", ExpectedRules, ckRequest.AccessRules)
	}

	// Test: allow /sms/*, RW, no delete
	ckRequest = client.NewCkRequest()
	ckRequest.AddRecursiveRules(ReadWriteSafe, "/sms")
	ExpectedRules = []AccessRule{
		{Method: "GET", Path: "/sms"},
		{Method: "POST", Path: "/sms"},
		{Method: "PUT", Path: "/sms"},

		{Method: "GET", Path: "/sms/*"},
		{Method: "POST", Path: "/sms/*"},
		{Method: "PUT", Path: "/sms/*"},
	}
	if !reflect.DeepEqual(ckRequest.AccessRules, ExpectedRules) {
		t.Fatalf("Inserting recursive safe RW rule for /sms should generate %v. Got %v", ExpectedRules, ckRequest.AccessRules)
	}
}

func TestCkRequestString(t *testing.T) {
	ckValidationState := &CkValidationState{
		ConsumerKey:   "ck",
		State:         "pending",
		ValidationURL: "fakeURL",
	}

	expected := fmt.Sprintf("CK: \"ck\"\nStatus: \"pending\"\nValidation URL: \"fakeURL\"\n")
	got := fmt.Sprintf("%s", ckValidationState)

	if got != expected {
		t.Errorf("expected %q, got %q", expected, got)
	}
}

func TestCkRequestRedirection(t *testing.T) {
	client, _ := NewClient("endpoint", "appKey", "appSecret", "consumerKey")

	redirection := "http://localhost/api/auth/callback?token=123456"

	ckRequest := client.NewCkRequestWithRedirection(redirection)

	if ckRequest.Redirection != redirection {
		t.Fatalf("NewCkRequestWithRedirection should set ckRequest.Redirection")
	}
}