github.com/michaellihs/golab@v0.1.0-beta3.0.20180726222757-f5cdabc76dfd/tests/user_test.go (about) 1 // Copyright © 2017 Michael Lihs 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package tests 22 23 import ( 24 "testing" 25 "os/exec" 26 "fmt" 27 "os" 28 "encoding/json" 29 "log" 30 31 "github.com/xanzy/go-gitlab" 32 "github.com/magiconair/properties/assert" 33 "strconv" 34 ) 35 36 var golabBinary = "golab" 37 38 // see http://lucapette.me/writing-integration-tests-for-a-go-cli-application for basic ideas of testing a CLI with golang 39 func TestMain(m *testing.M) { 40 chdirToParent() 41 os.Exit(m.Run()) 42 } 43 44 func TestUserLs(t *testing.T) { 45 args := []string{"user", "ls"} 46 cmd := exec.Command(golabBinary, args...) 47 out, err := cmd.CombinedOutput() 48 fatalOnErr(err, out, t) 49 } 50 51 func TestUserCreateGetDelete(t *testing.T) { 52 // create a user 53 args := []string{"user", "create", "-u", "root10", "-p", "12341234", "-e", "test10@test.de", "-n", "root10", "--skip_confirmation"} 54 cmd := exec.Command(golabBinary, args...) 55 out, err := cmd.CombinedOutput() 56 fatalOnErr(err, out, t) 57 58 // get it 59 args = []string{"user", "get", "-u", "root10"} 60 cmd = exec.Command(golabBinary, args...) 61 stdout, err := cmd.StdoutPipe() 62 if err := cmd.Start(); err != nil { 63 log.Fatal(err) 64 } 65 var user = gitlab.User{} 66 fatalOnErr(err, out, t) 67 if err := json.NewDecoder(stdout).Decode(&user); err != nil { 68 log.Fatal(err) 69 } 70 if err := cmd.Wait(); err != nil { 71 log.Fatal(err) 72 } 73 assert.Equal(t, user.Username, "root10") 74 assert.Equal(t, user.Email, "test10@test.de") 75 76 // delete it again 77 args = []string{"user", "delete", "-i", strconv.Itoa(user.ID)} 78 cmd = exec.Command(golabBinary, args...) 79 out, err = cmd.CombinedOutput() 80 fatalOnErr(err, out, t) 81 } 82 83 func fatalOnErr(err error, out []byte, t *testing.T) { 84 if err != nil { 85 fmt.Println(string(out)) 86 t.Fatal(err) 87 } 88 } 89 90 func chdirToParent() { 91 err := os.Chdir("..") 92 if err != nil { 93 fmt.Printf("could not change dir: %v", err) 94 os.Exit(1) 95 } 96 }