Skip to content
Permalink
Browse files

Add tests for bamboo tools

  • Loading branch information...
frioux committed Apr 6, 2019
1 parent f950d95 commit 480209b7ba1a28531c88202471966a6c673cdbde
@@ -0,0 +1,104 @@
package bamboo

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"regexp"
"strings"

"github.com/headzoo/surf"
"github.com/headzoo/surf/browser"
"github.com/pkg/errors"
)

type client struct {
authURL, dirURL, treeURL string

b *browser.Browser
}

func newClient() client {
return client{
authURL: "https://ziprecruiter1.bamboohr.com/login.php",
dirURL: "https://ziprecruiter1.bamboohr.com/employee_directory/ajax/get_directory_info",
treeURL: "https://ziprecruiter1.bamboohr.com/employees/orgchart.php?pin",
}
}

func (c *client) auth() error {
ua := surf.NewBrowser()
err := ua.Open(c.authURL)
if err != nil {
return errors.Wrap(err, "auth")
}

fm, err := ua.Form("form")
if err != nil {
return fmt.Errorf("auth: %s", err)
}

err = fm.Input("username", os.Getenv("BAMBOO_USER"))
if err != nil {
return errors.Wrap(err, "fm.Input")
}
err = fm.Input("password", os.Getenv("BAMBOO_PASSWORD"))
if err != nil {
return errors.Wrap(err, "fm.Input")
}

err = fm.Submit()
if err != nil {
return errors.Wrap(err, "auth")
}
c.b = ua

return nil
}

func (c *client) directory(w io.Writer) error {
if err := c.b.Open(c.dirURL); err != nil {
return err
}

if _, err := c.b.Download(w); err != nil {
return err
}

return nil
}

func (c *client) tree(w io.Writer) error {
if err := c.b.Open(c.treeURL); err != nil {
return errors.Wrap(err, "export-bamboohr-tree")
}
buff := &bytes.Buffer{}

if _, err := c.b.Download(buff); err != nil {
return errors.Wrap(err, "export-bamboohr-tree")
}

reader := bufio.NewReader(strings.NewReader(buff.String()))
re := regexp.MustCompile("json = (.*);")

var err error

for err == nil {
var line string
line, err = reader.ReadString('\n')
if err != nil && err != io.EOF {
return err
}
fmt.Printf("%q\n", line)
if strings.Contains(line, "json = ") {
if m := re.FindStringSubmatch(line); len(m) > 0 {
_, err := fmt.Fprint(w, m[1])
return err
}
}
}

return errors.New("export-bamboohr-tree: couldn't find json")
}
@@ -1,110 +1,27 @@
package bamboo

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"regexp"
"strings"

"github.com/headzoo/surf"
"github.com/headzoo/surf/browser"
"github.com/pkg/errors"
)

func auth() (*browser.Browser, error) {
ua := surf.NewBrowser()
err := ua.Open("https://ziprecruiter1.bamboohr.com/login.php")
if err != nil {
return nil, errors.Wrap(err, "auth")
}

fm, err := ua.Form("form")
if err != nil {
return nil, fmt.Errorf("auth: %s", err)
}

err = fm.Input("username", os.Getenv("BAMBOO_USER"))
if err != nil {
return nil, errors.Wrap(err, "fm.Input")
}
err = fm.Input("password", os.Getenv("BAMBOO_PASSWORD"))
if err != nil {
return nil, errors.Wrap(err, "fm.Input")
}

err = fm.Submit()
if err != nil {
return nil, errors.Wrap(err, "auth")
}

return ua, nil
}

const bamboo = "https://ziprecruiter1.bamboohr.com/employee_directory/ajax/get_directory_info"

// ExportDirectory will write the JSON extracted from bamboohr to stdout.
func ExportDirectory([]string, io.Reader) error {
ua, err := auth()
if err != nil {
fmt.Fprintf(os.Stderr, "export-bamboohr: %s\n", err)
os.Exit(1)
}

err = ua.Open(bamboo)
if err != nil {
fmt.Fprintf(os.Stderr, "export-bamboohr: %s\n", err)
os.Exit(1)
}
_, err = ua.Download(os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "export-bamboohr: %s\n", err)
os.Exit(1)
c := newClient()
if err := c.auth(); err != nil {
return err
}

return nil
return c.directory(os.Stdout)
}

