aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/friends-basic.ts
blob: 13edf627312da7d75ff396eff470402a6e956be0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* tslint:disable:no-unused-expression */

import 'mocha'
import * as chai from 'chai'
const expect = chai.expect

import {
  ServerInfo,
  flushTests,
  quitFriends,
  wait,
  setAccessTokensToServers,
  flushAndRunMultipleServers,
  killallServers,
  makeFriends,
  getFriendsList,
  dateIsValid,
  quitOneFriend
} from '../utils'

describe('Test basic friends', function () {
  let servers = []

  function makeFriendsWrapper (podNumber: number) {
    const server = servers[podNumber - 1]
    return makeFriends(server.url, server.accessToken)
  }

  async function testMadeFriends (servers: ServerInfo[], serverToTest: ServerInfo) {
    const friends = []
    for (let i = 0; i < servers.length; i++) {
      if (servers[i].url === serverToTest.url) continue
      friends.push(servers[i].host)
    }

    const res = await getFriendsList(serverToTest.url)

    const result = res.body.data
    expect(result).to.be.an('array')
    expect(result.length).to.equal(2)

    const resultHosts = [ result[0].host, result[1].host ]
    expect(resultHosts[0]).to.not.equal(resultHosts[1])

    const errorString = 'Friends host do not correspond for ' + serverToTest.host
    expect(friends).to.contain(resultHosts[0], errorString)
    expect(friends).to.contain(resultHosts[1], errorString)
  }

  // ---------------------------------------------------------------

  before(async function () {
    this.timeout(120000)

    servers = await flushAndRunMultipleServers(3)

    await setAccessTokensToServers(servers)
  })

  it('Should not have friends', async function () {
    for (const server of servers) {
      const res = await getFriendsList(server.url)

      const result = res.body.data
      expect(result).to.be.an('array')
      expect(result.length).to.equal(0)
    }
  })

  it('Should make friends', async function () {
    this.timeout(120000)

    // The second pod make friend with the third
    await makeFriendsWrapper(2)

    // Wait for the request between pods
    await wait(11000)

    // The second pod should have the third as a friend
    const res1 = await getFriendsList(servers[1].url)

    const friends = res1.body.data
    expect(friends).to.be.an('array')
    expect(friends.length).to.equal(1)

    const pod1 = friends[0]
    expect(pod1.host).to.equal(servers[2].host)
    expect(pod1.email).to.equal('admin3@example.com')
    expect(pod1.score).to.equal(20)
    expect(dateIsValid(pod1.createdAt)).to.be.true

    // Same here, the third pod should have the second pod as a friend
    const res2 = await getFriendsList(servers[2].url)
    const result = res2.body.data
    expect(result).to.be.an('array')
    expect(result.length).to.equal(1)

    const pod2 = result[0]
    expect(pod2.host).to.equal(servers[1].host)
    expect(pod2.email).to.equal('admin2@example.com')
    expect(pod2.score).to.equal(20)
    expect(dateIsValid(pod2.createdAt)).to.be.true

    // Finally the first pod make friend with the second pod
    await makeFriendsWrapper(1)

    // Wait for the request between pods
    await wait(11000)

    // Now each pod should be friend with the other ones
    for (const server of servers) {
      await testMadeFriends(servers, server)
    }
  })

  it('Should not be allowed to make friend again', async function () {
    this.timeout(10000)

    const server = servers[1]
    await makeFriends(server.url, server.accessToken, 409)
  })

  it('Should quit friends of pod 2', async function () {
    this.timeout(10000)

    // Pod 1 quit friends
    await quitFriends(servers[1].url, servers[1].accessToken)

    // Pod 1 should not have friends anymore
    const res = await getFriendsList(servers[1].url)
    const friends = res.body.data
    expect(friends).to.be.an('array')
    expect(friends).to.have.lengthOf(0)

    // Other pods shouldn't have pod 1 too
    const serversToTest = [ servers[0].url, servers[2].url ]
    for (const url of serversToTest) {
      const res = await getFriendsList(url)
      const friends = res.body.data

      expect(friends).to.be.an('array')
      expect(friends.length).to.equal(1)
      expect(friends[0].host).not.to.be.equal(servers[1].host)
    }
  })

  it('Should allow pod 2 to make friend again', async function () {
    this.timeout(120000)

    const server = servers[1]
    await makeFriends(server.url, server.accessToken)
    await wait(11000)

    for (const server of servers) {
      await testMadeFriends(servers, server)
    }
  })

  it('Should allow pod 1 to quit only pod 2', async function () {
    // Pod 1 quits pod 2
    const server = servers[0]

    // Get pod 2 id so we can query it
    const res1 = await getFriendsList(server.url)
    const friends1 = res1.body.data
    let pod1 = friends1.find(friend => (friend.host === servers[1].host))

    // Remove it from the friends list
    await quitOneFriend(server.url, server.accessToken, pod1.id)

    // Pod 1 should have only pod 3 in its friends list
    const res2 = await getFriendsList(servers[0].url)
    const friends2 = res2.body.data
    expect(friends2).to.be.an('array')
    expect(friends2.length).to.equal(1)

    const pod2 = friends2[0]
    expect(pod2.host).to.equal(servers[2].host)

    // Pod 2 should have only pod 3 in its friends list
    const res3 = await getFriendsList(servers[1].url)
    const friends3 = res3.body.data
    expect(friends3).to.be.an('array')
    expect(friends3.length).to.equal(1)

    const pod = friends3[0]
    expect(pod.host).to.equal(servers[2].host)

    // Pod 3 should have both pods in its friends list
    const res4 = await getFriendsList(servers[2].url)
    const friends4 = res4.body.data
    expect(friends4).to.be.an('array')
    expect(friends4.length).to.equal(2)
  })

  after(async function () {
    killallServers(servers)

    if (this['ok']) {
      await flushTests()
    }
  })
})