github.com/naphatkrit/deis@v1.12.3/client/controller/models/auth/auth_test.go (about)

     1  package auth
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"net/url"
     9  	"testing"
    10  
    11  	"github.com/deis/deis/client/controller/client"
    12  	"github.com/deis/deis/version"
    13  )
    14  
    15  const registerExpected string = `{"username":"test","password":"opensesame","email":"test@example.com"}`
    16  const loginExpected string = `{"username":"test","password":"opensesame"}`
    17  const passwdExpected string = `{"username":"test","password":"old","new_password":"new"}`
    18  const regenAllExpected string = `{"all":true}`
    19  const regenUserExpected string = `{"username":"test"}`
    20  const cancelUserExpected string = `{"username":"foo"}`
    21  
    22  type fakeHTTPServer struct {
    23  	regenBodyEmpty    bool
    24  	regenBodyAll      bool
    25  	regenBodyUsername bool
    26  	cancelEmpty       bool
    27  	cancelUsername    bool
    28  }
    29  
    30  func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
    31  	res.Header().Add("DEIS_API_VERSION", version.APIVersion)
    32  
    33  	if req.URL.Path == "/v1/auth/register/" && req.Method == "POST" {
    34  		body, err := ioutil.ReadAll(req.Body)
    35  
    36  		if err != nil {
    37  			fmt.Println(err)
    38  			res.WriteHeader(http.StatusInternalServerError)
    39  			res.Write(nil)
    40  		}
    41  
    42  		if string(body) != registerExpected {
    43  			fmt.Printf("Expected '%s', Got '%s'\n", registerExpected, body)
    44  			res.WriteHeader(http.StatusInternalServerError)
    45  			res.Write(nil)
    46  			return
    47  		}
    48  
    49  		res.WriteHeader(http.StatusCreated)
    50  		res.Write(nil)
    51  		return
    52  	}
    53  
    54  	if req.URL.Path == "/v1/auth/login/" && req.Method == "POST" {
    55  		body, err := ioutil.ReadAll(req.Body)
    56  
    57  		if err != nil {
    58  			fmt.Println(err)
    59  			res.WriteHeader(http.StatusInternalServerError)
    60  			res.Write(nil)
    61  			return
    62  		}
    63  
    64  		if string(body) != loginExpected {
    65  			fmt.Printf("Expected '%s', Got '%s'\n", loginExpected, body)
    66  			res.WriteHeader(http.StatusInternalServerError)
    67  			res.Write(nil)
    68  			return
    69  		}
    70  
    71  		res.Write([]byte(`{"token":"abc"}`))
    72  		return
    73  	}
    74  
    75  	if req.URL.Path == "/v1/auth/passwd/" && req.Method == "POST" {
    76  		body, err := ioutil.ReadAll(req.Body)
    77  
    78  		if err != nil {
    79  			fmt.Println(err)
    80  			res.WriteHeader(http.StatusInternalServerError)
    81  			res.Write(nil)
    82  		}
    83  
    84  		if string(body) != passwdExpected {
    85  			fmt.Printf("Expected '%s', Got '%s'\n", passwdExpected, body)
    86  			res.WriteHeader(http.StatusInternalServerError)
    87  			res.Write(nil)
    88  			return
    89  		}
    90  
    91  		res.Write(nil)
    92  		res.WriteHeader(http.StatusInternalServerError)
    93  		return
    94  	}
    95  
    96  	if req.URL.Path == "/v1/auth/tokens/" && req.Method == "POST" {
    97  		body, err := ioutil.ReadAll(req.Body)
    98  
    99  		if err != nil {
   100  			fmt.Println(err)
   101  			res.WriteHeader(http.StatusInternalServerError)
   102  			res.Write(nil)
   103  		}
   104  
   105  		if string(body) == regenAllExpected && !f.regenBodyAll {
   106  			f.regenBodyAll = true
   107  			res.Write(nil)
   108  			return
   109  		} else if string(body) == regenUserExpected && !f.regenBodyUsername {
   110  			f.regenBodyUsername = true
   111  			res.Write([]byte(`{"token":"123"}`))
   112  			return
   113  		} else if string(body) == "" && !f.regenBodyEmpty {
   114  			f.regenBodyEmpty = true
   115  			res.Write([]byte(`{"token":"abc"}`))
   116  			return
   117  		}
   118  
   119  		fmt.Printf("%s is not a valid body.", body)
   120  		res.WriteHeader(http.StatusInternalServerError)
   121  		res.Write(nil)
   122  		return
   123  	}
   124  
   125  	if req.URL.Path == "/v1/auth/cancel/" && req.Method == "DELETE" {
   126  		body, err := ioutil.ReadAll(req.Body)
   127  
   128  		if err != nil {
   129  			fmt.Println(err)
   130  			res.WriteHeader(http.StatusInternalServerError)
   131  			res.Write(nil)
   132  		}
   133  
   134  		if string(body) == cancelUserExpected && !f.cancelUsername {
   135  			f.cancelUsername = true
   136  			res.WriteHeader(http.StatusNoContent)
   137  			res.Write(nil)
   138  			return
   139  		} else if string(body) == "" && !f.cancelEmpty {
   140  			f.cancelEmpty = true
   141  			res.WriteHeader(http.StatusNoContent)
   142  			res.Write(nil)
   143  			return
   144  		}
   145  
   146  		fmt.Printf("%s is not a valid body.", body)
   147  		res.WriteHeader(http.StatusInternalServerError)
   148  		res.Write(nil)
   149  		return
   150  	}
   151  
   152  	fmt.Printf("Unrecognized URL %s\n", req.URL)
   153  	res.WriteHeader(http.StatusNotFound)
   154  	res.Write(nil)
   155  }
   156  
   157  func TestRegister(t *testing.T) {
   158  	t.Parallel()
   159  
   160  	handler := fakeHTTPServer{}
   161  	server := httptest.NewServer(&handler)
   162  	defer server.Close()
   163  
   164  	u, err := url.Parse(server.URL)
   165  
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  
   170  	httpClient := client.CreateHTTPClient(false)
   171  
   172  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u}
   173  
   174  	if err = Register(&client, "test", "opensesame", "test@example.com"); err != nil {
   175  		t.Error(err)
   176  	}
   177  }
   178  
   179  func TestLogin(t *testing.T) {
   180  	t.Parallel()
   181  
   182  	handler := fakeHTTPServer{}
   183  	server := httptest.NewServer(&handler)
   184  	defer server.Close()
   185  
   186  	u, err := url.Parse(server.URL)
   187  
   188  	if err != nil {
   189  		t.Fatal(err)
   190  	}
   191  
   192  	httpClient := client.CreateHTTPClient(false)
   193  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u}
   194  
   195  	actual, err := Login(&client, "test", "opensesame")
   196  
   197  	if err != nil {
   198  		t.Error(err)
   199  	}
   200  
   201  	expected := "abc"
   202  
   203  	if actual != expected {
   204  		t.Errorf("Expected %s, Got %s", expected, actual)
   205  	}
   206  }
   207  
   208  func TestPasswd(t *testing.T) {
   209  	t.Parallel()
   210  
   211  	handler := fakeHTTPServer{}
   212  	server := httptest.NewServer(&handler)
   213  	defer server.Close()
   214  
   215  	u, err := url.Parse(server.URL)
   216  
   217  	if err != nil {
   218  		t.Fatal(err)
   219  	}
   220  
   221  	httpClient := client.CreateHTTPClient(false)
   222  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u}
   223  
   224  	if err := Passwd(&client, "test", "old", "new"); err != nil {
   225  		t.Error(err)
   226  	}
   227  }
   228  
   229  func TestDelete(t *testing.T) {
   230  	t.Parallel()
   231  
   232  	handler := fakeHTTPServer{cancelUsername: false, cancelEmpty: false}
   233  	server := httptest.NewServer(&handler)
   234  	defer server.Close()
   235  
   236  	u, err := url.Parse(server.URL)
   237  
   238  	if err != nil {
   239  		t.Fatal(err)
   240  	}
   241  
   242  	httpClient := client.CreateHTTPClient(false)
   243  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u}
   244  
   245  	if err := Delete(&client, "foo"); err != nil {
   246  		t.Error(err)
   247  	}
   248  
   249  	if err := Delete(&client, ""); err != nil {
   250  		t.Error(err)
   251  	}
   252  }
   253  
   254  func TestRegenerate(t *testing.T) {
   255  	t.Parallel()
   256  
   257  	handler := fakeHTTPServer{}
   258  	server := httptest.NewServer(&handler)
   259  	defer server.Close()
   260  
   261  	u, err := url.Parse(server.URL)
   262  
   263  	if err != nil {
   264  		t.Fatal(err)
   265  	}
   266  
   267  	httpClient := client.CreateHTTPClient(false)
   268  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u}
   269  
   270  	token, err := Regenerate(&client, "", true)
   271  
   272  	if err != nil {
   273  		t.Error(err)
   274  	}
   275  
   276  	if token != "" {
   277  		t.Errorf("Expected token be empty, Got %s", token)
   278  	}
   279  
   280  	token, err = Regenerate(&client, "test", false)
   281  
   282  	if err != nil {
   283  		t.Error(err)
   284  	}
   285  
   286  	expected := "123"
   287  	if token != expected {
   288  		t.Errorf("Expected %s, Got %s", expected, token)
   289  	}
   290  
   291  	token, err = Regenerate(&client, "", false)
   292  
   293  	if err != nil {
   294  		t.Error(err)
   295  	}
   296  
   297  	expected = "abc"
   298  	if token != expected {
   299  		t.Errorf("Expected %s, Got %s", expected, token)
   300  	}
   301  }