提交 3ecf90b6 编写于 作者: J Jingwen Owen Ou

Get 2fa cucumber tests going

上级 8e87249a
...@@ -35,8 +35,8 @@ ...@@ -35,8 +35,8 @@
}, },
{ {
"ImportPath": "github.com/jingweno/go-octokit/octokit", "ImportPath": "github.com/jingweno/go-octokit/octokit",
"Comment": "v0.4.0-41-g85bc6b5", "Comment": "v0.4.0-46-g30737cb",
"Rev": "74f0495a72d8a2dfce059ebf718fd60dd3800f41" "Rev": "30737cb1e9207e1a7633e3d7539951c5c3d93058"
}, },
{ {
"ImportPath": "github.com/jtacoma/uritemplates", "ImportPath": "github.com/jtacoma/uritemplates",
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
}, },
{ {
"ImportPath": "github.com/lostisland/go-sawyer", "ImportPath": "github.com/lostisland/go-sawyer",
"Rev": "ec1b99a1915d53f09d40d0c87caf60460b7f8728" "Rev": "cb3f837f5ac26e080a9002dcb4d2e8472e623916"
} }
] ]
} }
...@@ -118,7 +118,7 @@ func getResponseErrorType(resp *http.Response) ResponseErrorType { ...@@ -118,7 +118,7 @@ func getResponseErrorType(resp *http.Response) ResponseErrorType {
case code == http.StatusUnauthorized: case code == http.StatusUnauthorized:
header := resp.Header.Get("X-GitHub-OTP") header := resp.Header.Get("X-GitHub-OTP")
r := regexp.MustCompile(`(?i)required; (\\w+)`) r := regexp.MustCompile(`(?i)required; (\w+)`)
if r.MatchString(header) { if r.MatchString(header) {
return ErrorOneTimePasswordRequired return ErrorOneTimePasswordRequired
} }
......
...@@ -26,6 +26,40 @@ func TestResponseError_Error_400(t *testing.T) { ...@@ -26,6 +26,40 @@ func TestResponseError_Error_400(t *testing.T) {
assert.Equal(t, ErrorBadRequest, e.Type) assert.Equal(t, ErrorBadRequest, e.Type)
} }
func TestResponseError_Error_401(t *testing.T) {
setup()
defer tearDown()
mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
head := w.Header()
head.Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
respondWith(w, `{"message":"Unauthorized"}`)
})
req, _ := client.NewRequest("error")
_, err := req.Get(nil)
assert.Tf(t, strings.Contains(err.Error(), "401 - Unauthorized"), "%s", err.Error())
e := err.(*ResponseError)
assert.Equal(t, ErrorUnauthorized, e.Type)
mux.HandleFunc("/error_2fa", func(w http.ResponseWriter, r *http.Request) {
head := w.Header()
head.Set("Content-Type", "application/json")
head.Set("X-GitHub-OTP", "required; app")
w.WriteHeader(http.StatusUnauthorized)
respondWith(w, `{"message":"Unauthorized"}`)
})
req, _ = client.NewRequest("error_2fa")
_, err = req.Get(nil)
assert.Tf(t, strings.Contains(err.Error(), "401 - Unauthorized"), "%s", err.Error())
e = err.(*ResponseError)
assert.Equal(t, ErrorOneTimePasswordRequired, e.Type)
}
func TestResponseError_Error_422_error(t *testing.T) { func TestResponseError_Error_422_error(t *testing.T) {
setup() setup()
defer tearDown() defer tearDown()
......
#!/bin/bash #!/bin/bash
gofmt -w -l "$@" *.go
gofmt -w -l *.go ./mediatype # don't run gofmt in these directories
ignored=(/bin/ /docs/ /log/ /script/ /tmp/)
for i in */ ; do
if [[ ! ${ignored[*]} =~ "/$i" ]]; then
gofmt -w -l "$@" "${i%?}"
fi
done
script/fmt script/fmt
go test -race -v "$@" ./mediatype
go test -race -v "$@" . packages=". ./hypermedia ./mediatype ./mediaheader"
go test -i $packages
go test -race -v $packages
...@@ -5,11 +5,11 @@ import ( ...@@ -5,11 +5,11 @@ import (
"fmt" "fmt"
"github.com/howeyc/gopass" "github.com/howeyc/gopass"
"github.com/jingweno/gh/utils" "github.com/jingweno/gh/utils"
"github.com/jingweno/go-octokit/octokit"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
) )
...@@ -37,10 +37,8 @@ func (c *Configs) PromptFor(host string) *Credentials { ...@@ -37,10 +37,8 @@ func (c *Configs) PromptFor(host string) *Credentials {
// Create Client with a stub Credentials // Create Client with a stub Credentials
client := &Client{Credentials: &Credentials{Host: host}} client := &Client{Credentials: &Credentials{Host: host}}
token, err := client.FindOrCreateToken(user, pass, "") token, err := client.FindOrCreateToken(user, pass, "")
// TODO: return a two-factor error
if err != nil { if err != nil {
re := regexp.MustCompile("two-factor authentication OTP code") if re, ok := err.(*octokit.ResponseError); ok && re.Type == octokit.ErrorOneTimePasswordRequired {
if re.MatchString(fmt.Sprintf("%s", err)) {
code := c.PromptForOTP() code := c.PromptForOTP()
token, err = client.FindOrCreateToken(user, pass, code) token, err = client.FindOrCreateToken(user, pass, code)
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册