Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Jan 19, 2024
1 parent f11ef03 commit fa6bba2
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 21 deletions.
28 changes: 17 additions & 11 deletions lib/accent/integrations/execute/azure_storage_container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Accent.IntegrationManager.Execute.AzureStorageContainer do

def upload_translations(integration, params) do
project = Repo.one!(Ecto.assoc(integration, :project))
version = fetch_version(project, params.azure_storage_container)
version = fetch_version(project, params)
documents = fetch_documents(project)
revisions = fetch_revisions(project)
master_revision = Repo.preload(Repo.one!(RevisionScope.master(Ecto.assoc(project, :revisions))), :language)
Expand All @@ -23,15 +23,19 @@ defmodule Accent.IntegrationManager.Execute.AzureStorageContainer do
Enum.flat_map(revisions, fn revision ->
translations = fetch_translations(document, revision, version)

render_options = %{
translations: translations,
master_language: Accent.Revision.language(master_revision),
language: Accent.Revision.language(revision),
document: document
}
if Enum.any?(translations) do
render_options = %{
translations: translations,
master_language: Revision.language(master_revision),
language: Revision.language(revision),
document: document
}

%{render: render} = Accent.TranslationsRenderer.render_translations(render_options)
[%{document: %{document | render: render}, language: Accent.Revision.language(revision)}]
%{render: render} = Accent.TranslationsRenderer.render_translations(render_options)
[%{document: %{document | render: render}, language: Accent.Revision.language(revision)}]
else
[]
end
end)
end)

Expand All @@ -40,12 +44,14 @@ defmodule Accent.IntegrationManager.Execute.AzureStorageContainer do
:ok = File.write(file, upload.document.render)

uri = URI.parse(integration.data.azure_storage_container_sas)
extension = Accent.DocumentFormat.extension_by_format(upload.document.format)

path =
Path.join([
uri.path,
(version && version.tag) || "latest",
upload.language.slug,
upload.document.path <> "." <> upload.document.format
upload.document.path <> "." <> extension
])

HTTPoison.put(URI.to_string(%{uri | path: path}), {:file, file}, [{"x-ms-blob-type", "BlockBlob"}])
Expand All @@ -56,7 +62,7 @@ defmodule Accent.IntegrationManager.Execute.AzureStorageContainer do

defp fetch_version(project, %{target_version: :specific, tag: tag}) do
Version
|> VersionScope.from_project(project)
|> VersionScope.from_project(project.id)
|> VersionScope.from_tag(tag)
|> Repo.one()
end
Expand Down
5 changes: 4 additions & 1 deletion lib/accent/integrations/integration_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ defmodule Accent.IntegrationManager do
end

defp execute_integration(%{service: "azure_storage_container"} = integration, params) do
Accent.IntegrationManager.Execute.AzureStorageContainer.upload_translations(integration, params)
Accent.IntegrationManager.Execute.AzureStorageContainer.upload_translations(
integration,
params[:azure_storage_container]
)

:ok
end
Expand Down
16 changes: 11 additions & 5 deletions lib/accent/schemas/document_format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ defmodule Accent.DocumentFormat do
@moduledoc false
defmacro ids, do: Enum.map(Langue.modules(), & &1.id)

def all,
do:
Enum.map(Langue.modules(), fn module ->
%{name: module.display_name(), slug: module.id(), extension: module.extension()}
end)
def all do
Enum.map(Langue.modules(), fn module ->
%{name: module.display_name(), slug: module.id(), extension: module.extension()}
end)
end

def extension_by_format(slug) do
Enum.find_value(Langue.modules(), fn module ->
module.id() === slug && module.extension()
end)
end
end
3 changes: 2 additions & 1 deletion test/graphql/resolvers/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ defmodule AccentTest.GraphQL.Resolvers.Integration do
{:ok, integration} = Resolver.create(project, %{service: "foo", data: %{url: ""}}, context)

assert integration.errors == [
service: {"is invalid", [validation: :inclusion, enum: ["slack", "github", "discord", "azure"]]}
service:
{"is invalid", [validation: :inclusion, enum: ["slack", "github", "discord", "azure_storage_container"]]}
]

assert Repo.all(Integration) == []
Expand Down
116 changes: 116 additions & 0 deletions test/services/integration_manager_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
defmodule AccentTest.IntegrationManager do
@moduledoc false
use Accent.RepoCase, async: true

import Mock

alias Accent.Document
alias Accent.Integration
alias Accent.IntegrationManager
alias Accent.Language
alias Accent.Project
alias Accent.Repo
alias Accent.Revision
alias Accent.Translation
alias Accent.User
alias Accent.Version

describe "execute" do
setup do
project = Repo.insert!(%Project{main_color: "red", name: "com"})
user = Repo.insert!(%User{email: "[email protected]"})
language = Repo.insert!(%Language{slug: "fr-custom", name: "Fr"})
revision = Repo.insert!(%Revision{project: project, language: language})
document = Repo.insert!(%Document{project: project, path: "foo", format: "gettext"})

