-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestDataInitializer.kt
169 lines (143 loc) · 7.39 KB
/
TestDataInitializer.kt
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
package backend.configuration
import backend.model.challenges.ChallengeService
import backend.model.event.EventService
import backend.model.event.TeamService
import backend.model.location.LocationService
import backend.model.misc.Coord
import backend.model.misc.EmailAddress
import backend.model.sponsoring.SponsoringService
import backend.model.user.Admin
import backend.model.user.Participant
import backend.model.user.Sponsor
import backend.model.user.UserService
import backend.util.Profiles.DEVELOPMENT
import backend.util.Profiles.HEROKU
import backend.util.euroOf
import org.javamoney.moneta.Money
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Profile
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import javax.annotation.PostConstruct
@Service
@Profile(HEROKU, DEVELOPMENT)
class TestDataInitializer {
// Services
@Autowired lateinit private var userService: UserService
@Autowired lateinit private var teamService: TeamService
@Autowired lateinit private var eventService: EventService
@Autowired lateinit private var locationService: LocationService
@Autowired lateinit private var sponsoringService: SponsoringService
@Autowired lateinit private var challengeService: ChallengeService
@Autowired lateinit private var userDetailsService: UserDetailsService
@Autowired lateinit private var template: JdbcTemplate
@PostConstruct
fun initialize() {
// Don't perform population if an event exists already
if (eventService.exists(1)) {
return
}
val date = LocalDateTime.now().minusHours(1)
// ---- Events ----
val eventMunich = eventService.createEvent("Breakout München", date, "München", Coord(48.1374300, 11.5754900), 36)
val eventBerlin = eventService.createEvent("Breakout Berlin", date, "Berlin", Coord(52.5243700, 13.4105300), 36)
arrayOf(eventMunich, eventBerlin).forEach {
eventService.markAsCurrent(it.id!!)
eventService.allowNewSponsoring(it.id!!)
}
// --- iOS Devs Test Accounts ---
val leo = userService.create("leokaessner@me.com", "password", { addRole(Participant::class) }).getRole(Participant::class)!!
val david = userService.create("david.symhoven@break-out.org", "password", { addRole(Participant::class) }).getRole(Participant::class)!!
val keno = userService.create("keno@break-out.org", "password", { addRole(Sponsor::class) }).getRole(Sponsor::class)!!
val teamiOS = teamService.create(leo, "Team Leo + Elo", "Die fleißigen iOS Tester", eventMunich, null)
setAuthenticatedUser(leo.email)
teamService.invite(EmailAddress(david.email), teamiOS)
setAuthenticatedUser(david.email)
teamService.join(david, teamiOS)
setAuthenticatedUser(keno.email)
sponsoringService.createSponsoring(keno, teamiOS, euroOf(5), euroOf(10000000))
locationService.create(Coord(51.0505, 13.7372), leo, date.plusHours(1))
locationService.create(Coord(48.8534100, 2.3488000), leo, date.plusHours(2))
setAuthenticatedUser(keno.email)
challengeService.proposeChallenge(keno, teamiOS, euroOf(10), "Bade nackt in der Spree", 2)
// ---- Team 1 ----
val participant1 = userService.create("participant1@break-out.org", "password", { addRole(Participant::class) }).getRole(Participant::class)!!
val participant2 = userService.create("participant2@break-out.org", "password", { addRole(Participant::class) }).getRole(Participant::class)!!
val team1 = teamService.create(participant1, "Erstes Team", "Geile Sache", eventMunich, null)
setAuthenticatedUser(participant1.email)
teamService.invite(EmailAddress(participant2.email), team1)
teamService.join(participant2, team1)
// ---- Team 2 ----
val participant3 = userService.create("participant3@break-out.org", "password", { addRole(Participant::class) }).getRole(Participant::class)!!
val participant4 = userService.create("participant4@break-out.org", "password", { addRole(Participant::class) }).getRole(Participant::class)!!
val team2 = teamService.create(participant3, "Zweites Team", "Immer noch geil", eventBerlin, null)
setAuthenticatedUser(participant3.email)
teamService.invite(EmailAddress(participant4.email), team2)
teamService.join(participant4, team2)
// ---- Admin ----
userService.create("admin@break-out.org", "password", { addRole(Admin::class) })
// ---- Sponsor1 ----
val sponsor1 = userService.create("sponsor1@break-out.org", "password", { addRole(Sponsor::class) }).getRole(Sponsor::class)!!
sponsoringService.createSponsoring(sponsor1, team1, Money.parse("EUR 0.1"), Money.parse("EUR 100"))
// ---- Sponsor2 ----
val sponsor2 = userService.create("sponsor2@break-out.org", "password", { addRole(Sponsor::class) }).getRole(Sponsor::class)!!
sponsoringService.createSponsoring(sponsor2, team2, Money.parse("EUR 0.2"), Money.parse("EUR 200"))
// ---- Locations for team1 ----
locationService.create(Coord(51.0505, 13.7372), participant1, date.plusHours(1))
locationService.create(Coord(48.8534100, 2.3488000), participant1, date.plusHours(2))
// ---- Locations for team2 ----
locationService.create(Coord(53.5753200, 10.0153400), participant3, date.plusHours(2))
locationService.create(Coord(52.3740300, 4.8896900), participant3, date.plusHours(2))
template.execute("""
INSERT INTO `oauth_client_details` (
`client_id`,
`resource_ids`,
`client_secret`,
`scope`,
`authorized_grant_types`,
`web_server_redirect_uri`,
`authorities`,
`access_token_validity`,
`refresh_token_validity`,
`additional_information`,
`autoapprove`
)
VALUES
(
'test_breakout_app',
'BREAKOUT_BACKEND',
'123456789',
'read,write',
'password,refresh_token',
'',
'USER',
NULL,
NULL,
'{}',
''
),
(
'test_client_app',
'BREAKOUT_BACKEND',
'123456789',
'read,write',
'password,refresh_token',
'',
'USER',
NULL,
NULL,
'{}',
''
);
""".trimIndent())
}
private fun setAuthenticatedUser(email: String) {
val details = userDetailsService.loadUserByUsername(email)!! // Not null because otherwise exception is thrown
val token = UsernamePasswordAuthenticationToken(details.username, details.password, details.authorities)
SecurityContextHolder.getContext().authentication = token
}
}