mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-06 02:13:29 +03:00
Add test to Login function
This commit is contained in:
parent
ad153f5f63
commit
490a7fcf52
@ -16,35 +16,67 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Auth", func() {
|
var _ = Describe("Auth", func() {
|
||||||
Describe("CreateAdmin", func() {
|
Describe("Public functions", func() {
|
||||||
var ds model.DataStore
|
var ds model.DataStore
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
var resp *httptest.ResponseRecorder
|
var resp *httptest.ResponseRecorder
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
ds = &tests.MockDataStore{}
|
ds = &tests.MockDataStore{}
|
||||||
req = httptest.NewRequest("POST", "/createAdmin", strings.NewReader(`{"username":"johndoe", "password":"secret"}`))
|
|
||||||
resp = httptest.NewRecorder()
|
|
||||||
CreateAdmin(ds)(resp, req)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
It("creates an admin user with the specified password", func() {
|
Describe("CreateAdmin", func() {
|
||||||
usr := ds.User(context.TODO())
|
BeforeEach(func() {
|
||||||
u, err := usr.FindByUsername("johndoe")
|
req = httptest.NewRequest("POST", "/createAdmin", strings.NewReader(`{"username":"johndoe", "password":"secret"}`))
|
||||||
Expect(err).To(BeNil())
|
resp = httptest.NewRecorder()
|
||||||
Expect(u.Password).ToNot(BeEmpty())
|
CreateAdmin(ds)(resp, req)
|
||||||
Expect(u.IsAdmin).To(BeTrue())
|
})
|
||||||
})
|
|
||||||
|
|
||||||
It("returns the expected payload", func() {
|
It("creates an admin user with the specified password", func() {
|
||||||
Expect(resp.Code).To(Equal(http.StatusOK))
|
usr := ds.User(context.TODO())
|
||||||
var parsed map[string]interface{}
|
u, err := usr.FindByUsername("johndoe")
|
||||||
Expect(json.Unmarshal(resp.Body.Bytes(), &parsed)).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
Expect(parsed["isAdmin"]).To(Equal(true))
|
Expect(u.Password).ToNot(BeEmpty())
|
||||||
Expect(parsed["username"]).To(Equal("johndoe"))
|
Expect(u.IsAdmin).To(BeTrue())
|
||||||
Expect(parsed["name"]).To(Equal("Johndoe"))
|
})
|
||||||
Expect(parsed["id"]).ToNot(BeEmpty())
|
|
||||||
Expect(parsed["token"]).ToNot(BeEmpty())
|
It("returns the expected payload", func() {
|
||||||
|
Expect(resp.Code).To(Equal(http.StatusOK))
|
||||||
|
var parsed map[string]interface{}
|
||||||
|
Expect(json.Unmarshal(resp.Body.Bytes(), &parsed)).To(BeNil())
|
||||||
|
Expect(parsed["isAdmin"]).To(Equal(true))
|
||||||
|
Expect(parsed["username"]).To(Equal("johndoe"))
|
||||||
|
Expect(parsed["name"]).To(Equal("Johndoe"))
|
||||||
|
Expect(parsed["id"]).ToNot(BeEmpty())
|
||||||
|
Expect(parsed["token"]).ToNot(BeEmpty())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
Describe("Login", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
req = httptest.NewRequest("POST", "/login", strings.NewReader(`{"username":"janedoe", "password":"abc123"}`))
|
||||||
|
resp = httptest.NewRecorder()
|
||||||
|
})
|
||||||
|
|
||||||
|
It("fails if user does not exist", func() {
|
||||||
|
Login(ds)(resp, req)
|
||||||
|
Expect(resp.Code).To(Equal(http.StatusUnauthorized))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("logs in successfully if user exists", func() {
|
||||||
|
usr := ds.User(context.TODO())
|
||||||
|
_ = usr.Put(&model.User{ID: "111", UserName: "janedoe", NewPassword: "abc123", Name: "Jane", IsAdmin: false})
|
||||||
|
|
||||||
|
Login(ds)(resp, req)
|
||||||
|
Expect(resp.Code).To(Equal(http.StatusOK))
|
||||||
|
|
||||||
|
var parsed map[string]interface{}
|
||||||
|
Expect(json.Unmarshal(resp.Body.Bytes(), &parsed)).To(BeNil())
|
||||||
|
Expect(parsed["isAdmin"]).To(Equal(false))
|
||||||
|
Expect(parsed["username"]).To(Equal("janedoe"))
|
||||||
|
Expect(parsed["name"]).To(Equal("Jane"))
|
||||||
|
Expect(parsed["id"]).ToNot(BeEmpty())
|
||||||
|
Expect(parsed["token"]).ToNot(BeEmpty())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user