-
Notifications
You must be signed in to change notification settings - Fork 1
/
duo_test.go
80 lines (67 loc) · 2.26 KB
/
duo_test.go
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
package main
import (
"io/ioutil"
"net/http"
"net/url"
"testing"
duoapi "github.com/duosecurity/duo_api_golang"
"github.com/duosecurity/duo_api_golang/authapi"
"github.com/stretchr/testify/assert"
)
var (
testDuoFailMessage = "Invalid request parameters"
testDuoFailMessageDetail = "username"
)
func duoAuthDeny(factor string, options ...func(*url.Values)) (*authapi.AuthResult, error) {
r := &authapi.AuthResult{StatResult: duoapi.StatResult{Stat: "OK"}}
r.Response.Result = "deny"
r.Response.Status = "deny"
r.Response.Status_Msg = "Login request denied."
return r, nil
}
func duoAuthError(factor string, options ...func(*url.Values)) (*authapi.AuthResult, error) {
return nil, http.ErrHijacked
}
func duoAuthFail(factor string, options ...func(*url.Values)) (*authapi.AuthResult, error) {
r := &authapi.AuthResult{StatResult: duoapi.StatResult{Stat: "FAIL"}}
r.StatResult.Message = &testDuoFailMessage
r.StatResult.Message_Detail = &testDuoFailMessageDetail
return r, nil
}
func duoAuthOK(factor string, options ...func(*url.Values)) (*authapi.AuthResult, error) {
r := &authapi.AuthResult{StatResult: duoapi.StatResult{Stat: "OK"}}
r.Response.Result = "allow"
r.Response.Status = "allow"
r.Response.Status_Msg = "Success. Logging you in..."
return r, nil
}
func TestDuoDeny(t *testing.T) {
duoAuthAuth = duoAuthDeny
request, err := http.NewRequest("GET", "http://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientSuccess.Do(request)
assert.NoError(t, err)
assert.Equal(t, 407, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, statusProxyAuthReq, string(body))
}
func TestDuoError(t *testing.T) {
duoAuthAuth = duoAuthError
request, err := http.NewRequest("GET", "http://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientSuccess.Do(request)
assert.NoError(t, err)
assert.Equal(t, 407, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, statusProxyAuthReq, string(body))
}
func TestDuoFail(t *testing.T) {
duoAuthAuth = duoAuthFail
request, err := http.NewRequest("GET", "https://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientSuccess.Do(request)
assert.Error(t, err)
assert.Nil(t, response)
}