const tree = "https://ziprecruiter1.bamboohr.com/employees/orgchart.php?pin"

// ExportOrgChart will write the JSON extracted from the bamboohr org chart
// to stdout.
func ExportOrgChart([]string, io.Reader) error {
ua, err := auth()
if err != nil {
return errors.Wrap(err, "export-bamboohr-tree")
}

err = ua.Open(tree)
if err != nil {
return errors.Wrap(err, "export-bamboohr-tree")
}
buff := bytes.NewBuffer([]byte{})

_, err = ua.Download(buff)
if err != nil {
return errors.Wrap(err, "export-bamboohr-tree")
}

reader := bufio.NewReader(strings.NewReader(buff.String()))
re := regexp.MustCompile("json = (.*);")

for err == nil {
var line string
line, err = reader.ReadString('\n')
if err != nil && err != io.EOF {
fmt.Fprintf(os.Stderr, "export-bamboohr-tree: %s\n", err)
os.Exit(1)
}
if strings.Contains(line, "json = ") {
if m := re.FindStringSubmatch(line); len(m) > 0 {
fmt.Print(m[1])
return nil
}
}
c := newClient()
if err := c.auth(); err != nil {
return err
}

return errors.New("export-bamboohr-tree: couldn't find json")
return c.tree(os.Stdout)
}
@@ -0,0 +1,40 @@
package bamboo

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTree(t *testing.T) {
cl, cleanup := testClientAndServer()
defer cleanup()

if err := cl.auth(); err != nil {
t.Fatalf("Couldn't auth: " + err.Error())
}

buf := &bytes.Buffer{}
if err := cl.tree(buf); err != nil {
t.Fatalf("Couldn't load tree: " + err.Error())
}

assert.Equal(t, `{"tree":1}`, buf.String())
}

func TestDir(t *testing.T) {
cl, cleanup := testClientAndServer()
defer cleanup()

if err := cl.auth(); err != nil {
t.Fatalf("Couldn't auth: " + err.Error())
}

buf := &bytes.Buffer{}
if err := cl.directory(buf); err != nil {
t.Fatalf("Couldn't load directory: " + err.Error())
}

assert.Equal(t, `dir`, buf.String())
}
@@ -0,0 +1,75 @@
package bamboo

import (
"io"
"net/http"
"net/http/httptest"
"os"
)

func authHandler(rw http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
f, err := os.Open("./testdata/auth.html")
if err != nil {
panic("Couldn't load testdata: " + err.Error())
}

if _, err := io.Copy(rw, f); err != nil {
panic("Couldn't copy testdata: " + err.Error())
}
return
}
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
panic("Couldn't ParseForm: " + err.Error())
}
http.SetCookie(rw, &http.Cookie{
Name: "auth",
Value: "xyzzy",
})
// fmt.Println(r.Form)
return
}
rw.WriteHeader(404)
}

func treeHandler(rw http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("auth")
if err != nil {
panic("Couldn't parse Cookie: " + err.Error())
}
if c.Value != "xyzzy" {
rw.WriteHeader(401)
return
}

rw.Write([]byte(`json = {"tree":1};` + "\n"))
}

func dirHandler(rw http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("auth")
if err != nil {
panic("Couldn't parse Cookie: " + err.Error())
}
if c.Value != "xyzzy" {
rw.WriteHeader(401)
return
}

rw.Write([]byte("dir"))
}

func testClientAndServer() (*client, func()) {
mx := http.NewServeMux()
mx.Handle("/auth", http.HandlerFunc(authHandler))
mx.Handle("/dir", http.HandlerFunc(dirHandler))
mx.Handle("/tree", http.HandlerFunc(treeHandler))

ts := httptest.NewServer(mx)

return &client{
authURL: ts.URL + "/auth",
dirURL: ts.URL + "/dir",
treeURL: ts.URL + "/tree",
}, ts.Close
}
Oops, something went wrong.

0 comments on commit 480209b

Please sign in to comment.
You can’t perform that action at this time.