github.com/naphatkrit/deis@v1.12.3/client/controller/models/keys/keys_test.go (about) 1 package keys 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "net/http/httptest" 8 "net/url" 9 "reflect" 10 "testing" 11 12 "github.com/deis/deis/client/controller/api" 13 "github.com/deis/deis/client/controller/client" 14 "github.com/deis/deis/version" 15 ) 16 17 const keysFixture string = ` 18 { 19 "created": "2014-01-01T00:00:00UTC", 20 "id": "test@example.com", 21 "owner": "test", 22 "public": "ssh-rsa abc test@example.com", 23 "updated": "2014-01-01T00:00:00UTC", 24 "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75" 25 }` 26 27 const keysListFixture string = ` 28 { 29 "count": 1, 30 "next": null, 31 "previous": null, 32 "results": [ 33 { 34 "created": "2014-01-01T00:00:00UTC", 35 "id": "test@example.com", 36 "owner": "test", 37 "public": "ssh-rsa abc test@example.com", 38 "updated": "2014-01-01T00:00:00UTC", 39 "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75" 40 } 41 ] 42 }` 43 44 const keyCreateExpected string = `{"id":"test@example.com","public":"ssh-rsa abc test@example.com"}` 45 46 type fakeHTTPServer struct{} 47 48 func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { 49 res.Header().Add("DEIS_API_VERSION", version.APIVersion) 50 51 if req.URL.Path == "/v1/keys/" && req.Method == "GET" { 52 res.Write([]byte(keysListFixture)) 53 return 54 } 55 56 if req.URL.Path == "/v1/keys/" && req.Method == "POST" { 57 body, err := ioutil.ReadAll(req.Body) 58 59 if err != nil { 60 fmt.Println(err) 61 res.WriteHeader(http.StatusInternalServerError) 62 res.Write(nil) 63 } 64 65 if string(body) != keyCreateExpected { 66 fmt.Printf("Expected '%s', Got '%s'\n", keyCreateExpected, body) 67 res.WriteHeader(http.StatusInternalServerError) 68 res.Write(nil) 69 return 70 } 71 72 res.WriteHeader(http.StatusCreated) 73 res.Write([]byte(keysFixture)) 74 return 75 } 76 77 if req.URL.Path == "/v1/keys/test@example.com" && req.Method == "DELETE" { 78 res.WriteHeader(http.StatusNoContent) 79 res.Write(nil) 80 return 81 } 82 83 fmt.Printf("Unrecongized URL %s\n", req.URL) 84 res.WriteHeader(http.StatusNotFound) 85 res.Write(nil) 86 } 87 88 func TestKeysList(t *testing.T) { 89 t.Parallel() 90 91 expected := []api.Key{ 92 api.Key{ 93 Created: "2014-01-01T00:00:00UTC", 94 ID: "test@example.com", 95 Owner: "test", 96 Public: "ssh-rsa abc test@example.com", 97 Updated: "2014-01-01T00:00:00UTC", 98 UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 99 }, 100 } 101 102 handler := fakeHTTPServer{} 103 server := httptest.NewServer(handler) 104 defer server.Close() 105 106 u, err := url.Parse(server.URL) 107 108 if err != nil { 109 t.Fatal(err) 110 } 111 112 httpClient := client.CreateHTTPClient(false) 113 114 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 115 116 actual, _, err := List(&client, 100) 117 118 if err != nil { 119 t.Fatal(err) 120 } 121 122 if !reflect.DeepEqual(expected, actual) { 123 t.Errorf("Expected %v, Got %v", expected, actual) 124 } 125 } 126 127 func TestKeyCreate(t *testing.T) { 128 t.Parallel() 129 130 expected := api.Key{ 131 Created: "2014-01-01T00:00:00UTC", 132 ID: "test@example.com", 133 Owner: "test", 134 Public: "ssh-rsa abc test@example.com", 135 Updated: "2014-01-01T00:00:00UTC", 136 UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75", 137 } 138 139 handler := fakeHTTPServer{} 140 server := httptest.NewServer(handler) 141 defer server.Close() 142 143 u, err := url.Parse(server.URL) 144 145 if err != nil { 146 t.Fatal(err) 147 } 148 149 httpClient := client.CreateHTTPClient(false) 150 151 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 152 153 actual, err := New(&client, "test@example.com", "ssh-rsa abc test@example.com") 154 155 if err != nil { 156 t.Fatal(err) 157 } 158 159 if !reflect.DeepEqual(expected, actual) { 160 t.Errorf("Expected %v, Got %v", expected, actual) 161 } 162 } 163 164 func TestKeysDestroy(t *testing.T) { 165 t.Parallel() 166 167 handler := fakeHTTPServer{} 168 server := httptest.NewServer(&handler) 169 defer server.Close() 170 171 u, err := url.Parse(server.URL) 172 173 if err != nil { 174 t.Fatal(err) 175 } 176 177 httpClient := client.CreateHTTPClient(false) 178 179 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 180 181 if err = Delete(&client, "test@example.com"); err != nil { 182 t.Fatal(err) 183 } 184 }