mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
121 lines
3.5 KiB
Go
121 lines
3.5 KiB
Go
package e2e
|
|
|
|
// TestWorkQueryFlow tests querying a work from fixtures.
|
|
func (s *E2ETestSuite) TestWorkQueryFlow() {
|
|
query := `
|
|
query GetWork($id: ID!) {
|
|
work(id: $id) {
|
|
id
|
|
name
|
|
language
|
|
content
|
|
}
|
|
}
|
|
`
|
|
variables := map[string]interface{}{"id": "1"}
|
|
|
|
resp := s.executeGraphQL(query, variables, "")
|
|
s.Require().NotNil(resp["data"])
|
|
s.Require().Nil(resp["errors"])
|
|
|
|
work := resp["data"].(map[string]interface{})["work"].(map[string]interface{})
|
|
s.Equal("1", work["id"])
|
|
s.Equal("War and Peace", work["name"])
|
|
s.Equal("ru", work["language"])
|
|
|
|
// Content may be the fixture description fallback.
|
|
if work["content"] != nil {
|
|
s.Contains(work["content"].(string), "Epic historical novel")
|
|
}
|
|
}
|
|
|
|
// TestWorkListFlow tests listing works.
|
|
func (s *E2ETestSuite) TestWorkListFlow() {
|
|
query := `query { works(limit: 20, offset: 0) { id name language } }`
|
|
resp := s.executeGraphQL(query, nil, "")
|
|
s.Require().NotNil(resp["data"])
|
|
s.Require().Nil(resp["errors"])
|
|
|
|
works := resp["data"].(map[string]interface{})["works"].([]interface{})
|
|
s.GreaterOrEqual(len(works), 4)
|
|
}
|
|
|
|
// TestCreateWorkFlow tests creating a work (requires authentication).
|
|
func (s *E2ETestSuite) TestCreateWorkFlow() {
|
|
mutation := `
|
|
mutation CreateWork($input: WorkInput!) {
|
|
createWork(input: $input) { id name language }
|
|
}
|
|
`
|
|
variables := map[string]interface{}{
|
|
"input": map[string]interface{}{
|
|
"name": "New Test Work",
|
|
"language": "en",
|
|
"content": "Hello world content",
|
|
},
|
|
}
|
|
|
|
token := s.GenerateToken("contributor")
|
|
resp := s.executeGraphQL(mutation, variables, token)
|
|
s.Require().NotNil(resp["data"])
|
|
s.Require().Nil(resp["errors"])
|
|
|
|
created := resp["data"].(map[string]interface{})["createWork"].(map[string]interface{})
|
|
s.NotEmpty(created["id"].(string))
|
|
s.Equal("New Test Work", created["name"])
|
|
s.Equal("en", created["language"])
|
|
}
|
|
|
|
// TestUpdateWorkFlow tests updating a work (admin can update any work).
|
|
func (s *E2ETestSuite) TestUpdateWorkFlow() {
|
|
mutation := `
|
|
mutation UpdateWork($id: ID!, $input: WorkInput!) {
|
|
updateWork(id: $id, input: $input) { id name language }
|
|
}
|
|
`
|
|
variables := map[string]interface{}{
|
|
"id": "1",
|
|
"input": map[string]interface{}{
|
|
"name": "War and Peace (Updated)",
|
|
"language": "ru",
|
|
},
|
|
}
|
|
|
|
token := s.GenerateToken("admin")
|
|
resp := s.executeGraphQL(mutation, variables, token)
|
|
s.Require().NotNil(resp["data"])
|
|
s.Require().Nil(resp["errors"])
|
|
|
|
updated := resp["data"].(map[string]interface{})["updateWork"].(map[string]interface{})
|
|
s.Equal("1", updated["id"])
|
|
s.Equal("War and Peace (Updated)", updated["name"])
|
|
}
|
|
|
|
// TestDeleteWorkFlow tests deleting a work (admin can delete any work).
|
|
func (s *E2ETestSuite) TestDeleteWorkFlow() {
|
|
mutation := `mutation { deleteWork(id: "4") }`
|
|
token := s.GenerateToken("admin")
|
|
resp := s.executeGraphQL(mutation, nil, token)
|
|
s.Require().NotNil(resp["data"])
|
|
s.Require().Nil(resp["errors"])
|
|
|
|
deleted := resp["data"].(map[string]interface{})["deleteWork"].(bool)
|
|
s.True(deleted)
|
|
|
|
var count int64
|
|
s.DB.Table("works").Where("id = ?", 4).Count(&count)
|
|
s.Equal(int64(0), count)
|
|
}
|
|
|
|
// TestWorkPermissions tests that a non-admin non-author cannot delete a work.
|
|
func (s *E2ETestSuite) TestWorkPermissions() {
|
|
mutation := `mutation { deleteWork(id: "1") }`
|
|
token := s.GenerateToken("reader")
|
|
resp := s.executeGraphQL(mutation, nil, token)
|
|
s.Require().NotNil(resp["errors"], "expected authorization error")
|
|
|
|
var count int64
|
|
s.DB.Table("works").Where("id = ?", 1).Count(&count)
|
|
s.Equal(int64(1), count)
|
|
}
|