diff --git a/server/auth.go b/server/auth.go index 593c268..d97b2dd 100644 --- a/server/auth.go +++ b/server/auth.go @@ -10,20 +10,20 @@ import ( // DeviceId is decided by the device. UserId is decided by the server, and is // gatekept by Email/Password -type AuthFullRequest struct { +type AuthRequest struct { DeviceId auth.DeviceId `json:"deviceId"` Email auth.Email `json:"email"` Password auth.Password `json:"password"` } -func (r *AuthFullRequest) validate() bool { +func (r *AuthRequest) validate() bool { return (r.DeviceId != "" && r.Email != auth.Email("") && // TODO email validation. Here or store. Stdlib does it: https://stackoverflow.com/a/66624104 r.Password != auth.Password("")) } -func (s *Server) getAuthTokenFull(w http.ResponseWriter, req *http.Request) { - var authRequest AuthFullRequest +func (s *Server) getAuthToken(w http.ResponseWriter, req *http.Request) { + var authRequest AuthRequest if !getPostData(w, req, &authRequest) { return } diff --git a/server/auth_test.go b/server/auth_test.go index 00faad2..5b02419 100644 --- a/server/auth_test.go +++ b/server/auth_test.go @@ -20,10 +20,10 @@ func TestServerAuthHandlerSuccess(t *testing.T) { requestBody := []byte(`{"deviceId": "dev-1", "email": "abc@example.com", "password": "123"}`) - req := httptest.NewRequest(http.MethodPost, PathAuthTokenFull, bytes.NewBuffer(requestBody)) + req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer(requestBody)) w := httptest.NewRecorder() - s.getAuthTokenFull(w, req) + s.getAuthToken(w, req) body, _ := ioutil.ReadAll(w.Body) var result auth.AuthToken @@ -130,10 +130,10 @@ func TestServerAuthHandlerErrors(t *testing.T) { server := Server{&testAuth, &testStore, &wallet.WalletUtil{}} // Make request - req := httptest.NewRequest(tc.method, PathAuthTokenFull, bytes.NewBuffer([]byte(tc.requestBody))) + req := httptest.NewRequest(tc.method, PathAuthToken, bytes.NewBuffer([]byte(tc.requestBody))) w := httptest.NewRecorder() - server.getAuthTokenFull(w, req) + server.getAuthToken(w, req) if want, got := tc.expectedStatusCode, w.Result().StatusCode; want != got { t.Errorf("StatusCode: expected %d, got %d", want, got) @@ -153,7 +153,7 @@ func TestServerAuthHandlerErrors(t *testing.T) { } } -func TestServerValidateAuthFullRequest(t *testing.T) { - t.Fatalf("Test me: Implement and test AuthFullRequest.validate()") +func TestServerValidateAuthRequest(t *testing.T) { + t.Fatalf("Test me: Implement and test AuthRequest.validate()") } diff --git a/server/integration_test.go b/server/integration_test.go index 7a3639d..a4dbc7b 100644 --- a/server/integration_test.go +++ b/server/integration_test.go @@ -89,8 +89,8 @@ func TestIntegrationWalletUpdates(t *testing.T) { responseBody, statusCode = request( t, http.MethodPost, - s.getAuthTokenFull, - PathAuthTokenFull, + s.getAuthToken, + PathAuthToken, &authToken1, `{"deviceId": "dev-1", "email": "abc@example.com", "password": "123"}`, ) @@ -117,8 +117,8 @@ func TestIntegrationWalletUpdates(t *testing.T) { responseBody, statusCode = request( t, http.MethodPost, - s.getAuthTokenFull, - PathAuthTokenFull, + s.getAuthToken, + PathAuthToken, &authToken2, `{"deviceId": "dev-2", "email": "abc@example.com", "password": "123"}`, ) diff --git a/server/server.go b/server/server.go index 4ecedde..b22406d 100644 --- a/server/server.go +++ b/server/server.go @@ -12,7 +12,7 @@ import ( // TODO proper doc comments! -const PathAuthTokenFull = "/auth/full" +const PathAuthToken = "/auth/full" const PathRegister = "/signup" const PathWalletState = "/wallet-state" @@ -154,7 +154,7 @@ func (s *Server) checkAuth( // PUT = "...creates a new resource or replaces a representation of the target resource with the request payload." func (s *Server) Serve() { - http.HandleFunc(PathAuthTokenFull, s.getAuthTokenFull) + http.HandleFunc(PathAuthToken, s.getAuthToken) http.HandleFunc(PathWalletState, s.handleWalletState) http.HandleFunc(PathRegister, s.register) diff --git a/test_client/README.md b/test_client/README.md index b537599..903c692 100644 --- a/test_client/README.md +++ b/test_client/README.md @@ -33,9 +33,9 @@ Registered Now that the account exists, grab an auth token with both clients. ``` ->>> c1.get_full_auth_token() +>>> c1.get_auth_token() Got auth token: 941e5159a2caff15f0bdc1c0e6da92691d3073543dbfae810cfe57d51c35f0e0 ->>> c2.get_full_auth_token() +>>> c2.get_auth_token() Got auth token: b323a18e51263ac052777ca68de716c1f3b4983bf4c918477e355f637c8ea2d4 ``` diff --git a/test_client/gen-readme.py b/test_client/gen-readme.py index 9fa855b..7c1c40a 100644 --- a/test_client/gen-readme.py +++ b/test_client/gen-readme.py @@ -54,8 +54,8 @@ Now that the account exists, grab an auth token with both clients. """) code_block(""" -c1.get_full_auth_token() -c2.get_full_auth_token() +c1.get_auth_token() +c2.get_auth_token() """) # TODO - wait isn't it redundant to have the `deviceId` field, for the same reason it's redundant to have the `sequence` field? diff --git a/test_client/test_client.py b/test_client/test_client.py index c7d00df..b5983be 100755 --- a/test_client/test_client.py +++ b/test_client/test_client.py @@ -3,7 +3,7 @@ import random, string, json, uuid, requests, hashlib from pprint import pprint BASE_URL = 'http://localhost:8090' -AUTH_FULL_URL = BASE_URL + '/auth/full' +AUTH_URL = BASE_URL + '/auth/full' REGISTER_URL = BASE_URL + '/signup' WALLET_STATE_URL = BASE_URL + '/wallet-state' @@ -97,15 +97,13 @@ class Client(): return print ("Registered") - # TODO - Rename to get_auth_token. same in go. Remember to grep, gotta change - # it in README as well. - def get_full_auth_token(self): + def get_auth_token(self): body = json.dumps({ 'email': self.email, 'password': create_login_password(self.root_password), 'deviceId': self.device_id, }) - response = requests.post(AUTH_FULL_URL, body) + response = requests.post(AUTH_URL, body) if response.status_code != 200: print ('Error', response.status_code) print (response.content)