Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 612d966

Browse files
committed
test: Add some initial tests for our API server
Only covers the branches(), columns(), and commits() functions so far. More to come later.
1 parent 2c36ce0 commit 612d966

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

common/cypress.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ func CypressSeed(w http.ResponseWriter, r *http.Request) {
6060
return
6161
}
6262

63+
// Add some API keys
64+
keys := map[string]string{
65+
"2MXwA5jGZkIQ3UNEcKsuDNSPMlx": "default",
66+
"2MXw0cd7IBAGR6mm0JX6O5BdySJ": "default",
67+
"2MXwB8hvXgUHlCkXq5odLe4L05j": "default",
68+
"2MXwGkD0il29I0e98rptPlfnABr": "first",
69+
"2MXwIsi2wUIqvzN6lNkpxqmsDQK": "second",
70+
"2MXwJkTQVonjJqNlpIFyA9BNtE6": "third",
71+
}
72+
for key, user := range keys {
73+
err = APIKeySave(key, user, time.Now())
74+
if err != nil {
75+
http.Error(w, err.Error(), http.StatusInternalServerError)
76+
return
77+
}
78+
}
79+
log.Println("API keys added to database")
80+
6381
// Log the database reset
6482
log.Println("Test data added to database")
6583
return

cypress/e2e/2-api/api.cy.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
describe('api tests', () => {
2+
before(() => {
3+
// Seed data
4+
cy.request('/x/test/seed')
5+
})
6+
7+
// Branches
8+
// Equivalent curl command:
9+
// curl -k -F apikey="2MXwA5jGZkIQ3UNEcKsuDNSPMlx" -F dbowner="default" \
10+
// -F dbname="Assembly Election 2017.sqlite" https://localhost:9444/v1/branches
11+
it('branches', () => {
12+
cy.request({
13+
method: 'POST',
14+
url: 'https://localhost:9444/v1/branches',
15+
form: true,
16+
body: {
17+
apikey: '2MXwA5jGZkIQ3UNEcKsuDNSPMlx',
18+
dbowner: 'default',
19+
dbname: 'Assembly Election 2017.sqlite'
20+
},
21+
}).then(
22+
(response) => {
23+
expect(response.status).to.eq(200)
24+
let jsonBody = JSON.parse(response.body)
25+
expect(jsonBody).to.include.keys(['branches', 'default_branch'])
26+
expect(jsonBody).to.have.property('default_branch', 'main')
27+
expect(jsonBody.branches.main).to.have.property('commit')
28+
expect(jsonBody.branches.main).to.have.property('commit_count', 1)
29+
expect(jsonBody.branches.main).to.have.property('description', '')
30+
}
31+
)
32+
})
33+
34+
// Columns
35+
// Equivalent curl command:
36+
// curl -k -F apikey="2MXwA5jGZkIQ3UNEcKsuDNSPMlx" \
37+
// -F dbowner="default" -F dbname="Assembly Election 2017.sqlite" \
38+
// -F table="Candidate_Information" \
39+
// https://localhost:9444/v1/columns
40+
it('columns', () => {
41+
cy.request({
42+
method: 'POST',
43+
url: 'https://localhost:9444/v1/columns',
44+
form: true,
45+
body: {
46+
apikey: '2MXwA5jGZkIQ3UNEcKsuDNSPMlx',
47+
dbowner: 'default',
48+
dbname: 'Assembly Election 2017.sqlite',
49+
table: 'Candidate_Information'
50+
},
51+
}).then(
52+
(response) => {
53+
expect(response.status).to.eq(200)
54+
let jsonBody = JSON.parse(response.body)
55+
expect(jsonBody[0]).to.include.keys(['column_id', 'default_value', 'name', 'not_null'])
56+
}
57+
)
58+
})
59+
60+
// Commits
61+
// Equivalent curl command:
62+
// curl -k -F apikey="2MXwA5jGZkIQ3UNEcKsuDNSPMlx" \
63+
// -F dbowner="default" -F dbname="Assembly Election 2017.sqlite" \
64+
// https://localhost:9444/v1/commits
65+
it('commits', () => {
66+
cy.request({
67+
method: 'POST',
68+
url: 'https://localhost:9444/v1/commits',
69+
form: true,
70+
body: {
71+
apikey: '2MXwA5jGZkIQ3UNEcKsuDNSPMlx',
72+
dbowner: 'default',
73+
dbname: 'Assembly Election 2017.sqlite',
74+
},
75+
}).then(
76+
(response) => {
77+
expect(response.status).to.eq(200)
78+
79+
// Needs an extra step, due to the structure of the returned JSON
80+
let temp = JSON.parse(response.body)
81+
let jsonBody = temp[Object.keys(temp)[0]]
82+
83+
expect(jsonBody).to.have.property('author_email', '[email protected]')
84+
expect(jsonBody).to.have.property('author_name', 'Default system user')
85+
expect(jsonBody).to.have.property('committer_email', '')
86+
expect(jsonBody).to.have.property('committer_name', '')
87+
expect(jsonBody).to.have.property('message', 'Initial commit')
88+
expect(jsonBody).to.have.property('other_parents', null)
89+
expect(jsonBody).to.have.property('parent', '')
90+
expect(jsonBody).to.include.keys(['id', 'timestamp', 'tree'])
91+
92+
// Test the "tree" entries
93+
let entries = jsonBody.tree.entries[0]
94+
expect(entries).to.have.property('entry_type', 'db')
95+
expect(entries).to.have.property('licence', '9348ddfd44da5a127c59141981954746a860ec8e03e0412cf3af7134af0f97e2')
96+
expect(entries).to.have.property('name', 'Assembly Election 2017.sqlite')
97+
expect(entries).to.have.property('sha256', '4244d55013359c6476d06c045700139629ecfd2752ffad141984ba14ecafd17e')
98+
expect(entries).to.have.property('size', 57344)
99+
expect(entries).to.include.keys(['last_modified'])
100+
}
101+
)
102+
})
103+
})

webui/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func apiKeyGenHandler(w http.ResponseWriter, r *http.Request) {
6464

6565
// Save the API key in PG database
6666
err = com.APIKeySave(key, loggedInUser, creationTime)
67+
if err != nil {
68+
http.Error(w, err.Error(), http.StatusInternalServerError)
69+
return
70+
}
6771

6872
// Log the key creation
6973
log.Printf("New API key created for user '%s', key: '%s'\n", loggedInUser, key)

0 commit comments

Comments
 (0)