github.com/apremalal/vamps-core@v1.0.1-0.20161221121535-d430b56ec174/api_test/authentication_test.go (about) 1 package api_test 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io" 8 "io/ioutil" 9 "net/http" 10 "net/http/httptest" 11 "os" 12 "os/exec" 13 "strconv" 14 "strings" 15 "testing" 16 17 "github.com/gorilla/mux" 18 "github.com/vedicsoft/vamps-core/commons" 19 "github.com/vedicsoft/vamps-core/models" 20 "github.com/vedicsoft/vamps-core/routes" 21 ) 22 23 var m *mux.Router 24 var req *http.Request 25 var respRec *httptest.ResponseRecorder 26 27 type JWTResponse struct { 28 Token string 29 TenantId int 30 } 31 32 var redisProcess *exec.Cmd 33 var jwtResponse JWTResponse 34 35 func TestMain(m *testing.M) { 36 setup() 37 code := m.Run() 38 shutdown() 39 os.Exit(code) 40 } 41 42 //initializing the server for api tests 43 func setup() { 44 serverConfigs := commons.InitConfigurations(commons.GetServerHome() + "/resources/.test/config.default.yaml") 45 commons.ConstructConnectionPool(serverConfigs.DBConfigMap) 46 47 //create the database in sqlite 48 constructTestDB(serverConfigs.Home) 49 redisProcess = startRedis(serverConfigs.Home) 50 //constructing new routes 51 m = routes.NewRouter() 52 //The response recorder used to record HTTP responses 53 respRec = httptest.NewRecorder() 54 } 55 56 func shutdown() { 57 //deleting sqlite database 58 err := os.Remove(commons.GetServerHome() + "/resources/.test/vampstest.db") 59 if err != nil { 60 fmt.Println("Unable to remove the test databsae stack:" + err.Error()) 61 } 62 63 // Stopping redis process by reading the pid 64 redisPid, err := ioutil.ReadFile(commons.GetServerHome() + "/resources/.test/redis.pid") 65 err = redisProcess.Process.Kill() 66 redisProcess.Wait() 67 68 redisPid2, err := strconv.Atoi(string(redisPid)[:len(string(redisPid))-1]) 69 if err != nil { 70 println(err.Error()) 71 } 72 redisProcess2, _ := os.FindProcess(redisPid2) 73 redisProcess2.Kill() 74 redisProcess2.Wait() 75 if err != nil { 76 fmt.Println(err.Error()) 77 } 78 } 79 80 //takes the sqlite database descriptor and create a new one 81 func constructTestDB(serverHome string) { 82 os.Chdir(serverHome + "/resources/.test") 83 c1 := exec.Command("cat", "sqlite_serverdb.sql") 84 c2 := exec.Command("./sqlite3", "vampstest.db") 85 r, w := io.Pipe() 86 c1.Stdout = w 87 c2.Stdin = r 88 89 var b2 bytes.Buffer 90 c2.Stdout = &b2 91 92 c1.Start() 93 c2.Start() 94 c1.Wait() 95 w.Close() 96 c2.Wait() 97 io.Copy(os.Stdout, &b2) 98 } 99 100 func startRedis(serverHome string) *exec.Cmd { 101 os.Chdir(serverHome + "/resources/.test") 102 // Starting caddy server to server static files 103 args := []string{"redis.default.conf"} 104 cmd := exec.Command("./redis-server", args...) 105 if err := cmd.Start(); err != nil { 106 fmt.Println("Error occourred while starting redis server : ", err.Error()) 107 os.Exit(1) 108 } 109 return cmd 110 } 111 112 func TestLogin(t *testing.T) { 113 user := models.SystemUser{Username: "admin@super.com", Password: "admin"} 114 b, err := json.Marshal(user) 115 req, err = http.NewRequest("POST", "/login", strings.NewReader(string(b))) 116 if err != nil { 117 t.Fatal("Creating 'POST /login' request failed!") 118 } 119 //The response recorder used to record HTTP responses 120 respRec = httptest.NewRecorder() 121 m.ServeHTTP(respRec, req) 122 if respRec.Code != http.StatusOK { 123 //TestDeleteUser(t) 124 t.Fatal("Server error: Returned ", respRec.Code, " instead of ", http.StatusBadRequest) 125 } 126 t.Log(respRec.Body) 127 decoder := json.NewDecoder(respRec.Body) 128 err = decoder.Decode(&jwtResponse) 129 if err != nil { 130 t.Error("Error while decoding JWT token responce") 131 } 132 respRec.Flush() 133 } 134 135 func TestLogout(t *testing.T) { 136 user := models.SystemUser{Username: "admin@super.com", Password: "admin"} 137 b, err := json.Marshal(user) 138 req, err = http.NewRequest("POST", "/logout", strings.NewReader(string(b))) 139 if err != nil { 140 t.Fatal("Creating 'POST /logout' request failed!") 141 } 142 req.Header.Set("Authorization", "Bearer "+jwtResponse.Token) 143 respRec = httptest.NewRecorder() 144 m.ServeHTTP(respRec, req) 145 if respRec.Code != http.StatusOK { 146 //TestDeleteUser(t) 147 t.Fatal("Server error: Returned ", respRec.Code, " instead of ", http.StatusBadRequest) 148 } 149 t.Log(respRec.Body) 150 }