From 4ad72851033bdd7a600a6231a886ec6801cc510a Mon Sep 17 00:00:00 2001 From: nxshock Date: Mon, 22 Jul 2019 22:18:58 +0500 Subject: [PATCH] Add tests for memory backend --- internal/backends/memory/backend_test.go | 109 +++++++++++++++++++++++ internal/backends/memory/user_test.go | 16 ---- 2 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 internal/backends/memory/backend_test.go diff --git a/internal/backends/memory/backend_test.go b/internal/backends/memory/backend_test.go new file mode 100644 index 0000000..87e4fb3 --- /dev/null +++ b/internal/backends/memory/backend_test.go @@ -0,0 +1,109 @@ +package memory + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRegisterUser(t *testing.T) { + backend := NewBackend("localhost") + + var ( + username = "user1" + password = "password1" + device = "device1" + ) + + user, token, err := backend.Register(username, password, device) + assert.Nil(t, err) + assert.Equal(t, username, user.Name()) + assert.Equal(t, password, user.Password()) + assert.NotEmpty(t, token) +} + +func TestRegisterUserWithAlreadyTakenName(t *testing.T) { + backend := NewBackend("localhost") + + var ( + userName = "user1" + ) + + _, _, err := backend.Register(userName, "", "") + assert.Nil(t, err) + + _, _, err = backend.Register(userName, "", "") + assert.NotNil(t, err) +} + +func TestLogin(t *testing.T) { + backend := NewBackend("localhost") + + var ( + userName = "user1" + password = "password1" + ) + + _, _, err := backend.Register(userName, password, "") + assert.Nil(t, err) + + token, err := backend.Login(userName, password, "") + assert.Nil(t, err) + assert.NotZero(t, token) +} + +func TestLoginWithWrongCredentials(t *testing.T) { + backend := NewBackend("localhost") + + var ( + userName = "user1" + password = "password1" + ) + + _, _, err := backend.Register(userName, password, "") + assert.Nil(t, err) + + _, err = backend.Login(userName, "wrong password", "") + assert.NotNil(t, err) + + _, err = backend.Login("wrong user name", password, "") + assert.NotNil(t, err) +} + +func TestLogout(t *testing.T) { + backend := NewBackend("localhost") + + var ( + userName = "user1" + password = "password1" + ) + + _, _, err := backend.Register(userName, password, "") + assert.Nil(t, err) + + token, err := backend.Login(userName, password, "") + assert.Nil(t, err) + assert.NotZero(t, token) + + err = backend.Logout(token) + assert.Nil(t, err) +} + +func TestLogoutWithWrongToken(t *testing.T) { + backend := NewBackend("localhost") + + var ( + userName = "user1" + password = "password1" + ) + + _, _, err := backend.Register(userName, password, "") + assert.Nil(t, err) + + token, err := backend.Login(userName, password, "") + assert.Nil(t, err) + assert.NotZero(t, token) + + err = backend.Logout("worng token") + assert.NotNil(t, err) +} diff --git a/internal/backends/memory/user_test.go b/internal/backends/memory/user_test.go index 11673fe..af410b6 100644 --- a/internal/backends/memory/user_test.go +++ b/internal/backends/memory/user_test.go @@ -6,22 +6,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestRegisterUser(t *testing.T) { - backend := NewBackend("localhost") - - var ( - username = "user1" - password = "password1" - device = "device1" - ) - - user, token, err := backend.Register(username, password, device) - assert.Nil(t, err) - assert.Equal(t, username, user.Name()) - assert.Equal(t, password, user.Password()) - assert.NotEmpty(t, token) -} - func TestUserID(t *testing.T) { var ( userName = "user1"