{:ok, [project: project, user: user, language: language, revision: revision, document: document]}
end

test "azure storage container with version", %{
user: user,
revision: revision,
document: document,
project: project
} do
version = Repo.insert!(%Version{project: project, tag: "1.2.45", name: "vNext", user: user})

Repo.insert!(%Translation{
revision: revision,
document: document,
key: "key",
corrected_text: "value latest"
})

Repo.insert!(%Translation{
revision: revision,
version: version,
document: document,
key: "key",
corrected_text: "value v1.2.45"
})

integration =
Repo.insert!(%Integration{
project: project,
user: user,
service: "azure_storage_container",
data: %{azure_storage_container_sas: "http://azure.blob.test/container?sas=1234"}
})

with_mock HTTPoison,
put: fn url, {:file, file}, headers ->
content = File.read!(file)

assert content === """
msgid "key"
msgstr "value v1.2.45"
"""

assert String.ends_with?(url, "1.2.45/fr-custom/foo.po?sas=1234")
assert headers === [{"x-ms-blob-type", "BlockBlob"}]
{:ok, nil}
end do
IntegrationManager.execute(integration, user, %{
azure_storage_container: %{target_version: :specific, tag: "1.2.45"}
})
end

updated_integration = Repo.reload!(integration)

assert updated_integration.last_executed_at
assert updated_integration.last_executed_by_user_id === user.id
end

test "azure storage container latest version", %{
user: user,
revision: revision,
document: document,
project: project
} do
Repo.insert!(%Translation{revision: revision, document: document, key: "key", corrected_text: "value"})

integration =
Repo.insert!(%Integration{
project: project,
user: user,
service: "azure_storage_container",
data: %{azure_storage_container_sas: "http://azure.blob.test/container?sas=1234"}
})

with_mock HTTPoison,
put: fn url, body, headers ->
assert match?({:file, _}, body)
assert String.ends_with?(url, "latest/fr-custom/foo.po?sas=1234")
assert headers === [{"x-ms-blob-type", "BlockBlob"}]
{:ok, nil}
end do
IntegrationManager.execute(integration, user, %{azure_storage_container: %{target_version: :latest}})
end

updated_integration = Repo.reload!(integration)

assert updated_integration.last_executed_at
assert updated_integration.last_executed_by_user_id === user.id
end
end
end
3 changes: 2 additions & 1 deletion webapp/app/locales/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@
"cancel_button": "Cancel",
"error": "Error while pushing to Azure. Check your credentials and try again.",
"push_button": "Upload",
"sas_base_url": "SAS base URL",
"sas_base_url": "Base upload URL",
"submit_confirm": "Uploading files to Azure Storage Container can override existing files if you upload for the latest or an already uploaded version tag.",
"target_version": {
"label": "Target Version",
"options": {
Expand Down
2 changes: 2 additions & 0 deletions webapp/app/locales/fr-ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@
"cancel_button": "Annuler",
"error": "Une erreur s’est produite lors de la publication sur Azure. Veuillez vérifier vos informations d’identification et réessayer.",
"push_button": "Publier",
"sas_base_url": "Base de l’URL de publication",
"submit_confirm": "La publication de fichiers vers Azure Storage Container peut remplacer les fichiers existants si vous publiez la dernière version ou une version déjà publiée.",
"target_version": {
"label": "Version Cible",
"options": {
Expand Down
1 change: 1 addition & 0 deletions webapp/app/pods/components/azure-push-form/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
font-size: 27px;
font-weight: 300;
color: var(--color-primary);
line-height: 1.2;
}

.text {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,17 @@ export default class IntegrationExecuteAzureStorageContainer extends Component<A

@action
async submit() {
const confirmMessage = this.intl.t(
'components.project_settings.integrations.execute.azure_storage_container.submit_confirm'
);
/* eslint-disable-next-line no-alert */
if (!window.confirm(confirmMessage)) {
return;
}

const response = await this.apolloMutate.mutate({
mutation: executeIntegration,
refetchQueries: ['ProjectServiceIntegrations'],
variables: {
integrationId: this.args.integration.id,
azureStorageContainer: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.title {
margin-bottom: 20px;
text-align: center;
line-height: 1.2;
font-size: 27px;
font-weight: 300;
color: var(--color-primary);
Expand All @@ -19,6 +20,7 @@
padding-bottom: 16px;
border-bottom: 1px solid var(--background-light-highlight);
font-size: 13px;

div {
display: flex;
flex-direction: column;
Expand All @@ -28,6 +30,7 @@
span {
font-size: 12px;
font-family: var(--font-monospace);
opacity: 0.6;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
{{t 'components.project_settings.integrations.execute.azure_storage_container.push_button'}}
</AsyncButton>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
<AccModal @small={{true}} @onClose={{fn this.toggleExecuting}}>
{{component this.dataExecuteComponent integration=@integration close=this.toggleExecuting}}
</AccModal>
{{/if}}
{{/if}}

0 comments on commit fa6bba2

Please sign in to comment.