Login (#6)
Reviewed-on: phoenix/textsender-auth#6 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
||||
"git.kundeng.us/phoenix/textsender-auth/internal/utility"
|
||||
)
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
mockstore := NewMockUserStore()
|
||||
handler := NewLoginHandler(mockstore)
|
||||
|
||||
testUser := GetTestUser()
|
||||
unhashedPassword := testUser.Password
|
||||
hashing := utility.HashMash{Password: testUser.Password}
|
||||
hashedPassword, err := hashing.HashPassword()
|
||||
assert.NoError(t, err)
|
||||
|
||||
testUser.Password = hashedPassword
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
mockstore.CreateUser(ctx, &testUser)
|
||||
|
||||
loginUser := LoginAccount{Username: testUser.Username, Password: unhashedPassword}
|
||||
jsonValue, _ := json.Marshal(loginUser)
|
||||
|
||||
req, _ := http.NewRequest("POST", endpoint.Login, strings.NewReader(string(jsonValue)))
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler.Login(rr, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
var response LoginResponse
|
||||
err = json.Unmarshal(rr.Body.Bytes(), &response)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NotEmpty(t, response.Data, "An access token should have been returned")
|
||||
}
|
||||
Reference in New Issue
Block a user