Skip to content

Commit 42f5391

Browse files
committed
testing
1 parent 4235933 commit 42f5391

File tree

7 files changed

+51
-15
lines changed

7 files changed

+51
-15
lines changed

htpasswd.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/sha1"
55
"encoding/base64"
66
"encoding/csv"
7+
"io"
78
"log"
89
"os"
910
)
@@ -15,26 +16,30 @@ type HtpasswdFile struct {
1516
Users map[string]string
1617
}
1718

18-
func NewHtpasswdFile(path string) *HtpasswdFile {
19+
func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) {
1920
log.Printf("using htpasswd file %s", path)
2021
r, err := os.Open(path)
2122
if err != nil {
22-
log.Fatalf("failed opening %v, %s", path, err.Error())
23+
return nil, err
2324
}
24-
csv_reader := csv.NewReader(r)
25+
return NewHtpasswd(r)
26+
}
27+
28+
func NewHtpasswd(file io.Reader) (*HtpasswdFile, error) {
29+
csv_reader := csv.NewReader(file)
2530
csv_reader.Comma = ':'
2631
csv_reader.Comment = '#'
2732
csv_reader.TrimLeadingSpace = true
2833

2934
records, err := csv_reader.ReadAll()
3035
if err != nil {
31-
log.Fatalf("Failed reading file %s", err.Error())
36+
return nil, err
3237
}
3338
h := &HtpasswdFile{Users: make(map[string]string)}
3439
for _, record := range records {
3540
h.Users[record[0]] = record[1]
3641
}
37-
return h
42+
return h, nil
3843
}
3944

4045
func (h *HtpasswdFile) Validate(user string, password string) bool {

htpasswd_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"github.com/bmizerany/assert"
6+
"testing"
7+
)
8+
9+
func TestHtpasswd(t *testing.T) {
10+
file := bytes.NewBuffer([]byte("testuser:{SHA}PaVBVZkYqAjCQCu6UBL2xgsnZhw=\n"))
11+
h, err := NewHtpasswd(file)
12+
assert.Equal(t, err, nil)
13+
14+
valid := h.Validate("testuser", "asdf")
15+
assert.Equal(t, valid, true)
16+
}

main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"log"
67
"net"
78
"net/http"
89
"net/url"
910
"strings"
10-
"fmt"
1111
)
1212

1313
const VERSION = "0.0.1"
@@ -72,7 +72,10 @@ func main() {
7272
oauthproxy.SignInMessage = fmt.Sprintf("using a %s email address", *googleAppsDomain)
7373
}
7474
if *htpasswdFile != "" {
75-
oauthproxy.HtpasswdFile = NewHtpasswdFile(*htpasswdFile)
75+
oauthproxy.HtpasswdFile, err = NewHtpasswdFromFile(*htpasswdFile)
76+
if err != nil {
77+
log.Fatalf("FATAL: unable to open %s %s", *htpasswdFile, err.Error())
78+
}
7679
}
7780
listener, err := net.Listen("tcp", *httpAddr)
7881
if err != nil {

oauthproxy.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, m
169169
rw.WriteHeader(code)
170170
templates := getTemplates()
171171
t := struct {
172-
Title string
173-
Message string
172+
Title string
173+
Message string
174174
}{
175-
Title: fmt.Sprintf("%d %s", code, title),
176-
Message: message,
175+
Title: fmt.Sprintf("%d %s", code, title),
176+
Message: message,
177177
}
178178
templates.ExecuteTemplate(rw, "error.html", t)
179179
}
@@ -254,7 +254,7 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
254254
return
255255
}
256256
}
257-
257+
258258
cookie, err := req.Cookie(p.CookieKey)
259259
var ok bool
260260
var email string

templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func getTemplates() *template.Template {
1818
if err != nil {
1919
log.Fatalf("failed parsing template %s", err.Error())
2020
}
21-
21+
2222
t, err = t.Parse(`{{define "error.html"}}
2323
<html><head><title>{{.Title}}</title></head>
2424
<body>

templates_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"github.com/bmizerany/assert"
5+
"testing"
6+
)
7+
8+
func TestTemplatesCompile(t *testing.T) {
9+
templates := getTemplates()
10+
assert.NotEqual(t, templates, nil)
11+
12+
}

validator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4-
"os"
5-
"log"
64
"encoding/csv"
75
"fmt"
6+
"log"
7+
"os"
88
"strings"
99
)
1010

0 commit comments

Comments
 (0)