Skip to content

Commit

Permalink
feat: demo project for self hosted (#2410)
Browse files Browse the repository at this point in the history
[issue 2394](#2394)
  • Loading branch information
huglx committed Jul 17, 2024
1 parent 4b9d020 commit 43a4c1a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.tolgee.configuration.tolgee.TolgeeProperties
import io.tolgee.dtos.request.organization.OrganizationDto
import io.tolgee.model.UserAccount
import io.tolgee.security.InitialPasswordManager
import io.tolgee.service.QuickStartService
import io.tolgee.service.organization.OrganizationService
import io.tolgee.service.security.UserAccountService
import org.slf4j.LoggerFactory
Expand All @@ -25,6 +26,7 @@ class InitialUserCreatorCommandLineRunner(
private val organizationService: OrganizationService,
private val passwordEncoder: PasswordEncoder,
private val internalProperties: InternalProperties,
private val quickStartService: QuickStartService,
) : CommandLineRunner, ApplicationListener<ContextClosedEvent> {
private val logger = LoggerFactory.getLogger(this::class.java)

Expand Down Expand Up @@ -71,12 +73,17 @@ class InitialUserCreatorCommandLineRunner(

// If the user was already existing, it may already have assigned orgs.
// To avoid conflicts, we only create the org if the user doesn't have any.
organizationService.create(
OrganizationDto(
properties.authentication.initialUsername,
),
userAccount = user,
)
val organization =
organizationService.create(
OrganizationDto(
properties.authentication.initialUsername,
),
userAccount = user,
)

if (properties.authentication.createDemoForInitialUser) {
quickStartService.create(user, organization)
}
}

fun updatePasswordIfNecessary(initialUser: UserAccount) {
Expand Down
1 change: 1 addition & 0 deletions backend/app/src/main/resources/application-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tolgee:
authorization-url: "https://dummy-url.com/authorize"
token-url: "https://dummy-url.com/oauth/token"
user-url: "https://dummy-url.com/userinfo"
create-demo-for-initial-user: false
rate-limits: # tests go brrrr but then get 429'd and no longer go brrrr
global-limits: false
endpoint-limits: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import io.tolgee.CleanDbBeforeClass
import io.tolgee.commandLineRunners.InitialUserCreatorCommandLineRunner
import io.tolgee.component.fileStorage.FileStorage
import io.tolgee.configuration.tolgee.TolgeeProperties
import io.tolgee.model.enums.OrganizationRoleType
import io.tolgee.repository.QuickStartRepository
import io.tolgee.repository.UserAccountRepository
import io.tolgee.security.InitialPasswordManager
import io.tolgee.service.project.ProjectService
import io.tolgee.service.security.UserAccountService
import io.tolgee.testing.AbstractTransactionalTest
import io.tolgee.testing.ContextRecreatingTest
Expand Down Expand Up @@ -58,11 +61,27 @@ class CreateEnabledTest : AbstractTransactionalTest() {
@Autowired
lateinit var initialUserCreatorCommandLineRunner: InitialUserCreatorCommandLineRunner

@Autowired
lateinit var projectService: ProjectService

@Autowired
lateinit var quickStartRepository: QuickStartRepository

@BeforeAll
fun callTheRunner() {
initialUserCreatorCommandLineRunner.run()
}

@Test
fun `creates demo project for initial user`() {
val johny = userAccountService.findActive("johny")
assertThat(johny!!.organizationRoles).hasSize(1)
assertThat(johny.organizationRoles[0].type).isEqualTo(OrganizationRoleType.OWNER)
val organization = johny.organizationRoles[0].organization!!
val projects = projectService.findAllInOrganization(organizationId = organization.id)
assertThat(projects[0].name).isEqualTo("Demo project")
}

@Test
fun storesPassword() {
assertThat(getPasswordFileContents().toString(Charsets.UTF_8)).isNotBlank
Expand Down Expand Up @@ -109,7 +128,7 @@ class CreateEnabledTest : AbstractTransactionalTest() {
fun cleanUp() {
(fileStorage as InMemoryFileStorage).clear()
resetInitialPassword()

quickStartRepository.deleteAll()
val initial = userAccountService.findActive("johny")!!
userAccountRepository.delete(initial)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ class AuthenticationProperties(
"a set period of time to prevent unauthorized access to images.",
)
var securedImageRetrieval: Boolean = false,
@DocProperty(
description =
"When enabled, creates Demo project and quick start guide for inital user\n",
)
var createDemoForInitialUser: Boolean = true,
@DocProperty(
description = "Expiration time of a generated image access token in milliseconds.",
defaultExplanation = "= 10 minutes",
Expand Down
2 changes: 2 additions & 0 deletions e2e/cypress/e2e/translations/base.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ describe('Translations Base', () => {
key: 'Test key 2',
translation: 'Translated test key 2',
});
cy.contains('Key created').should('be.visible');
cy.wait(100);
cy.xpath(getAnyContainingText('Key', 'a'))
.xpath(getClosestContainingText('Test key 2'))
.scrollIntoView()
Expand Down

0 comments on commit 43a4c1a

Please sign in to comment.