github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/sdk/exttest/auth.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package exttest 13 14 import ( 15 "errors" 16 "os" 17 "strings" 18 "testing" 19 20 "github.com/documize/community/sdk" 21 ) 22 23 // auth provides authorization tests to be run locally or from the main Documize repo. 24 func auth(t *testing.T) (*documize.Client, error) { 25 26 testEndPt := os.Getenv("DOCUMIZEAPI") //e.g. "http://localhost:5002" 27 testAuth := os.Getenv("DOCUMIZEAUTH") //e.g. "demo1:jim@davidson.com:demo123" 28 29 testCreds(t, testEndPt, testAuth) 30 testEndpoint(t, testEndPt, testAuth) 31 32 //t.Log("Auth", testEndPt, testAuth) 33 34 c, err := documize.NewClient(testEndPt, testAuth) // should work 35 if err == nil && c == nil { 36 err = errors.New("unable to authorize, new client nil") 37 } 38 return c, err 39 } 40 41 func testCreds(t *testing.T, testEndPt, testAuth string) { 42 _, err := documize.NewClient(testEndPt, "") 43 if err == nil { 44 t.Error("ExtTestAuth did not error on empty auth string ") 45 } else { 46 t.Log("INFO: Empty Auth string error:", err) 47 } 48 _, err = documize.NewClient(testEndPt, "AAA:BBB") 49 if err == nil { 50 t.Error("ExtTestAuth did not error on AAA:BBB auth string ") 51 } else { 52 t.Log("INFO: Malfomed auth string error:", err) 53 } 54 credentials := strings.SplitN(testAuth, ":", 3) 55 if len(credentials) == 3 { 56 base := []string{"XXX", "YYY", "ZZZ"} 57 for i := range credentials { 58 ta := make([]string, 3) 59 copy(ta, base) 60 for j := range ta { 61 if j != i { // make sure one of the three is wrong 62 ta[j] = credentials[j] 63 } 64 } 65 as := strings.Join(ta, ":") 66 //t.Log(as) 67 if credentials[i] != "" { // to avoid the case where the sub-domain is empty 68 _, err = documize.NewClient(testEndPt, as) 69 if err == nil { 70 t.Error("ExtTestAuth did not error on bad auth string: ", as) 71 } else { 72 t.Log("INFO: Bad component to auth string error:", err) 73 } 74 } 75 } 76 } 77 } 78 79 func testEndpoint(t *testing.T, testEndPt, testAuth string) { 80 _, err := documize.NewClient("", testAuth) 81 if err == nil { 82 t.Error("ExtTestAuth did not error on empty end point") 83 } else { 84 t.Log("INFO: Empty end-point error:", err) 85 } 86 _, err = documize.NewClient("XXXXX", testAuth) 87 if err == nil { 88 t.Error("ExtTestAuth did not error on bad end point") 89 } else { 90 t.Log("INFO: Bad end point error:", err) 91 } 92 _, err = documize.NewClient("http://XXXXXYYYYYYZZZZZZ.com", testAuth) 93 if err == nil { 94 t.Error("ExtTestAuth did not error on invalid end point") 95 } else { 96 t.Log("INFO: Invalid end point error:", err) 97 } 98 }